-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.go
More file actions
144 lines (122 loc) · 4 KB
/
Copy pathmethods.go
File metadata and controls
144 lines (122 loc) · 4 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package httpclient
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Post sends a POST request with the specified body and headers and decodes the response into the provided type T.
func Post[T any](ctx context.Context, client httpClient, url string, req any, headers map[string]string) (T, error) {
var resStruct T
resp, err := client.PostRequest(ctx, url, req, headers)
if err != nil {
return resStruct, fmt.Errorf("http POST request failed: %w", err)
}
if err = json.Unmarshal(resp, &resStruct); err != nil {
return resStruct, fmt.Errorf("failed to unmarshal response: %w", err)
}
return resStruct, nil
}
// Get sends a GET request with the specified headers and decodes the response into the provided type T.
func Get[T any](ctx context.Context, client httpClient, url string, headers map[string]string) (T, error) {
var resStruct T
resp, err := client.GetRequest(ctx, url, headers)
if err != nil {
return resStruct, fmt.Errorf("http GET request failed: %w", err)
}
if err = json.Unmarshal(resp, &resStruct); err != nil {
return resStruct, fmt.Errorf("failed to unmarshal response: %w", err)
}
return resStruct, nil
}
// WithTimeout sets a custom timeout for the HTTP client.
func WithTimeout(timeout time.Duration) Option {
return func(c *httpClient) {
c.client.Timeout = timeout
}
}
// WithLogging enables or disables request/response logging.
func WithLogging(enable bool) Option {
return func(c *httpClient) {
c.enableLogging = enable
}
}
// WithCustomHeader adds a custom header to the HTTP request.
func WithCustomHeader(key, value string) Option {
return func(c *httpClient) {
c.client.Transport = http.DefaultTransport
}
}
// WithRetries enables retries for failed requests and specifies retry count and delay.
func WithRetries(retries int, delay time.Duration) Option {
return func(c *httpClient) {
c.retries = retries
c.retryDelay = delay
}
}
// New creates a new HTTP client with the provided options.
func New(options ...Option) *httpClient {
client := &httpClient{
client: &http.Client{
Timeout: 30 * time.Second, // Default timeout
},
}
for _, opt := range options {
opt(client)
}
return client
}
// GetJSON is a helper function to simplify GET requests and decode JSON responses.
func (c *httpClient) GetJSON(ctx context.Context, url string, headers map[string]string, result any) error {
respBody, err := c.GetRequest(ctx, url, headers)
if err != nil {
return fmt.Errorf("failed to make GET request: %w", err)
}
// Decode JSON response into the provided result
if err := json.Unmarshal(respBody, result); err != nil {
return fmt.Errorf("failed to decode JSON response: %w", err)
}
return nil
}
// GetWithResponseTime sends a GET request and returns response time along with the data.
func (c *httpClient) GetWithResponseTime(ctx context.Context, url string, headers map[string]string) ([]byte, time.Duration, error) {
start := time.Now()
respBody, err := c.GetRequest(ctx, url, headers)
if err != nil {
return nil, 0, fmt.Errorf("failed to make GET request: %w", err)
}
duration := time.Since(start)
return respBody, duration, nil
}
// WithUserAgent sets a custom User-Agent header globally.
func WithUserAgent(userAgent string) Option {
return func(c *httpClient) {
c.userAgent = userAgent
}
}
// WithTransport allows setting a custom HTTP transport for fine-grained control.
func WithTransport(transport http.RoundTripper) Option {
return func(c *httpClient) {
c.transport = transport
}
}
// WithProxy sets the proxy URL for the HTTP client.
func WithProxy(proxyURL string) Option {
return func(c *httpClient) {
c.proxyURL = proxyURL
}
}
// WithTLSConfig allows configuring custom TLS settings, e.g., disabling SSL verification.
func WithTLSConfig(tlsConfig *tls.Config) Option {
return func(c *httpClient) {
c.tlsConfig = tlsConfig
}
}
// WithBodyLogging enables logging of request and response bodies (use with caution).
func WithBodyLogging(enable bool) Option {
return func(c *httpClient) {
c.enableBodyLogging = enable
}
}