-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
59 lines (47 loc) · 1.39 KB
/
utils.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
package daraja
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type httpHeaders = map[string]string
func getRequest[model interface{}](url string, headers httpHeaders) (*model, error) {
res := _makeRequest(url, http.MethodGet, headers, []byte{})
if res != nil {
defer res.Body.Close()
}
var response model
err := json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to decode json response: %v", err)
}
return &response, nil
}
func postRequest[resModel interface{}](url string, body []byte, headers httpHeaders) (*resModel, error) {
res := _makeRequest(url, http.MethodPost, headers, body)
defer res.Body.Close()
var response resModel
err := json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to decode json response: %v", err)
}
return &response, nil
}
func _makeRequest(url string, method string, headers httpHeaders, body []byte) *http.Response {
req, e := http.NewRequest(method, url, bytes.NewBuffer(body))
if e != nil {
panic("An internal error occurred when creating the request.")
}
req.Header.Set("Content-Type", "application/json")
for key, value := range headers {
req.Header.Set(key, value)
}
client := &http.Client{Timeout: 60 * time.Second}
res, err := client.Do(req)
if err != nil {
panic("An internal error occurred when trying to send a request.")
}
return res
}