Skip to content

Commit

Permalink
chore(httpc): refactor inputs to eliminate required path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsteenb2 committed May 2, 2020
1 parent 95ad3c6 commit 35ed573
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions pkg/httpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
}
Expand Down Expand Up @@ -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()
}

0 comments on commit 35ed573

Please sign in to comment.