Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bhanurp committed Jul 12, 2024
1 parent f2c1c4c commit bd54b32
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
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()
```
48 changes: 48 additions & 0 deletions client/client.go
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)
}
25 changes: 25 additions & 0 deletions client/delete.go
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)
}
29 changes: 29 additions & 0 deletions client/get.go
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)
}
30 changes: 30 additions & 0 deletions client/post.go
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)
}
28 changes: 28 additions & 0 deletions client/put.go
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)
}
20 changes: 20 additions & 0 deletions client/types.go
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)
}
26 changes: 26 additions & 0 deletions client/utils.go
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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module rest

go 1.22.5

0 comments on commit bd54b32

Please sign in to comment.