-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
119 lines (109 loc) · 2.79 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package tuxedo
import (
"bytes"
"io"
"net/http"
"time"
)
// Client represents an HTTP client with custom configurations.
//
// It provides methods to send requests, apply middlewares, and handle responses.
type Client struct {
httpClient *http.Client
}
// NewClient creates a new HTTP client with a specified timeout.
//
// Parameters:
// - timeout: Duration for request timeouts.
//
// Returns:
// - A new instance of Client.
func NewClient(timeout time.Duration) *Client {
return &Client{
httpClient: &http.Client{Timeout: timeout},
}
}
// execute sends an HTTP request with the given method and URL.
//
// It also applies headers, handles request bodies, and ensures proper response handling.
//
// Parameters:
// - method: HTTP method (GET, POST, etc.).
// - url: Target URL for the request.
//
// Returns:
// - *Response: A structured response containing status code, body, and headers.
// - error: Any error encountered during execution.
func (r *Request) execute(method, url string) (*Response, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(r.body))
if err != nil {
return nil, err
}
for key, value := range r.headers {
req.Header.Set(key, value)
}
if r.body != nil && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
if r.enableTrace {
// TODO: Add trace logs
}
resp, err := r.client.httpClient.Do(req)
if err != nil {
return nil, err
}
defer doClose(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return &Response{
StatusCode: resp.StatusCode,
Body: respBody,
Headers: resp.Header,
}, nil
}
// Get sends a GET request to the specified URL.
//
// Parameters:
// - url: Target URL for the request.
//
// Returns:
// - *Response: The HTTP response.
// - error: Any error encountered.
func (r *Request) Get(url string) (*Response, error) {
return r.execute(http.MethodGet, url)
}
// Post sends a POST request to the specified URL.
//
// Parameters:
// - url: Target URL for the request.
//
// Returns:
// - *Response: The HTTP response.
// - error: Any error encountered.
func (r *Request) Post(url string) (*Response, error) {
return r.execute(http.MethodPost, url)
}
// Put sends a PUT request to the specified URL.
//
// Parameters:
// - url: Target URL for the request.
//
// Returns:
// - *Response: The HTTP response.
// - error: Any error encountered.
func (r *Request) Put(url string) (*Response, error) {
return r.execute(http.MethodPut, url)
}
// Delete sends a DELETE request to the specified URL.
//
// Parameters:
// - url: Target URL for the request.
//
// Returns:
// - *Response: The HTTP response.
// - error: Any error encountered.
func (r *Request) Delete(url string) (*Response, error) {
return r.execute(http.MethodDelete, url)
}