Skip to content

Commit 0b94db6

Browse files
committed
Introduced new APIs for rest client
1 parent 75d0076 commit 0b94db6

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

client.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package rest
22

3+
import "net/url"
4+
35
// NewClient creates a new Client struct with the provided URL and headers.
46
// the default timeout is 30 seconds
57
// the default method is GET
@@ -42,7 +44,37 @@ func (c *Client) SetStrategy(s RequestMethod) {
4244
c.Strategy = s
4345
}
4446

47+
// SetTimeout sets a custom timeout for the HTTP client
48+
func (c *Client) SetTimeout(timeout int) {
49+
c.Timeout = timeout
50+
}
51+
52+
// SetBody sets a custom body for the request
53+
func (c *Client) SetBody(body []byte) {
54+
c.Body = body
55+
}
56+
4557
// Send delegates the request handling to the strategy
46-
func (c *Client) Send(url string, body []byte, headers map[string]string, timeout int) (*Response, error) {
47-
return c.Strategy.Do(url, body, headers, timeout)
58+
func (c *Client) Send() (*Response, error) {
59+
return c.Strategy.Do(c.URL, c.Body, c.Headers, c.Timeout)
60+
}
61+
62+
// AddQueryParams adds query parameters to the given URL and returns the modified URL
63+
func (c *Client) AddQueryParams(params map[string]string) (string, error) {
64+
u, err := url.Parse(c.URL)
65+
if err != nil {
66+
return "", err
67+
}
68+
69+
q := u.Query()
70+
for key, value := range params {
71+
q.Set(key, value)
72+
}
73+
u.RawQuery = q.Encode()
74+
return u.String(), nil
75+
}
76+
77+
// AddBasicAuth adds basic authentication headers to the request
78+
func AddBasicAuth(username, password string, c *Client) {
79+
c.basicAuth = &basicAuth{username: username, password: password}
4880
}

delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
type DeleteRequest struct{}
88

9-
func (d *DeleteRequest) Do(url string, headers map[string]string, timeout int) (*Response, error) {
9+
func (d *DeleteRequest) Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error) {
1010
req, err := http.NewRequest(http.MethodDelete, url, nil)
1111
if err != nil {
1212
return nil, err

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/bhanurp/rest
22

3-
go 1.22.5
3+
go 1.22.5

response.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package rest
2+
3+
func (r *Response) IsResponseOK() bool {
4+
return r.StatusCode >= 200 && r.StatusCode < 300
5+
}
6+
7+
func (r *Response) IsResponseError() bool {
8+
return r.StatusCode >= 400
9+
}

types.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package rest
22

33
type Client struct {
4-
URL string
5-
Headers map[string]string
6-
Body []byte
7-
Timeout int
8-
Strategy RequestMethod
4+
URL string
5+
Headers map[string]string
6+
Body []byte
7+
Timeout int
8+
Strategy RequestMethod
9+
basicAuth *basicAuth
910
}
1011

1112
type Response struct {
@@ -14,6 +15,11 @@ type Response struct {
1415
Error error
1516
}
1617

18+
type basicAuth struct {
19+
username string
20+
password string
21+
}
22+
1723
// RequestMethod defines the interface for HTTP request strategies.
1824
type RequestMethod interface {
1925
Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error)

0 commit comments

Comments
 (0)