aboutsummaryrefslogtreecommitdiff
path: root/lib/http
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-03-14 22:51:01 +0700
committerShulhan <ms@kilabit.info>2021-03-14 22:51:46 +0700
commit25fb633ee6ab446917af8dcd2f9b303e85a13c98 (patch)
tree7e4b3aff5593498e31270c241e5a749a54f69ca8 /lib/http
parente7552ad0189f761875bc1c2ca3dd716d43a01e0d (diff)
downloadpakakeh.go-25fb633ee6ab446917af8dcd2f9b303e85a13c98.tar.xz
all: refactoring http.Client methods signature
Previously, parameters to method Delete, Get, Post, PostForm, PostFormData, PostJSON, Put, and PutJSON are in the following order: (headers, path, params) This is sometimes confusing. To make it better and works with format of HTTP request header, METHOD PATH HEADERS PARAMS we move the path to the first parameter and headers as the second parameter, so the call to client methods would be (path, headers, params)
Diffstat (limited to 'lib/http')
-rw-r--r--lib/http/client.go16
-rw-r--r--lib/http/endpoint_example_test.go4
-rw-r--r--lib/http/endpoint_response_example_test.go6
-rw-r--r--lib/http/example_server_test.go2
4 files changed, 14 insertions, 14 deletions
diff --git a/lib/http/client.go b/lib/http/client.go
index 7a56b1ef..aeed005d 100644
--- a/lib/http/client.go
+++ b/lib/http/client.go
@@ -99,7 +99,7 @@ func NewClient(serverURL string, headers http.Header, insecure bool) (client *Cl
// parameters.
// On success, it will return the uncompressed response body.
//
-func (client *Client) Delete(headers http.Header, path string, params url.Values) (
+func (client *Client) Delete(path string, headers http.Header, params url.Values) (
httpRes *http.Response, resBody []byte, err error,
) {
if params != nil {
@@ -114,7 +114,7 @@ func (client *Client) Delete(headers http.Header, path string, params url.Values
// parameters.
// On success, it will return the uncompressed response body.
//
-func (client *Client) Get(headers http.Header, path string, params url.Values) (
+func (client *Client) Get(path string, headers http.Header, params url.Values) (
httpRes *http.Response, resBody []byte, err error,
) {
if params != nil {
@@ -128,7 +128,7 @@ func (client *Client) Get(headers http.Header, path string, params url.Values) (
// Post send the POST request to path without setting "Content-Type".
// If the params is not nil, it will send as query parameters in the path.
//
-func (client *Client) Post(headers http.Header, path string, params url.Values) (
+func (client *Client) Post(path string, headers http.Header, params url.Values) (
httpRes *http.Response, resBody []byte, err error,
) {
if params != nil {
@@ -142,7 +142,7 @@ func (client *Client) Post(headers http.Header, path string, params url.Values)
// PostForm send the POST request to path using
// "application/x-www-form-urlencoded".
//
-func (client *Client) PostForm(headers http.Header, path string, params url.Values) (
+func (client *Client) PostForm(path string, headers http.Header, params url.Values) (
httpRes *http.Response, resBody []byte, err error,
) {
body := strings.NewReader(params.Encode())
@@ -155,8 +155,8 @@ func (client *Client) PostForm(headers http.Header, path string, params url.Valu
// using "multipart/form-data".
//
func (client *Client) PostFormData(
- headers http.Header,
path string,
+ headers http.Header,
params map[string][]byte,
) (
httpRes *http.Response, resBody []byte, err error,
@@ -175,7 +175,7 @@ func (client *Client) PostFormData(
// PostJSON send the POST request with content type set to "application/json"
// and params encoded automatically to JSON.
//
-func (client *Client) PostJSON(headers http.Header, path string, params interface{}) (
+func (client *Client) PostJSON(path string, headers http.Header, params interface{}) (
httpRes *http.Response, resBody []byte, err error,
) {
paramsJSON, err := json.Marshal(params)
@@ -192,7 +192,7 @@ func (client *Client) PostJSON(headers http.Header, path string, params interfac
// Put send the HTTP PUT request with specific content type and body to
// specific path at the server.
//
-func (client *Client) Put(headers http.Header, path, contentType string, body []byte) (
+func (client *Client) Put(path, contentType string, headers http.Header, body []byte) (
*http.Response, []byte, error,
) {
bodyReader := bytes.NewReader(body)
@@ -203,7 +203,7 @@ func (client *Client) Put(headers http.Header, path, contentType string, body []
// PutJSON send the PUT request with content type set to "application/json"
// and params encoded automatically to JSON.
//
-func (client *Client) PutJSON(headers http.Header, path string, params interface{}) (
+func (client *Client) PutJSON(path string, headers http.Header, params interface{}) (
httpRes *http.Response, resBody []byte, err error,
) {
paramsJSON, err := json.Marshal(params)
diff --git a/lib/http/endpoint_example_test.go b/lib/http/endpoint_example_test.go
index ef76f95b..5abe9a16 100644
--- a/lib/http/endpoint_example_test.go
+++ b/lib/http/endpoint_example_test.go
@@ -51,11 +51,11 @@ func ExampleEndpoint_errorHandler() {
params := url.Values{}
params.Set("error", "400:error with status code")
- httpres, resbody, _ := client.Get(nil, "/", params)
+ httpres, resbody, _ := client.Get("/", nil, params)
fmt.Printf("%d: %s\n", httpres.StatusCode, resbody)
params.Set("error", "error without status code")
- httpres, resbody, _ = client.Get(nil, "/", params)
+ httpres, resbody, _ = client.Get("/", nil, params)
fmt.Printf("%d: %s\n", httpres.StatusCode, resbody)
// Output:
diff --git a/lib/http/endpoint_response_example_test.go b/lib/http/endpoint_response_example_test.go
index 3c4fa33c..65780dbb 100644
--- a/lib/http/endpoint_response_example_test.go
+++ b/lib/http/endpoint_response_example_test.go
@@ -67,7 +67,7 @@ func ExampleEndpointResponse() {
params := url.Values{}
// Test call endpoint without "id" parameter.
- _, resBody, err := cl.Get(nil, "/", params)
+ _, resBody, err := cl.Get("/", nil, params)
if err != nil {
log.Fatal(err)
}
@@ -76,7 +76,7 @@ func ExampleEndpointResponse() {
// Test call endpoint with "id" parameter set to "0", it should return
// HTTP status 500 with custom message.
params.Set("id", "0")
- _, resBody, err = cl.Get(nil, "/", params)
+ _, resBody, err = cl.Get("/", nil, params)
if err != nil {
log.Fatal(err)
}
@@ -84,7 +84,7 @@ func ExampleEndpointResponse() {
// Test with "id" parameter is set.
params.Set("id", "1000")
- _, resBody, err = cl.Get(nil, "/", params)
+ _, resBody, err = cl.Get("/", nil, params)
if err != nil {
log.Fatal(err)
}
diff --git a/lib/http/example_server_test.go b/lib/http/example_server_test.go
index 966d6b64..8ee7268a 100644
--- a/lib/http/example_server_test.go
+++ b/lib/http/example_server_test.go
@@ -58,7 +58,7 @@ func ExampleServer_customHTTPStatusCode() {
client := NewClient("http://127.0.0.1:8123", nil, false)
- httpRes, resBody, err := client.PostJSON(nil, epCustom.Path, nil)
+ httpRes, resBody, err := client.PostJSON(epCustom.Path, nil, nil)
if err != nil {
log.Fatal(err)
}