Test Setup Failed
Push — main ( 1663fe...ca36bd )
by Acho
01:59
created

mtnmomo.*apiUserService.CreateAPIKey   A

Complexity

Conditions 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nop 2
dl 0
loc 20
rs 9.75
c 0
b 0
f 0
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
	request.Header.Set("X-Reference-Id", userID)
25
26
	response, err := service.client.do(request)
27
	return userID, response, err
28
}
29
30
// CreateAPIKey Used to create an API user in the sandbox target environment.
31
//
32
// API Docs: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser
33
func (service *apiUserService) CreateAPIKey(ctx context.Context, userID string) (string, *Response, error) {
34
	request, err := service.client.newRequest(ctx, http.MethodPost, "/v1_0/apiuser/"+userID+"/apikey ", nil)
35
	if err != nil {
36
		return "", nil, err
37
	}
38
39
	response, err := service.client.do(request)
40
	if err != nil {
41
		return "", response, err
42
	}
43
44
	body := struct {
45
		APIKey string `json:"apiKey"`
46
	}{}
47
48
	if err = json.Unmarshal(*response.Body, &body); err != nil {
49
		return "", response, err
50
	}
51
52
	return body.APIKey, response, err
53
}
54