1
|
|
|
package mtnmomo |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"context" |
6
|
|
|
"encoding/json" |
7
|
|
|
"fmt" |
8
|
|
|
"io" |
9
|
|
|
"io/ioutil" |
10
|
|
|
"net/http" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
const ( |
14
|
|
|
subscriptionKeyHeaderKey = "Ocp-Apim-Subscription-Key" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
type service struct { |
18
|
|
|
client *Client |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// Client is the campay API client. |
22
|
|
|
// Do not instantiate this client with Client{}. Use the New method instead. |
23
|
|
|
type Client struct { |
24
|
|
|
httpClient *http.Client |
25
|
|
|
common service |
26
|
|
|
baseURL string |
27
|
|
|
subscriptionKey string |
28
|
|
|
|
29
|
|
|
APIUser *apiUserService |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// New creates and returns a new campay.Client from a slice of campay.ClientOption. |
33
|
|
|
func New(options ...Option) *Client { |
34
|
|
|
config := defaultClientConfig() |
35
|
|
|
|
36
|
|
|
for _, option := range options { |
37
|
|
|
option.apply(config) |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
client := &Client{ |
41
|
|
|
httpClient: config.httpClient, |
42
|
|
|
subscriptionKey: config.subscriptionKey, |
43
|
|
|
baseURL: config.baseURL, |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
client.common.client = client |
47
|
|
|
client.APIUser = (*apiUserService)(&client.common) |
48
|
|
|
return client |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// newRequest creates an API request. A relative URL can be provided in uri, |
52
|
|
|
// in which case it is resolved relative to the BaseURL of the Client. |
53
|
|
|
// URI's should always be specified without a preceding slash. |
54
|
|
|
func (client *Client) newRequest(ctx context.Context, method, uri string, body interface{}) (*http.Request, error) { |
55
|
|
|
var buf io.ReadWriter |
56
|
|
|
if body != nil { |
57
|
|
|
buf = &bytes.Buffer{} |
58
|
|
|
enc := json.NewEncoder(buf) |
59
|
|
|
enc.SetEscapeHTML(false) |
60
|
|
|
err := enc.Encode(body) |
61
|
|
|
if err != nil { |
62
|
|
|
return nil, err |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
req, err := http.NewRequestWithContext(ctx, method, client.baseURL+uri, buf) |
67
|
|
|
if err != nil { |
68
|
|
|
return nil, err |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
req.Header.Set("Content-Type", "application/json") |
72
|
|
|
req.Header.Set("Accept", "application/json") |
73
|
|
|
req.Header.Set(subscriptionKeyHeaderKey, client.subscriptionKey) |
74
|
|
|
|
75
|
|
|
return req, nil |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
// addURLParams adds urls parameters to an *http.Request |
80
|
|
|
func (client *Client) addURLParams(request *http.Request, params map[string]string) *http.Request { |
81
|
|
|
q := request.URL.Query() |
82
|
|
|
for key, value := range params { |
83
|
|
|
q.Add(key, value) |
84
|
|
|
} |
85
|
|
|
request.URL.RawQuery = q.Encode() |
86
|
|
|
return request |
87
|
|
|
} |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
// do carries out an HTTP request and returns a Response |
91
|
|
|
func (client *Client) do(req *http.Request) (*Response, error) { |
92
|
|
|
httpResponse, err := client.httpClient.Do(req) |
93
|
|
|
if err != nil { |
94
|
|
|
return nil, err |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
defer func() { _ = httpResponse.Body.Close() }() |
98
|
|
|
|
99
|
|
|
resp, err := client.newResponse(httpResponse) |
100
|
|
|
if err != nil { |
101
|
|
|
return resp, err |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
_, err = io.Copy(ioutil.Discard, httpResponse.Body) |
105
|
|
|
if err != nil { |
106
|
|
|
return resp, err |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return resp, nil |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// newResponse converts an *http.Response to *Response |
113
|
|
|
func (client *Client) newResponse(httpResponse *http.Response) (*Response, error) { |
114
|
|
|
if httpResponse == nil { |
115
|
|
|
return nil, fmt.Errorf("%T cannot be nil", httpResponse) |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
resp := new(Response) |
119
|
|
|
resp.HTTPResponse = httpResponse |
120
|
|
|
|
121
|
|
|
buf, err := ioutil.ReadAll(resp.HTTPResponse.Body) |
122
|
|
|
if err != nil { |
123
|
|
|
return nil, err |
124
|
|
|
} |
125
|
|
|
resp.Body = &buf |
126
|
|
|
|
127
|
|
|
return resp, resp.Error() |
128
|
|
|
} |
129
|
|
|
|