1
10
11 package requests
12
13 import (
14 "bytes"
15 "fmt"
16 "io"
17 "net/http"
18
19 "gitlab.hexacode.org/go-libs/hctypes"
20 )
21
22
23 type Input struct {
24 Method string
25 URL string
26 URLParams hctypes.Dict
27 Headers hctypes.Dict
28 Body hctypes.Buffer
29 }
30
31
32 type Http struct {
33 Headers hctypes.Dict
34 }
35
36
37 type Output struct {
38 Status int
39 StatusText string
40 Headers hctypes.Dict
41 Body hctypes.Buffer
42 }
43
44
45 var default_headers = hctypes.Dict{
46 "User-Agent": hctypes.String("Requests/1.0 (go-requests)"),
47 "Accept": hctypes.String("*/*"),
48 "Accept-Language": hctypes.String("en-US,en;q=0.5"),
49 "DNT": hctypes.String("1"),
50 }
51
52 func base(in *Input) (*Output, error) {
53 url := in.URL
54 method := in.Method
55 if in.Body == nil {
56 in.Body = hctypes.Buffer{}
57 }
58
59 body := bytes.NewReader(in.Body)
60
61 if in.URLParams != nil {
62 url = fmt.Sprintf("%s?%s", url, in.URLParams.Copy().ToQueryString().Encode())
63 }
64
65 req, err := http.NewRequest(method, url, body)
66 if err != nil {
67 return nil, err
68 }
69
70 if default_headers == nil {
71 default_headers = hctypes.Dict{}
72 }
73
74 if in.Headers == nil {
75 in.Headers = hctypes.Dict{}
76 }
77
78 for k, v := range default_headers.Copy() {
79 val, ok := v.(hctypes.String)
80 if ok {
81 req.Header.Set(k, string(val))
82 }
83 }
84
85 for k, v := range in.Headers.Copy() {
86 val, ok := v.(hctypes.String)
87 if ok {
88 req.Header.Set(k, string(val))
89 }
90 }
91
92 client := &http.Client{}
93 resp, err := client.Do(req)
94 if err != nil {
95 return nil, err
96 }
97 defer resp.Body.Close()
98
99 bytes, err := io.ReadAll(resp.Body)
100 if err != nil {
101 return nil, err
102 }
103
104 out_headers := hctypes.Dict{}
105
106 for k, v := range resp.Header {
107 out_headers[k] = hctypes.String(v[0])
108 }
109
110 return &Output{
111 Status: resp.StatusCode,
112 StatusText: StatusText(resp.StatusCode),
113 Headers: out_headers,
114 Body: hctypes.Buffer(bytes),
115 }, nil
116 }
117
118
119
120
121 func SetDefaultHeaders(headers hctypes.Dict) {
122 if headers == nil {
123 default_headers = hctypes.Dict{}
124 } else {
125 default_headers = headers
126 }
127 }
128
129
130
131
132 func Request(in *Input) (*Output, error) {
133 return base(in)
134 }
135
136
137
138
139
140 func NewHttp(headers hctypes.Dict) *Http {
141 return &Http{
142 Headers: headers,
143 }
144 }
145
146
147
148
149
150
151
152
153 func (http *Http) Request(method string, url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
154 in := &Input{
155 Method: method,
156 URL: url,
157 URLParams: url_param,
158 Headers: http.Headers,
159 Body: body,
160 }
161
162 return base(in)
163 }
164
165
166
167
168
169
170
171 func (http *Http) Get(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
172 return http.Request("GET", url, url_param, body)
173 }
174
175
176
177
178
179
180
181 func (http *Http) Post(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
182 return http.Request("POST", url, url_param, body)
183 }
184
185
186
187
188
189
190
191 func (http *Http) Put(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
192 return http.Request("PUT", url, url_param, body)
193 }
194
195
196
197
198
199
200
201 func (http *Http) Delete(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
202 return http.Request("DELETE", url, url_param, body)
203 }
204
205
206
207
208
209
210
211 func (http *Http) Patch(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
212 return http.Request("PATCH", url, url_param, body)
213 }
214
215
216
217
218
219
220
221 func (http *Http) Options(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
222 return http.Request("OPTIONS", url, url_param, body)
223 }
224
225
226
227
228
229
230
231 func (http *Http) Head(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
232 return http.Request("HEAD", url, url_param, body)
233 }
234
235
236
237
238
239
240
241 func (http *Http) Trace(url string, url_param hctypes.Dict, body hctypes.Buffer) (*Output, error) {
242 return http.Request("TRACE", url, url_param, body)
243 }
244
View as plain text