-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
95 lines (87 loc) · 2.16 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package zignsec
// This is an interface to http://docs.zignsec.com/api/web-based/
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
const (
// APIHostBase is the production endpoint
APIHostBase = "https://api.zignsec.com/v2/eid"
// APIHostBaseTest is the test endpoint
APIHostBaseTest = "https://test.zignsec.com/v2/eid"
)
// Client is a Zignsec web-based client.
type Client struct {
APIHostBase string
APIKey string
}
// New create a new Client
func New(APIHostBase string, APIKey string) *Client {
c := new(Client)
c.APIHostBase = APIHostBase
c.APIKey = APIKey
return c
}
// Initiate a login or sign request
func (c *Client) Initiate(method string, config ZWInitConfig) (*ZWInitRespBody, error) {
url := c.APIHostBase + "/" + method
configB, err := json.Marshal(config)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(configB))
if err != nil {
return nil, err
}
setHeaders(req, c.APIKey, "application/json")
var httpClient http.Client
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error response body:", string(b))
return nil, err
}
resp.Body.Close()
var response ZWInitRespBody
err = json.Unmarshal(b, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// Verify a login or signature
func (c *Client) Verify(uuid string) (*ZWVerifyRespBody, error) {
url := c.APIHostBase + "/" + uuid
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
setHeaders(req, c.APIKey, "application/x-www-form-urlencoded")
var httpClient http.Client
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error response body:", string(b))
return nil, err
}
resp.Body.Close()
var response ZWVerifyRespBody
err = json.Unmarshal(b, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func setHeaders(req *http.Request, APIKey string, contentType string) {
req.Header.Add("Authorization", APIKey)
req.Header.Add("Content-Type", contentType)
}