-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
136 lines (118 loc) · 3.67 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package go_requests
import (
"net"
"net/http"
"sync"
)
type Method string
// goHTTPClient is the default implementation of the Client interface
// it is used to make http requests
type goHTTPClient struct {
builder *builderImpl
client *http.Client
clientOnce sync.Once
queryParams QueryParams
}
func (c *goHTTPClient) QueryParams() QueryParams {
if c.queryParams == nil {
c.queryParams = NewQueryParams()
}
return c.queryParams
}
// Client is an interface for http client
type Client interface {
QueryParams() QueryParams
DisableTimeouts()
EnableTimeouts()
Headers() Headers
Get(url string, headers ...http.Header) (*Response, error)
Post(url string, body []byte, headers ...http.Header) (*Response, error)
Put(url string, body []byte, headers ...http.Header) (*Response, error)
Patch(url string, body []byte, headers ...http.Header) (*Response, error)
Delete(url string, body []byte, headers ...http.Header) (*Response, error)
Head(url string, body []byte, headers ...http.Header) (*Response, error)
}
func (c *goHTTPClient) Get(url string, headers ...http.Header) (*Response, error) {
response, err := c.do(http.MethodGet, url, getHeader(headers...), nil)
// restore timeout state to default in case it was disabled
if c.builder.Timeout.GetRequestTimeout() == 0 {
c.builder.Timeout = c.builder.Timeout.Enable()
}
if err != nil {
return nil, err
}
return response, nil
}
func (c *goHTTPClient) Post(url string, body []byte, headers ...http.Header) (*Response, error) {
response, err := c.do(http.MethodPost, url, getHeader(headers...), body)
if err != nil {
return nil, err
}
return response, nil
}
func (c *goHTTPClient) Put(url string, body []byte, headers ...http.Header) (*Response, error) {
response, err := c.do(http.MethodPut, url, getHeader(headers...), body)
if err != nil {
return nil, err
}
return response, nil
}
func (c *goHTTPClient) Delete(url string, body []byte, headers ...http.Header) (*Response, error) {
response, err := c.do(http.MethodDelete, url, getHeader(headers...), body)
if err != nil {
return nil, err
}
return response, nil
}
func (c *goHTTPClient) Patch(url string, body []byte, headers ...http.Header) (*Response, error) {
response, err := c.do(http.MethodPatch, url, getHeader(headers...), body)
if err != nil {
return nil, err
}
return response, nil
}
func (c *goHTTPClient) Head(url string, body []byte, headers ...http.Header) (*Response, error) {
response, err := c.do(http.MethodHead, url, getHeader(headers...), body)
if err != nil {
return nil, err
}
return response, nil
}
// DisableTimeouts disables the timeouts for the client requests
// Example:
//
// client.DisableTimeouts()
// response, err := client.Get("https://www.google.com", nil, nil)
// if err != nil {
// log.Fatal(err)
// }
func (c *goHTTPClient) DisableTimeouts() {
c.builder.Timeout = c.builder.Timeout.Disable()
}
func (c *goHTTPClient) EnableTimeouts() {
c.builder.Timeout = c.builder.Timeout.Enable()
}
// getClient returns the *http.client if exists or creates a new one and returns it.
func (c *goHTTPClient) getClient() *http.Client {
c.clientOnce.Do(func() {
if c.builder.cstClient != nil {
c.client = c.builder.cstClient
return
}
c.client = &http.Client{
Timeout: c.builder.Timeout.GetRequestTimeout(),
Transport: &http.Transport{
MaxIdleConnsPerHost: c.builder.Timeout.GetMaxIdleConnections(),
ResponseHeaderTimeout: c.builder.Timeout.GetResponseTimeout(),
DialContext: (&net.Dialer{
Timeout: c.builder.Timeout.GetRequestTimeout(),
}).DialContext,
},
}
})
return c.client
}
// Headers sets the headers for the client
func (c *goHTTPClient) Headers() Headers {
return c.builder.Headers()
}