-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
138 lines (123 loc) · 3.29 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package genesis
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"time"
)
type Genesis struct {
client *http.Client
ClientID string
ClientSecret string
Env string
Token string
}
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
Module string `json:"module"`
Body string `json:"body"`
}
type TokenRequest struct {
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
}
func NewClient(ClientID, ClientSecret, env string) *Genesis {
genesis := &Genesis{
client: &http.Client{Timeout: 60 * time.Second},
ClientID: ClientID,
ClientSecret: ClientSecret,
Env: env,
}
return genesis
}
func (genesis *Genesis) Request(token, method, action string, body []byte, out interface{}) (error, *Error) {
url := genesis.devProd()
endpoint := fmt.Sprintf("%s/%s", url, action)
req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(body))
if err != nil {
return err, nil
}
req.Header.Add("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
_, err, errBody := genesis.RequestMaster(req, &out)
return err, errBody
}
func (genesis *Genesis) RequestMaster(req *http.Request, out interface{}) ([]byte, error, *Error) {
res, err := genesis.client.Do(req)
if err != nil {
return nil, err, nil
}
bodyResponse, err := ioutil.ReadAll(res.Body)
fmt.Printf("bodyResponse %s\n", string(bodyResponse))
if res.StatusCode > 202 {
var errAPI Error
err = json.Unmarshal(bodyResponse, &errAPI)
if err != nil {
return bodyResponse, err, nil
}
errAPI.Body = string(bodyResponse)
return bodyResponse, nil, &errAPI
}
if out != nil {
err = json.Unmarshal(bodyResponse, out)
if err != nil {
return bodyResponse, err, nil
}
}
return bodyResponse, nil, nil
}
func (Genesis *Genesis) devProd() string {
if Genesis.Env == "production" {
return "https://api.genesispay.online"
}
return "https://api.sandbox.genesispay.online"
}
func (genesis *Genesis) RequestToken() (*TokenResponse, error) {
var tokenResponse TokenResponse
url := genesis.devProd()
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
writer.WriteField("client_secret", genesis.ClientSecret)
writer.WriteField("grant_type", "all")
writer.WriteField("client_id", genesis.ClientID)
err := writer.Close()
if err != nil {
return nil, err
}
endpoint := fmt.Sprintf("%s/%s", url, "customer/v1/connect/token")
req, err := http.NewRequest("POST", endpoint, payload)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
bodyResponse, err := ioutil.ReadAll(res.Body)
if res.StatusCode > 202 {
var errAPI Error
err = json.Unmarshal(bodyResponse, &errAPI)
if err != nil {
return nil, err
}
return nil, errors.New(string(bodyResponse))
}
err = json.Unmarshal(bodyResponse, &tokenResponse)
if err != nil {
return nil, err
}
genesis.Token = tokenResponse.AccessToken
return &tokenResponse, nil
}