-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
136 lines (111 loc) · 3.13 KB
/
request.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
package paystack
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/mitchellh/mapstructure"
)
const (
// User agent used when communicating with the Paystack API.
userAgent = "Mozilla/5.0 (Unknown; Linux) AppleWebKit/538.1 (KHTML, like Gecko) Chrome/v1.0.0 Safari/538.1"
)
func mapstruct(data interface{}, v interface{}) error {
config := &mapstructure.DecoderConfig{
Result: v,
TagName: "json",
WeaklyTypedInput: true,
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
err = decoder.Decode(data)
return err
}
func postResource(ctx context.Context, c *Client, url string, body interface{}, res interface{}) error {
reqUrl, _ := c.BaseUrl.Parse(url)
buf, err := json.Marshal(body)
if err != nil {
return err
}
payload := bytes.NewBuffer(buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqUrl.String(), payload)
if err != nil {
return err
}
return doReq(c, req, res)
}
func putResource(ctx context.Context, c *Client, url string, body interface{}, res interface{}) error {
reqUrl, _ := c.BaseUrl.Parse(url)
if body == nil {
body = `{}`
}
buf, err := json.Marshal(body)
if err != nil {
return err
}
payload := bytes.NewBuffer(buf)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, reqUrl.String(), payload)
if err != nil {
return err
}
return doReq(c, req, res)
}
func getResource(ctx context.Context, c *Client, url string, res interface{}) error {
reqUrl, _ := c.BaseUrl.Parse(url)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl.String(), nil)
if err != nil {
return err
}
return doReq(c, req, res)
}
func deleteResource(ctx context.Context, c *Client, url string, res interface{}) error {
reqUrl, _ := c.BaseUrl.Parse(url)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, reqUrl.String(), nil)
if err != nil {
return err
}
return doReq(c, req, res)
}
func doReq(c *Client, req *http.Request, res interface{}) error {
if req.Body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.APIKey))
req.Header.Set("User-Agent", userAgent)
resp, err := c.HttpClient.Do(req)
if err != nil {
return fmt.Errorf("error processing request - %+v", err)
}
err = parseAPIResponse(c, resp, res)
if err != nil {
return err
}
return nil
}
func parseAPIResponse(c *Client, resp *http.Response, resultPtr interface{}) error {
var response APIResponse
err := json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return fmt.Errorf("error while unmarshalling the response bytes %+v ", err)
}
if status, _ := response["status"].(bool); !status || resp.StatusCode >= 400 {
return newAPIError(resp, response)
}
// looking for a more betterway
if data, ok := response["data"]; ok {
switch t := response["data"].(type) {
case map[string]interface{}:
return mapstruct(data, resultPtr)
// i
default:
// if response is an array
_ = t
return mapstruct(response, resultPtr)
}
}
// if response data does not contain data key, map entire response to v
return mapstruct(response, resultPtr)
}