-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
176 lines (143 loc) · 4.16 KB
/
api.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package jenga
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
// Env is the environment type
type Env string
const (
// DEV is the development env tag
// SANDBOX is the sandbox env tag
SANDBOX = iota
// PRODUCTION is the production env tag
PRODUCTION
)
// Service is an Jenga Service
type Service struct {
Username string
Password string
Env int
}
var signature = "vtU9bsRz0WSBrjLYY4dbqloUd0bk7mE6rYa80jxJls7R++YA5hStZIh8mZkMFwQ4UEfXIwQQES8DpP0H9lhyt62ftLf3i6M4WcI31KV4VK2w2Wqf7ZVouw1pYbitWuMcoEQc0YUHBUPMFVmuO8N82ns72914Oms3iOlxg9/pkC1W/FWCHQAOq8RWNGFpmsufEtEnKUOUKAsj0+yVrJ1fpUEpqG2I5hVipz0/c0RVAhuHnTH+/YY6n7jCraSUMMGSfgUDPwY7WgaVfMVv30UTKsq6a0JEdsvOeUVr4jDao+WLK4W6cv3S2vJSDex5lmnQykFptWeVZn0u0PsPu1aTfw=="
// New return a new Jenga Service
func New(username, password string, env int) (Service, error) {
return Service{username, password, env}, nil
}
//Generate the Jenga Access Token
func (s Service) auth() (string, error) {
data := url.Values{}
data.Set("username", s.Username)
data.Add("password", s.Password)
reqUrl := s.baseURL() + "identity-test/v2/token"
req, err := http.NewRequest(http.MethodPost, reqUrl, strings.NewReader(data.Encode()))
if err != nil {
return "", err
}
req.Header.Add("Authorization", "Basic QWEzY0dxRVpWOVo5dDFVSnhTR3BwZUF4WEZrcFFyYUk6cU5BYWs5YXcyVDNtSW1uMg==")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{Timeout: 60 * time.Second}
res, err := client.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return "", fmt.Errorf("could not send auth request: %v", err)
}
var authResponse authResponse
err = json.NewDecoder(res.Body).Decode(&authResponse)
if err != nil {
return "", fmt.Errorf("could not decode auth response: %v", err)
}
accessToken := authResponse.AccessToken
return accessToken, nil
}
// BalanceInquiry sends a balance inquiry
func (s Service) BalanceInquiry() string {
auth, err := s.auth()
if err != nil {
return ""
}
reqUrl := s.baseURL() + "account-test/v2/accounts/balances/KE/0011547896523"
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, reqUrl, nil)
req.Header.Set("Authorization", "Bearer "+auth)
req.Header.Set("signature", signature)
res, err := client.Do(req)
if err != nil {
defer res.Body.Close()
}
stringBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return ""
}
return string(stringBody)
}
// MobileWalletRequest sends a new request
func (s Service) MobileWalletRequest(mobileWallets MobileWallets) (string, error) {
body, err := json.Marshal(mobileWallets)
if err != nil {
return "", nil
}
auth, err := s.auth()
if err != nil {
return "", nil
}
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Bearer " + auth
headers["signature"] = signature
reqUrl := s.baseURL() + "transaction-test/v2/remittance"
return s.newReq(reqUrl, body, headers)
}
// RTGSRequest sends a new request
func (s Service) RTGSRequest(rtgs RTGS) (string, error) {
body, err := json.Marshal(rtgs)
if err != nil {
return "", nil
}
auth, err := s.auth()
if err != nil {
return "", nil
}
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Bearer " + auth
headers["signature"] = signature
reqUrl := s.baseURL() + "transaction-test/v2/remittance"
return s.newReq(reqUrl, body, headers)
}
func (s Service) newReq(url string, body []byte, headers map[string]string) (string, error) {
request, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return "", nil
}
for key, value := range headers {
request.Header.Set(key, value)
}
client := &http.Client{Timeout: 60 * time.Second}
res, err := client.Do(request)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return "", err
}
stringBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(stringBody), nil
}
func (s Service) baseURL() string {
if s.Env == PRODUCTION {
return "https://sandbox.jengahq.io/"
}
return "https://sandbox.jengahq.io/"
}