From 0b94db6e149c92e175c99adfd4ddbdfb3cdb1815 Mon Sep 17 00:00:00 2001 From: bhanur Date: Sat, 13 Jul 2024 21:01:59 +0530 Subject: [PATCH] Introduced new APIs for rest client --- client.go | 36 ++++++++++++++++++++++++++++++++++-- delete.go | 2 +- go.mod | 2 +- response.go | 9 +++++++++ types.go | 16 +++++++++++----- 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 response.go diff --git a/client.go b/client.go index e71c45b..2c94407 100644 --- a/client.go +++ b/client.go @@ -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 @@ -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} } diff --git a/delete.go b/delete.go index ca5bf50..ef7b2ab 100644 --- a/delete.go +++ b/delete.go @@ -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 diff --git a/go.mod b/go.mod index 49313e2..508ba7b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/bhanurp/rest -go 1.22.5 +go 1.22.5 \ No newline at end of file diff --git a/response.go b/response.go new file mode 100644 index 0000000..82868eb --- /dev/null +++ b/response.go @@ -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 +} diff --git a/types.go b/types.go index 0a493fd..cd780b2 100644 --- a/types.go +++ b/types.go @@ -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 { @@ -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)