-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.go
70 lines (61 loc) · 1.97 KB
/
sandbox.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
package gomomo
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// SandboxService handles communication with sandbox related methods of the Momo API
type SandboxService interface {
CreateSandboxUser(callbackHost string) (string, error)
GenerateSandboxUserAPIKey(referenceID string) (*APIKeyResponse, error)
}
// SandboxServiceOp handles communication with methods on Momo API to create Sandbox users
type SandboxServiceOp struct {
client *Client
}
// APIKeyResponse structure for returning API Key
type APIKeyResponse struct {
APIKey string `json:"apiKey"`
}
// CreateSandboxUser creates a user to test the Momo APU in a sandbox environment
func (c *SandboxServiceOp) CreateSandboxUser(callbackHost string) (string, error) {
ctx := context.Background()
body := map[string]string{
"providerCallbackHost": callbackHost,
}
req, err := c.client.NewRequest(ctx, http.MethodPost, "v1_0/apiuser", body)
if err != nil {
return "", err
}
response, err := c.client.Do(ctx, req)
if err != nil {
return "", err
}
if response.StatusCode != http.StatusCreated {
return "", fmt.Errorf("response code: %d with error %s", response.StatusCode, string(response.Body))
}
return response.ReferenceID, nil
}
// GenerateSandboxUserAPIKey is used to create an API key for an API user in the sandbox target environment
func (c *SandboxServiceOp) GenerateSandboxUserAPIKey(referenceID string) (*APIKeyResponse, error) {
urlStr := fmt.Sprintf("v1_0/apiuser/%s/apikey", referenceID)
ctx := context.Background()
req, err := c.client.NewRequest(ctx, http.MethodPost, urlStr, nil)
if err != nil {
return nil, err
}
response, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("response code: %d with error %s", response.StatusCode, string(response.Body))
}
keyResponse := &APIKeyResponse{}
err = json.Unmarshal(response.Body, keyResponse)
if err != nil {
return nil, err
}
return keyResponse, nil
}