1
9
10 package strapi
11
12 import (
13 "errors"
14 "fmt"
15 "io"
16 "net/http"
17
18 "gitlab.hexacode.org/go-libs/hctypes"
19 )
20
21 func (strapi *StrapiClassic) base(method, url string, body io.Reader) (int, hctypes.Dict, error) {
22 req, err := http.NewRequest(method, url, body)
23 if err != nil {
24 return 500, hctypes.Dict{
25 "error": hctypes.Dict{
26 "message": "Internal server error",
27 "status": 500,
28 },
29 }, err
30 }
31
32 req.Header.Set("Accept", "*/*")
33 req.Header.Set("Content-Type", "application/json")
34 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token))
35
36 client := &http.Client{}
37 resp, err := client.Do(req)
38 if err != nil {
39 return 500, hctypes.Dict{
40 "error": hctypes.Dict{
41 "message": "Internal server error",
42 "status": 500,
43 },
44 }, err
45 }
46 defer resp.Body.Close()
47
48 bytes, err := io.ReadAll(resp.Body)
49 if err != nil {
50 return 500, hctypes.Dict{
51 "error": hctypes.Dict{
52 "message": "Internal server error",
53 "status": 500,
54 },
55 }, err
56 }
57
58 data := hctypes.NewDictFromJson(hctypes.Buffer(bytes))
59 return resp.StatusCode, data, nil
60 }
61
62 func (strapi *StrapiClassic) baseBuffer(method, url string, body io.Reader) (int, hctypes.Buffer, error) {
63 req, err := http.NewRequest(method, url, body)
64 if err != nil {
65 return 500, hctypes.Dict{
66 "error": hctypes.Dict{
67 "message": "Internal server error",
68 "status": 500,
69 },
70 }.ToJson(), err
71 }
72
73 req.Header.Set("Accept", "*/*")
74 req.Header.Set("Content-Type", "application/json")
75 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token))
76
77 client := &http.Client{}
78 resp, err := client.Do(req)
79 if err != nil {
80 return 500, hctypes.Dict{
81 "error": hctypes.Dict{
82 "message": "Internal server error",
83 "status": 500,
84 },
85 }.ToJson(), err
86 }
87 defer resp.Body.Close()
88
89 bytes, err := io.ReadAll(resp.Body)
90 if err != nil {
91 return 500, hctypes.Dict{
92 "error": hctypes.Dict{
93 "message": "Internal server error",
94 "status": 500,
95 },
96 }.ToJson(), err
97 }
98 return resp.StatusCode, hctypes.Buffer(bytes), nil
99 }
100
101 func (strapi *StrapiClassic) baseList(method, url string, body io.Reader) (int, hctypes.List, error) {
102 req, err := http.NewRequest(method, url, body)
103 if err != nil {
104 return 500, hctypes.List{
105 hctypes.Dict{
106 "error": hctypes.Dict{
107 "message": "Internal server error",
108 "status": 500,
109 },
110 },
111 }, err
112 }
113
114 req.Header.Set("Accept", "*/*")
115 req.Header.Set("Content-Type", "application/json")
116 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token))
117
118 client := &http.Client{}
119 resp, err := client.Do(req)
120 if err != nil {
121 return 500, hctypes.List{
122 hctypes.Dict{
123 "error": hctypes.Dict{
124 "message": "Internal server error",
125 "status": 500,
126 },
127 },
128 }, err
129 }
130 defer resp.Body.Close()
131
132 bytes, err := io.ReadAll(resp.Body)
133 if err != nil {
134 return 500, hctypes.List{
135 hctypes.Dict{
136 "error": hctypes.Dict{
137 "message": "Internal server error",
138 "status": 500,
139 },
140 },
141 }, err
142 }
143
144 data := hctypes.NewListFromJson(hctypes.Buffer(bytes))
145 return resp.StatusCode, data, nil
146 }
147
148 func (strapi *StrapiClassic) basePublic(method, url string, body io.Reader) (int, hctypes.Dict, error) {
149 req, err := http.NewRequest(method, url, body)
150 if err != nil {
151 return 500, hctypes.Dict{
152 "error": hctypes.Dict{
153 "message": "Internal server error",
154 "status": 500,
155 },
156 }, err
157 }
158
159 req.Header.Set("Accept", "*/*")
160 req.Header.Set("Content-Type", "application/json")
161
162 client := &http.Client{}
163 resp, err := client.Do(req)
164 if err != nil {
165 return 500, hctypes.Dict{
166 "error": hctypes.Dict{
167 "message": "Internal server error",
168 "status": 500,
169 },
170 }, err
171 }
172 defer resp.Body.Close()
173
174 bytes, err := io.ReadAll(resp.Body)
175 if err != nil {
176 return 500, hctypes.Dict{
177 "error": hctypes.Dict{
178 "message": "Internal server error",
179 "status": 500,
180 },
181 }, err
182 }
183
184 data := hctypes.NewDictFromJson(hctypes.Buffer(bytes))
185 return resp.StatusCode, data, nil
186 }
187
188 func (strapi *StrapiClassic) basePublicBuffer(method, url string, body io.Reader) (int, hctypes.Buffer, error) {
189 req, err := http.NewRequest(method, url, body)
190 if err != nil {
191 return 500, hctypes.Dict{
192 "error": hctypes.Dict{
193 "message": "Internal server error",
194 "status": 500,
195 },
196 }.ToJson(), err
197 }
198
199 req.Header.Set("Accept", "*/*")
200 req.Header.Set("Content-Type", "application/json")
201
202 client := &http.Client{}
203 resp, err := client.Do(req)
204 if err != nil {
205 return 500, hctypes.Dict{
206 "error": hctypes.Dict{
207 "message": "Internal server error",
208 "status": 500,
209 },
210 }.ToJson(), err
211 }
212 defer resp.Body.Close()
213
214 bytes, err := io.ReadAll(resp.Body)
215 if err != nil {
216 return 500, hctypes.Dict{
217 "error": hctypes.Dict{
218 "message": "Internal server error",
219 "status": 500,
220 },
221 }.ToJson(), err
222 }
223
224 return resp.StatusCode, hctypes.Buffer(bytes), nil
225 }
226
227 func (strapi *StrapiClassic) basePublicList(method, url string, body io.Reader) (int, hctypes.List, error) {
228 req, err := http.NewRequest(method, url, body)
229 if err != nil {
230 return 500, hctypes.List{
231 hctypes.Dict{
232 "error": hctypes.Dict{
233 "message": "Internal server error",
234 "status": 500,
235 },
236 },
237 }, err
238 }
239
240 req.Header.Set("Accept", "*/*")
241 req.Header.Set("Content-Type", "application/json")
242
243 client := &http.Client{}
244 resp, err := client.Do(req)
245 if err != nil {
246 return 500, hctypes.List{
247 hctypes.Dict{
248 "error": hctypes.Dict{
249 "message": "Internal server error",
250 "status": 500,
251 },
252 },
253 }, err
254 }
255 defer resp.Body.Close()
256
257 bytes, err := io.ReadAll(resp.Body)
258 if err != nil {
259 return 500, hctypes.List{
260 hctypes.Dict{
261 "error": hctypes.Dict{
262 "message": "Internal server error",
263 "status": 500,
264 },
265 },
266 }, err
267 }
268
269 data := hctypes.NewListFromJson(hctypes.Buffer(bytes))
270 return resp.StatusCode, data, nil
271 }
272
273 func (strapi *StrapiClassic) baseCustomHeader(method, url string, headers hctypes.Dict, body io.Reader) (int, hctypes.Dict, error) {
274 req, err := http.NewRequest(method, url, body)
275 if err != nil {
276 return 500, hctypes.Dict{
277 "error": hctypes.Dict{
278 "message": "Internal server error",
279 "status": 500,
280 },
281 }, err
282 }
283
284 req.Header.Set("Accept", "*/*")
285 req.Header.Set("Content-Type", "application/json")
286 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token))
287
288 for key, _ := range headers {
289 value, ok := headers.GetString(key)
290 if !ok {
291 return 500, hctypes.Dict{
292 "error": hctypes.Dict{
293 "message": "Internal server error",
294 "status": 500,
295 },
296 }, errors.New("header value must string")
297 }
298
299 req.Header.Set(key, string(value))
300 }
301
302 client := &http.Client{}
303 resp, err := client.Do(req)
304 if err != nil {
305 return 500, hctypes.Dict{
306 "error": hctypes.Dict{
307 "message": "Internal server error",
308 "status": 500,
309 },
310 }, err
311 }
312 defer resp.Body.Close()
313
314 bytes, err := io.ReadAll(resp.Body)
315 if err != nil {
316 return 500, hctypes.Dict{
317 "error": hctypes.Dict{
318 "message": "Internal server error",
319 "status": 500,
320 },
321 }, err
322 }
323
324 data := hctypes.NewDictFromJson(hctypes.Buffer(bytes))
325 return resp.StatusCode, data, nil
326 }
327
328 func (strapi *StrapiClassic) baseCustomHeaderList(method, url string, headers hctypes.Dict, body io.Reader) (int, hctypes.List, error) {
329 req, err := http.NewRequest(method, url, body)
330 if err != nil {
331 return 500, hctypes.List{
332 hctypes.Dict{
333 "error": hctypes.Dict{
334 "message": "Internal server error",
335 "status": 500,
336 },
337 },
338 }, err
339 }
340
341 req.Header.Set("Accept", "*/*")
342 req.Header.Set("Content-Type", "application/json")
343 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", strapi.token))
344
345 for key, _ := range headers {
346 value, ok := headers.GetString(key)
347 if !ok {
348 return 500, hctypes.List{
349 hctypes.Dict{
350 "error": hctypes.Dict{
351 "message": "Internal server error",
352 "status": 500,
353 },
354 },
355 }, errors.New("header value must string")
356 }
357
358 req.Header.Set(key, string(value))
359 }
360
361 client := &http.Client{}
362 resp, err := client.Do(req)
363 if err != nil {
364 return 500, hctypes.List{
365 hctypes.Dict{
366 "error": hctypes.Dict{
367 "message": "Internal server error",
368 "status": 500,
369 },
370 },
371 }, err
372 }
373 defer resp.Body.Close()
374
375 bytes, err := io.ReadAll(resp.Body)
376 if err != nil {
377 return 500, hctypes.List{
378 hctypes.Dict{
379 "error": hctypes.Dict{
380 "message": "Internal server error",
381 "status": 500,
382 },
383 },
384 }, err
385 }
386
387 data := hctypes.NewListFromJson(hctypes.Buffer(bytes))
388 return resp.StatusCode, data, nil
389 }
390
View as plain text