generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiuser_service.go
77 lines (62 loc) · 2.2 KB
/
apiuser_service.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
package mtnmomo
import (
"context"
"encoding/json"
"net/http"
)
// apiUserService is the API client for the `/` endpoint
type apiUserService service
// CreateAPIUser Used to create an API user in the sandbox target environment.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser
func (service *apiUserService) CreateAPIUser(ctx context.Context, userID string, providerCallbackHost string) (string, *Response, error) {
payload := map[string]string{
"providerCallbackHost": providerCallbackHost,
}
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1_0/apiuser", payload)
if err != nil {
return userID, nil, err
}
service.client.addReferenceID(request, userID)
response, err := service.client.do(request)
return userID, response, err
}
// CreateAPIKey Used to create an API key for an API user in the sandbox target environment.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser-apikey
func (service *apiUserService) CreateAPIKey(ctx context.Context, userID string) (string, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1_0/apiuser/"+userID+"/apikey", nil)
if err != nil {
return "", nil, err
}
response, err := service.client.do(request)
if err != nil {
return "", response, err
}
body := struct {
APIKey string `json:"ApiKey"`
}{}
if err = json.Unmarshal(*response.Body, &body); err != nil {
return "", response, err
}
return body.APIKey, response, err
}
// Get Used to get API user information.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/get-v1_0-apiuser?
func (service *apiUserService) Get(ctx context.Context, userID string) (*APIUser, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodGet, "/v1_0/apiuser/"+userID, nil)
if err != nil {
return nil, nil, err
}
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
apiUser := new(APIUser)
if err = json.Unmarshal(*response.Body, apiUser); err != nil {
return nil, response, err
}
apiUser.UserID = userID
return apiUser, response, err
}