Skip to content

Commit

Permalink
Introduced new APIs for rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
bhanurp committed Jul 13, 2024
1 parent 75d0076 commit 0b94db6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
36 changes: 34 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rest

import "net/url"

// NewClient creates a new Client struct with the provided URL and headers.
// the default timeout is 30 seconds
// the default method is GET
Expand Down Expand Up @@ -42,7 +44,37 @@ func (c *Client) SetStrategy(s RequestMethod) {
c.Strategy = s
}

// SetTimeout sets a custom timeout for the HTTP client
func (c *Client) SetTimeout(timeout int) {
c.Timeout = timeout
}

// SetBody sets a custom body for the request
func (c *Client) SetBody(body []byte) {
c.Body = body
}

// Send delegates the request handling to the strategy
func (c *Client) Send(url string, body []byte, headers map[string]string, timeout int) (*Response, error) {
return c.Strategy.Do(url, body, headers, timeout)
func (c *Client) Send() (*Response, error) {
return c.Strategy.Do(c.URL, c.Body, c.Headers, c.Timeout)
}

// AddQueryParams adds query parameters to the given URL and returns the modified URL
func (c *Client) AddQueryParams(params map[string]string) (string, error) {
u, err := url.Parse(c.URL)
if err != nil {
return "", err
}

q := u.Query()
for key, value := range params {
q.Set(key, value)
}
u.RawQuery = q.Encode()
return u.String(), nil
}

// AddBasicAuth adds basic authentication headers to the request
func AddBasicAuth(username, password string, c *Client) {
c.basicAuth = &basicAuth{username: username, password: password}
}
2 changes: 1 addition & 1 deletion delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type DeleteRequest struct{}

func (d *DeleteRequest) Do(url string, headers map[string]string, timeout int) (*Response, error) {
func (d *DeleteRequest) Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error) {
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/bhanurp/rest

go 1.22.5
go 1.22.5
9 changes: 9 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package rest

func (r *Response) IsResponseOK() bool {
return r.StatusCode >= 200 && r.StatusCode < 300
}

func (r *Response) IsResponseError() bool {
return r.StatusCode >= 400
}
16 changes: 11 additions & 5 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package rest

type Client struct {
URL string
Headers map[string]string
Body []byte
Timeout int
Strategy RequestMethod
URL string
Headers map[string]string
Body []byte
Timeout int
Strategy RequestMethod
basicAuth *basicAuth
}

type Response struct {
Expand All @@ -14,6 +15,11 @@ type Response struct {
Error error
}

type basicAuth struct {
username string
password string
}

// RequestMethod defines the interface for HTTP request strategies.
type RequestMethod interface {
Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error)
Expand Down

0 comments on commit 0b94db6

Please sign in to comment.