-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.go
146 lines (134 loc) · 3.71 KB
/
Network.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
package darajaAuth
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type networkPackage struct {
Payload interface{}
Endpoint string
Method string
Headers map[string]string
}
type networkResponse[T any] struct {
Body T
StatusCode int
}
func newRequestPackage(payload interface{}, endpoint string, method string, headers map[string]string, env Environment) *networkPackage {
var reqUrl = baseUrlSandbox
if env == ENVIROMENT_PRODUCTION {
reqUrl = baseUrlLive
}
reqUrl = reqUrl + endpoint
if method == http.MethodGet {
q := url.Values{}
var mapPayload map[string]interface{} = struct2Map(payload)
if len(mapPayload) > 0 {
for key, value := range mapPayload {
q.Add(key, value.(string))
}
if strings.Index(reqUrl, "?") == -1 {
reqUrl = reqUrl + "?" + q.Encode()
} else {
reqUrl = reqUrl + "&" + q.Encode()
}
reqUrl += q.Encode()
}
}
return &networkPackage{
Payload: payload,
Endpoint: reqUrl,
Method: method,
Headers: headers,
}
}
func (p *networkPackage) addHeader(key string, value string) {
if p.Headers == nil {
p.Headers = make(map[string]string)
}
p.Headers[key] = value
}
func newRequest[T any](pac *networkPackage) (*networkResponse[T], *ErrorResponse) {
netResponseHolder := &networkResponse[T]{}
client := &http.Client{}
var jsonDataBytes []byte
var httpReq *http.Request
if pac.Payload != nil {
jsonDataBytes, _ = json.Marshal(pac.Payload)
httpReq, _ = http.NewRequest(pac.Method, pac.Endpoint, strings.NewReader(string(jsonDataBytes)))
} else {
httpReq, _ = http.NewRequest(pac.Method, pac.Endpoint, nil)
}
for key, value := range pac.Headers {
httpReq.Header.Add(key, value)
}
if pac.Method == http.MethodPost {
httpReq.Header.Add("Content-Type", "application/json")
}
resp, err := client.Do(httpReq)
if err != nil {
return nil, &ErrorResponse{error: err}
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
netResponseHolder.StatusCode = resp.StatusCode
if netResponseHolder.StatusCode >= 400 {
if resp.Body != nil {
var errorResponse ErrorResponse
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, &ErrorResponse{error: err}
}
//errorResponse.Raw = string(body)
bodyString := string(body)
err = json.Unmarshal([]byte(bodyString), &errorResponse)
if err != nil {
if bodyString != "" {
return nil, &ErrorResponse{error: errors.New(resp.Status)}
}
return nil, &ErrorResponse{error: errors.New(resp.Status + " " + bodyString)}
}
if errorResponse.ErrorMessage != "" || errorResponse.ErrorCode != "" {
return nil, &errorResponse
}
errorResponse.Raw = string(body)
errorResponse.error = errors.New(http.StatusText(netResponseHolder.StatusCode))
return nil, &errorResponse
} else {
return nil, &ErrorResponse{error: errors.New(resp.Status)}
}
}
if err := json.NewDecoder(resp.Body).Decode(&netResponseHolder.Body); err != nil {
return nil, &ErrorResponse{error: err}
}
return netResponseHolder, nil
}
func performSecurePostRequest[T any](payload interface{}, endpoint string, d *Daraja) (*networkResponse[T], *ErrorResponse) {
var headers = make(map[string]string)
if d.authorization.AccessToken == "" {
_, err := d.Authorize()
if err != nil {
return nil, &ErrorResponse{error: err}
}
}
if time.Now().After(d.nextAuthTime) {
_, err := d.Authorize()
if err != nil {
return nil, &ErrorResponse{error: err}
}
}
headers["Authorization"] = "Bearer " + d.authorization.AccessToken
netPackage := newRequestPackage(payload, endpoint, http.MethodPost, headers, d.environment)
newResponse, err := newRequest[T](netPackage)
if err != nil {
return nil, err
}
return newResponse, nil
}