From 28aa464770856745f9ea16008b893ff830dd8571 Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Fri, 1 May 2020 17:19:57 -0700 Subject: [PATCH] chore(httpc): refactor inputs to eliminate required path the base address should be enough to make a request. All requests are now valid without a path provided. This will be true as long as the address is valid. --- pkg/httpc/client.go | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/pkg/httpc/client.go b/pkg/httpc/client.go index a28123f73a..c4f94e1a3d 100644 --- a/pkg/httpc/client.go +++ b/pkg/httpc/client.go @@ -76,51 +76,51 @@ func New(opts ...ClientOptFn) (*Client, error) { } // Delete generates a DELETE request. -func (c *Client) Delete(urlPath string, rest ...string) *Req { - return c.Req(http.MethodDelete, nil, urlPath, rest...) +func (c *Client) Delete(urlPath ...string) *Req { + return c.Req(http.MethodDelete, nil, urlPath...) } // Get generates a GET request. -func (c *Client) Get(urlPath string, rest ...string) *Req { - return c.Req(http.MethodGet, nil, urlPath, rest...) +func (c *Client) Get(urlPath ...string) *Req { + return c.Req(http.MethodGet, nil, urlPath...) } // Patch generates a PATCH request. -func (c *Client) Patch(bFn BodyFn, urlPath string, rest ...string) *Req { - return c.Req(http.MethodPatch, bFn, urlPath, rest...) +func (c *Client) Patch(bFn BodyFn, urlPath ...string) *Req { + return c.Req(http.MethodPatch, bFn, urlPath...) } // PatchJSON generates a PATCH request. This is to be used with value or pointer to value type. // Providing a stream/reader will result in disappointment. -func (c *Client) PatchJSON(v interface{}, urlPath string, rest ...string) *Req { - return c.Patch(BodyJSON(v), urlPath, rest...) +func (c *Client) PatchJSON(v interface{}, urlPath ...string) *Req { + return c.Patch(BodyJSON(v), urlPath...) } // Post generates a POST request. -func (c *Client) Post(bFn BodyFn, urlPath string, rest ...string) *Req { - return c.Req(http.MethodPost, bFn, urlPath, rest...) +func (c *Client) Post(bFn BodyFn, urlPath ...string) *Req { + return c.Req(http.MethodPost, bFn, urlPath...) } // PostJSON generates a POST request and json encodes the body. This is to be // used with value or pointer to value type. Providing a stream/reader will result // in disappointment. -func (c *Client) PostJSON(v interface{}, urlPath string, rest ...string) *Req { - return c.Post(BodyJSON(v), urlPath, rest...) +func (c *Client) PostJSON(v interface{}, urlPath ...string) *Req { + return c.Post(BodyJSON(v), urlPath...) } // Put generates a PUT request. -func (c *Client) Put(bFn BodyFn, urlPath string, rest ...string) *Req { - return c.Req(http.MethodPut, bFn, urlPath, rest...) +func (c *Client) Put(bFn BodyFn, urlPath ...string) *Req { + return c.Req(http.MethodPut, bFn, urlPath...) } // PutJSON generates a PUT request. This is to be used with value or pointer to value type. // Providing a stream/reader will result in disappointment. -func (c *Client) PutJSON(v interface{}, urlPath string, rest ...string) *Req { - return c.Put(BodyJSON(v), urlPath, rest...) +func (c *Client) PutJSON(v interface{}, urlPath ...string) *Req { + return c.Put(BodyJSON(v), urlPath...) } // Req constructs a request. -func (c *Client) Req(method string, bFn BodyFn, urlPath string, rest ...string) *Req { +func (c *Client) Req(method string, bFn BodyFn, urlPath ...string) *Req { bodyF := BodyEmpty if bFn != nil { bodyF = bFn @@ -165,7 +165,7 @@ func (c *Client) Req(method string, bFn BodyFn, urlPath string, rest ...string) body = &buf } - req, err := http.NewRequest(method, c.buildURL(urlPath, rest...), body) + req, err := http.NewRequest(method, c.buildURL(urlPath...), body) if err != nil { return &Req{err: err} } @@ -205,11 +205,10 @@ func (c *Client) Clone(opts ...ClientOptFn) (*Client, error) { return New(append(existingOpts, opts...)...) } -func (c *Client) buildURL(urlPath string, rest ...string) string { +func (c *Client) buildURL(urlPath ...string) string { u := c.addr - u.Path = path.Join(u.Path, urlPath) - if len(rest) > 0 { - u.Path = path.Join(u.Path, path.Join(rest...)) + if len(urlPath) > 0 { + u.Path = path.Join(u.Path, path.Join(urlPath...)) } return u.String() }