/
http.go
178 lines (157 loc) · 4.29 KB
/
http.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//utils package is a collection of useful modules needed to develop Redump
package utils
import (
"bytes"
"context"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
type Api struct {
timeout int
httpclient *http.Client
}
// NewHttpClient is a function to create an HTTP client. When calling any of the methods,
// it is recommended to use the methods of the API structure generated by this function.
func NewHttpClient(timeout int, opts ...Option) *Api {
api := &Api{
timeout: timeout,
httpclient: http.DefaultClient,
}
for _, opt := range opts {
opt(api)
}
return api
}
type Option func(*Api)
// OptionHTTPClient is a function used to specify a custom client.
func OptionHTTPClient(c *http.Client) Option {
return func(api *Api) {
api.httpclient = c
}
}
func (api *Api) request(ctx context.Context, req *http.Request) (*http.Response, error) {
req = req.WithContext(ctx)
respCh := make(chan *http.Response)
errCh := make(chan error)
go func() {
resp, err := api.httpclient.Do(req)
if err != nil {
errCh <- err
return
}
respCh <- resp
}()
select {
case resp := <-respCh:
return resp, nil
case err := <-errCh:
return nil, err
case <-ctx.Done():
return nil, errors.New("HTTP request cancelled")
}
}
type HttpClientError struct {
StatusCode int
}
func (e *HttpClientError) Error() string {
return "HTTP Client error!: " + strconv.Itoa(e.StatusCode)
}
// Get function is a function to HTTP GET. The result can be obtained as a byte slice.
func (api *Api) Get(url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(api.timeout)*time.Millisecond)
defer cancel()
resp, err := api.request(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
return nil, &HttpClientError{StatusCode: resp.StatusCode}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// Post function is a function to HTTP POST. The result can be obtained as a byte slice.
func (api *Api) Post(url, contentType string, data []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(api.timeout)*time.Millisecond)
defer cancel()
req.Header.Set("Content-Type", contentType)
resp, err := api.request(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
return nil, &HttpClientError{StatusCode: resp.StatusCode}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// // Put function is a function to HTTP PUT. The result can be obtained as a byte slice.
func (api *Api) Put(url, contentType string, data []byte) error {
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", contentType)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(api.timeout)*time.Millisecond)
defer cancel()
resp, err := api.request(ctx, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
return &HttpClientError{StatusCode: resp.StatusCode}
}
return nil
}
// Delete function is a function to HTTP DELETE. The result can be obtained as a byte slice.
func (api *Api) Delete(url string) error {
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(api.timeout)*time.Millisecond)
defer cancel()
resp, err := api.request(ctx, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
return &HttpClientError{StatusCode: resp.StatusCode}
}
return nil
}
// NewProxyClient can create an http client with proxy settings by specifying the URL of the HTTP Proxy.
func NewProxyClient(proxyUrl string) (*http.Client, error) {
pu, err := url.Parse(proxyUrl)
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(pu),
},
}
return client, nil
}