Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Replace HttpGet et al with HttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed Oct 25, 2017
1 parent 08fc377 commit 7a1f308
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 46 deletions.
30 changes: 6 additions & 24 deletions client.go
Expand Up @@ -518,37 +518,19 @@ func (c *Client) status() (*Status, error) {
return root.Status, nil
}

// HttpGet sends a GET request to the Pilosa server.
// HttpGet sends an HTTP request to the Pilosa server.
// **NOTE**: This function is experimental and may be removed in later revisions.
func (c *Client) HttpGet(path string, data []byte, headers map[string]string) (*http.Response, []byte, error) {
if path == "" {
return nil, nil, errors.New("Path is required for GET request")
}
return c.httpRequest("GET", path, data, headers, errorCheckedResponse)
}

// HttpPost sends a POST request to the Pilosa server.
// **NOTE**: This function is experimental and may be removed in later revisions.
func (c *Client) HttpPost(path string, data []byte, headers map[string]string) (*http.Response, []byte, error) {
if path == "" {
return nil, nil, errors.New("Path is required for POST request")
}
return c.httpRequest("POST", path, data, headers, errorCheckedResponse)
}

// HttpDelete sends a DELETE request to the Pilosa server.
// **NOTE**: This function is experimental and may be removed in later revisions.
func (c *Client) HttpDelete(path string, data []byte, headers map[string]string) (*http.Response, []byte, error) {
if path == "" {
return nil, nil, errors.New("Path is required for DELETE request")
}
return c.httpRequest("DELETE", path, data, headers, errorCheckedResponse)
func (c *Client) HttpRequest(method string, path string, data []byte, headers map[string]string) (*http.Response, []byte, error) {
return c.httpRequest(method, path, data, headers, rawResponse)
}

// httpRequest makes a request to the cluster - use this when you want the
// client to choose a host, and it doesn't matter if the request goes to a
// specific host
func (c *Client) httpRequest(method string, path string, data []byte, headers map[string]string, returnResponse returnClientInfo) (*http.Response, []byte, error) {
if path == "" {
return nil, nil, errors.New("Path is required for HTTP request")
}
if data == nil {
data = []byte{}
}
Expand Down
25 changes: 3 additions & 22 deletions client_it_test.go
Expand Up @@ -38,7 +38,6 @@ import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -295,7 +294,7 @@ func TestTopNReturns(t *testing.T) {
)
client.Query(qry, nil)
// XXX: The following is required to make this test pass. See: https://github.com/pilosa/pilosa/issues/625
client.HttpPost("/recalculate-caches", nil, nil)
client.HttpRequest("POST", "/recalculate-caches", nil, nil)
response, err := client.Query(frame.TopN(2), nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1373,17 +1372,7 @@ func TestStatusToNodeSlicesForIndex(t *testing.T) {

func TestHttpRequest(t *testing.T) {
client := getClient()
reqData := []byte("")
framePath := fmt.Sprintf("/index/%s/frame/%s", index.Name(), "http-test-frame")
_, _, err := client.HttpPost(framePath, reqData, nil)
if err != nil {
t.Fatal(err)
}
_, _, err = client.HttpGet("/status", nil, nil)
if err != nil {
t.Fatal(err)
}
_, _, err = client.HttpDelete(framePath, nil, nil)
_, _, err := client.HttpRequest("GET", "/status", nil, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1393,18 +1382,10 @@ func TestHttpRequestWithoutPathFails(t *testing.T) {
client := getClient()
data := []byte("some data")
headers := map[string]string{"Foo": "Bar"}
_, _, err := client.HttpPost("", data, headers)
_, _, err := client.HttpRequest("GET", "", data, headers)
if err == nil {
t.Fatalf("HttpPostRequest without path should fail")
}
_, _, err = client.HttpGet("", data, headers)
if err == nil {
t.Fatalf("HttpGetRequest without path should fail")
}
_, _, err = client.HttpDelete("", data, headers)
if err == nil {
t.Fatalf("HttpDeleteRequest without path should fail")
}
}

func getClient() *Client {
Expand Down

0 comments on commit 7a1f308

Please sign in to comment.