1
|
|
|
package mtnmomo |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// apiUserService is the API client for the `/` endpoint |
10
|
|
|
type apiUserService service |
11
|
|
|
|
12
|
|
|
// CreateAPIUser Used to create an API user in the sandbox target environment. |
13
|
|
|
// |
14
|
|
|
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser |
15
|
|
|
func (service *apiUserService) CreateAPIUser(ctx context.Context, userID string, providerCallbackHost string) (string, *Response, error) { |
16
|
|
|
payload := map[string]string{ |
17
|
|
|
"providerCallbackHost": providerCallbackHost, |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1_0/apiuser", payload) |
21
|
|
|
if err != nil { |
22
|
|
|
return userID, nil, err |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
service.client.addReferenceID(request, userID) |
26
|
|
|
|
27
|
|
|
response, err := service.client.do(request) |
28
|
|
|
return userID, response, err |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// CreateAPIKey Used to create an API key for an API user in the sandbox target environment. |
32
|
|
|
// |
33
|
|
|
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser-apikey |
34
|
|
|
func (service *apiUserService) CreateAPIKey(ctx context.Context, userID string) (string, *Response, error) { |
35
|
|
|
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1_0/apiuser/"+userID+"/apikey", nil) |
36
|
|
|
if err != nil { |
37
|
|
|
return "", nil, err |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
response, err := service.client.do(request) |
41
|
|
|
if err != nil { |
42
|
|
|
return "", response, err |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
body := struct { |
46
|
|
|
APIKey string `json:"ApiKey"` |
47
|
|
|
}{} |
48
|
|
|
|
49
|
|
|
if err = json.Unmarshal(*response.Body, &body); err != nil { |
50
|
|
|
return "", response, err |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return body.APIKey, response, err |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Get Used to get API user information. |
57
|
|
|
// |
58
|
|
|
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/get-v1_0-apiuser? |
59
|
|
|
func (service *apiUserService) Get(ctx context.Context, userID string) (*APIUser, *Response, error) { |
60
|
|
|
request, err := service.client.newRequest(ctx, http.MethodGet, "/v1_0/apiuser/"+userID, nil) |
61
|
|
|
if err != nil { |
62
|
|
|
return nil, nil, err |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
response, err := service.client.do(request) |
66
|
|
|
if err != nil { |
67
|
|
|
return nil, response, err |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
apiUser := new(APIUser) |
71
|
|
|
if err = json.Unmarshal(*response.Body, apiUser); err != nil { |
72
|
|
|
return nil, response, err |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
apiUser.UserID = userID |
76
|
|
|
return apiUser, response, err |
77
|
|
|
} |
78
|
|
|
|