-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
126 lines (105 loc) · 2.93 KB
/
client.go
File metadata and controls
126 lines (105 loc) · 2.93 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
package httpclient
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/cookiejar"
"time"
"golang.org/x/net/publicsuffix"
)
type Client struct {
client *http.Client
baseURL string
headers map[string]string
}
type Option func(*Client)
// WithTimeout sets custom timeout
func WithTimeout(timeout time.Duration) Option {
return func(c *Client) {
c.client.Timeout = timeout
}
}
// WithHeader adds custom header
func WithHeader(key, value string) Option {
return func(c *Client) {
c.headers[key] = value
}
}
// WithAuth enables cookie handling for authentication
func WithAuth() Option {
return func(c *Client) {
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
// In practice this error is very unlikely, but we should handle it gracefully
jar, _ = cookiejar.New(nil)
}
c.client.Jar = jar
}
}
// RequestOption defines per-request configuration
type RequestOption func(*http.Request)
// WithRequestHeader adds a header for a single request
func WithRequestHeader(key, value string) RequestOption {
return func(req *http.Request) {
req.Header.Set(key, value)
}
}
// NewClient creates a new HTTP client with default configurations
func NewClient(baseURL string, opts ...Option) *Client {
c := &Client{
client: &http.Client{
Timeout: 30 * time.Second,
},
baseURL: baseURL,
headers: map[string]string{
"Content-Type": "application/json",
},
}
for _, opt := range opts {
opt(c)
}
return c
}
// doRequest performs the HTTP request
func (c *Client) doRequest(ctx context.Context, method, path string, body interface{}, opts ...RequestOption) (*http.Response, error) {
var reqBody bytes.Buffer
if body != nil {
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
reqBody.Write(jsonBody)
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, &reqBody)
if err != nil {
return nil, err
}
// Set default headers
for k, v := range c.headers {
req.Header.Set(k, v)
}
// Apply per-request options
for _, opt := range opts {
opt(req)
}
return c.client.Do(req)
}
// Get performs a GET request
func (c *Client) Get(ctx context.Context, path string, opts ...RequestOption) (*http.Response, error) {
return c.doRequest(ctx, http.MethodGet, path, nil, opts...)
}
// Post performs a POST request
func (c *Client) Post(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*http.Response, error) {
return c.doRequest(ctx, http.MethodPost, path, body, opts...)
}
// Put performs a PUT request
func (c *Client) Put(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*http.Response, error) {
return c.doRequest(ctx, http.MethodPut, path, body, opts...)
}
// Delete performs a DELETE request
func (c *Client) Delete(ctx context.Context, path string, opts ...RequestOption) (*http.Response, error) {
return c.doRequest(ctx, http.MethodDelete, path, nil, opts...)
}