-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# rest | ||
A generic rest client for making go http requests | ||
A simple wrapper around http client to make restful requests easier. | ||
|
||
- An example usage for a post request | ||
```go | ||
client := DefaultClient() | ||
client.URL = "http://example.com" | ||
client.Headers = map[string]string{ | ||
"ContentType": "application/json" | ||
"Authorization":"Bearer token" | ||
} | ||
client.Body = bytes.NewReader([]byte(`{"key": "value"}`)) | ||
client.Strategy = PostRequest{} | ||
client.Send() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package client | ||
|
||
// NewClient creates a new Client struct with the provided URL and headers. | ||
// the default timeout is 30 seconds | ||
// the default method is GET | ||
// the default Body is an empty byte slice | ||
func NewClient(url string, headers map[string]string) *Client { | ||
return &Client{ | ||
URL: url, | ||
Headers: headers, | ||
Body: []byte{}, | ||
Timeout: 10, | ||
Strategy: &GetRequest{}, | ||
} | ||
} | ||
|
||
// DefaultClient creates a new Client struct with default values. | ||
// URL: empty string | ||
// headers: nil | ||
// timeout: 10 seconds | ||
// Body: empty byte slice | ||
// Strategy: nil | ||
func DefaultClient() *Client { | ||
return &Client{ | ||
URL: "", | ||
Headers: nil, | ||
Body: []byte{}, | ||
Timeout: 10, | ||
Strategy: nil, | ||
} | ||
} | ||
|
||
// CreateClientWithTimeout creates a new http.Client with the provided timeout | ||
func CreateClientWithTimeout(timeout int) *Client { | ||
client := DefaultClient() | ||
client.Timeout = timeout | ||
return client | ||
} | ||
|
||
// SetStrategy allows setting the current request strategy | ||
func (c *Client) SetStrategy(s RequestMethod) { | ||
c.Strategy = s | ||
} | ||
|
||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type DeleteRequest struct{} | ||
|
||
func (d *DeleteRequest) Do(url string, headers map[string]string, timeout int) (*Response, error) { | ||
req, err := http.NewRequest(http.MethodDelete, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for key, value := range headers { | ||
req.Header.Add(key, value) | ||
} | ||
|
||
client := createClient(timeout) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return handleResponse(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// GetRequest strategy | ||
type GetRequest struct{} | ||
|
||
// Do makes a generic GET request to the specified URL with the provided headers. | ||
// It returns the response body as a byte slice and any error encountered. | ||
func (g *GetRequest) Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error) { | ||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Add headers to the request | ||
for key, value := range headers { | ||
req.Header.Add(key, value) | ||
} | ||
|
||
client := createClient(timeout) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return handleResponse(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
) | ||
|
||
// PostRequest strategy | ||
type PostRequest struct{} | ||
|
||
// Do makes a generic POST request to the specified URL with the provided body and headers. | ||
// It returns a Response struct and any error encountered. | ||
func (p *PostRequest) Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error) { | ||
reqBody := bytes.NewBuffer(body) | ||
req, err := http.NewRequest(http.MethodPost, url, reqBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for key, value := range headers { | ||
req.Header.Add(key, value) | ||
} | ||
|
||
client := createClient(timeout) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return handleResponse(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
) | ||
|
||
// PutRequest strategy | ||
type PutRequest struct{} | ||
|
||
func (p *PutRequest) Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error) { | ||
reqBody := bytes.NewBuffer(body) | ||
req, err := http.NewRequest(http.MethodPut, url, reqBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for key, value := range headers { | ||
req.Header.Add(key, value) | ||
} | ||
|
||
client := createClient(timeout) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return handleResponse(resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package client | ||
|
||
type Client struct { | ||
URL string | ||
Headers map[string]string | ||
Body []byte | ||
Timeout int | ||
Strategy RequestMethod | ||
} | ||
|
||
type Response struct { | ||
StatusCode int | ||
Body []byte | ||
Error error | ||
} | ||
|
||
// RequestMethod defines the interface for HTTP request strategies. | ||
type RequestMethod interface { | ||
Do(url string, body []byte, headers map[string]string, timeout int) (*Response, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package client | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func createClient(timeout int) *http.Client { | ||
return &http.Client{ | ||
Timeout: time.Duration(timeout) * time.Second, | ||
} | ||
} | ||
|
||
func handleResponse(resp *http.Response) (*Response, error) { | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Response{ | ||
StatusCode: resp.StatusCode, | ||
Body: body, | ||
Error: nil, // Error handling could be more nuanced | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module rest | ||
|
||
go 1.22.5 |