-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
80 lines (69 loc) · 2.08 KB
/
client.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
78
79
80
package sandbox
import (
"crypto/rsa"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
yotirequest "github.com/getyoti/yoti-go-sdk/v3/requests"
)
// Client is responsible for setting up test data in the sandbox instance. BaseURL is not required.
type Client struct {
// Client SDK ID. This can be found in the Yoti Hub after you have created and activated an application.
ClientSdkID string
// Private Key associated for your application, can be downloaded from the Yoti Hub.
Key *rsa.PrivateKey
// Base URL to use. This is not required, and a default will be set if not provided.
BaseURL string
// Mockable HTTP Client Interface
HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
}
func (client *Client) do(request *http.Request) (*http.Response, error) {
if client.HTTPClient != nil {
return client.HTTPClient.Do(request)
}
return http.DefaultClient.Do(request)
}
// SetupSharingProfile creates a user profile in the sandbox instance
func (client *Client) SetupSharingProfile(tokenRequest TokenRequest) (token string, err error) {
if client.BaseURL == "" {
if value, exists := os.LookupEnv("YOTI_API_URL"); exists && value != "" {
client.BaseURL = value
} else {
client.BaseURL = "https://api.yoti.com/sandbox/v1"
}
}
requestEndpoint := "/apps/" + client.ClientSdkID + "/tokens"
requestBody, err := json.Marshal(tokenRequest)
if err != nil {
return
}
request, err := (&yotirequest.SignedRequest{
Key: client.Key,
HTTPMethod: http.MethodPost,
BaseURL: client.BaseURL,
Endpoint: requestEndpoint,
Headers: yotirequest.JSONHeaders(),
Body: requestBody,
}).Request()
if err != nil {
return
}
response, err := client.do(request)
if err != nil {
return
}
if response.StatusCode != http.StatusCreated {
body, _ := ioutil.ReadAll(response.Body)
return "", fmt.Errorf("Sharing Profile not created (HTTP %d) %s", response.StatusCode, string(body))
}
responseStruct := struct {
Token string `json:"token"`
}{}
err = json.NewDecoder(response.Body).Decode(&responseStruct)
token = responseStruct.Token
return
}