-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
373 lines (285 loc) · 10 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Package synapse is a wrapper library for the Synapse API (https://docs.synapsefi.com)
Instantiate client
// credentials used to set headers for each method request
var client = synapse.New(
"CLIENT_ID",
"CLIENT_SECRET",
"IP_ADDRESS",
"FINGERPRINT",
)
# Examples
Enable logging & turn off developer mode (developer mode is true by default)
var client = synapse.New(
"CLIENT_ID",
"CLIENT_SECRET",
"IP_ADDRESS",
"FINGERPRINT",
true,
false,
)
Register Fingerprint
// payload response
{
"error": {
"en": "Fingerprint not registered. Please perform the MFA flow."
},
"error_code": "10",
"http_code": "202",
"phone_numbers": [
"901-111-2222"
],
"success": false
}
// Submit a valid email address or phone number from "phone_numbers" list
res, err := user.Select2FA("[email protected]")
// MFA sent to [email protected]
res, err := user.VerifyPIN("123456")
Set an `IDEMPOTENCY_KEY` (for `POST` requests only)
scopeSettings := `{
"scope": [
"USERS|POST",
"USER|PATCH",
"NODES|POST",
"NODE|PATCH",
"TRANS|POST",
"TRAN|PATCH"
],
"url": "https://requestb.in/zp216zzp"
}`
idempotencyKey := `1234567890`
data, err := client.CreateSubscription(scopeSettings, idempotencyKey)
Submit optional query parameters
params := "per_page=3&page=2"
data, err := client.GetUsers(params)
*/
package synapse
import (
"github.com/mitchellh/mapstructure"
)
/********** GLOBAL VARIABLES **********/
var logMode = false
var developerMode = true
/********** TYPES **********/
type (
// Client represents the credentials used by the developer to instantiate a client
Client struct {
ClientID string
ClientSecret string
Fingerprint string
IP string
request Request
}
)
/********** METHODS **********/
func (c *Client) do(method, url, data string, queryParams []string) (map[string]interface{}, error) {
var body []byte
var err error
switch method {
case "GET":
body, err = c.request.Get(url, queryParams)
case "POST":
body, err = c.request.Post(url, data, queryParams)
case "PATCH":
body, err = c.request.Patch(url, data, queryParams)
case "DELETE":
body, err = c.request.Delete(url)
}
return readStream(body), err
}
/********** CLIENT **********/
// New creates a client object
func New(clientID, clientSecret, fingerprint, ipAddress string, modes ...bool) *Client {
log.info("========== CREATING CLIENT INSTANCE ==========")
if len(modes) > 0 {
if modes[0] == true {
logMode = true
}
if len(modes) > 1 && modes[1] == false {
developerMode = false
}
}
request := Request{
clientID: clientID,
clientSecret: clientSecret,
fingerprint: fingerprint,
ipAddress: ipAddress,
}
return &Client{
ClientID: clientID,
ClientSecret: clientSecret,
Fingerprint: fingerprint,
IP: ipAddress,
request: request,
}
}
/********** AUTHENTICATION **********/
// GetPublicKey returns a public key as a token representing client credentials
func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) {
log.info("========== GET PUBLIC KEY ==========")
url := buildURL(path["client"])
defaultScope := "OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS"
if len(scope) > 0 {
defaultScope = scope[0]
}
qp := []string{"issue_public_key=YES&scope=" + defaultScope}
if len(scope) > 1 {
userId := scope[1]
qp[0] += "&user_id=" + userId
}
return c.do("GET", url, "", qp)
}
/********** NODE **********/
// GetNodes returns all of the nodes
func (c *Client) GetNodes(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET CLIENT NODES ==========")
url := buildURL(path["nodes"])
return c.do("GET", url, "", queryParams)
}
// GetTradeMarketData returns data on a stock based on its ticker symbol
func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}, error) {
log.info("========== GET TRADE MARKET DATA ==========")
url := buildURL(path["nodes"], "trade-market-watch")
ts := []string{tickerSymbol}
return c.do("GET", url, "", ts)
}
// GetNodeTypes returns available node types
func (c *Client) GetNodeTypes() (map[string]interface{}, error) {
log.info("========== GET NODE TYPES ==========")
url := buildURL(path["nodes"], "types")
return c.do("GET", url, "", nil)
}
/********** OTHER **********/
// GetCryptoMarketData returns market data for cryptocurrencies
func (c *Client) GetCryptoMarketData() (map[string]interface{}, error) {
log.info("========== GET CRYPTO MARKET DATA ==========")
url := buildURL(path["nodes"], "crypto-market-watch")
return c.do("GET", url, "", nil)
}
// GetCryptoQuotes returns all of the quotes for crypto currencies
func (c *Client) GetCryptoQuotes(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET CRYPTO QUOTES ==========")
url := buildURL(path["nodes"], "crypto-quotes")
return c.do("GET", url, "", queryParams)
}
// GetInstitutions returns a list of all available banking institutions
func (c *Client) GetInstitutions() (map[string]interface{}, error) {
log.info("========== GET INSTITUTIONS ==========")
url := buildURL(path["institutions"])
return c.do("GET", url, "", nil)
}
// LocateATMs returns a list of nearby ATMs
func (c *Client) LocateATMs(queryParams ...string) (map[string]interface{}, error) {
log.info("========== LOCATE ATMS ==========")
url := buildURL(path["nodes"], "atms")
return c.do("GET", url, "", queryParams)
}
// VerifyAddress checks if an address if valid
func (c *Client) VerifyAddress(data string) (map[string]interface{}, error) {
log.info("========== VERIFY ADDRESS ==========")
url := buildURL("address-verification")
return c.do("POST", url, data, nil)
}
// VerifyRoutingNumber checks and returns the bank details of a routing number
func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error) {
log.info("========== VERIFY ROUTING NUMBER ==========")
url := buildURL("routing-number-verification")
return c.do("POST", url, data, nil)
}
/********** SUBSCRIPTION **********/
// GetSubscriptions returns all of the nodes associated with a user
func (c *Client) GetSubscriptions(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET SUBSCRIPTIONS ==========")
url := buildURL(path["subscriptions"])
return c.do("GET", url, "", queryParams)
}
// GetSubscription returns a single subscription
func (c *Client) GetSubscription(subscriptionID string) (map[string]interface{}, error) {
log.info("========== GET SUBSCRIPTION ==========")
url := buildURL(path["subscriptions"], subscriptionID)
return c.do("GET", url, "", nil)
}
// CreateSubscription creates a subscription and returns the subscription data
func (c *Client) CreateSubscription(data string, idempotencyKey ...string) (map[string]interface{}, error) {
log.info("========== CREATE SUBSCRIPTION ==========")
url := buildURL(path["subscriptions"])
return c.do("POST", url, data, idempotencyKey)
}
// UpdateSubscription updates an existing subscription
func (c *Client) UpdateSubscription(subscriptionID string, data string) (map[string]interface{}, error) {
log.info("========== UPDATE SUBSCRIPTION ==========")
url := buildURL(path["subscriptions"], subscriptionID)
return c.do("PATCH", url, data, nil)
}
// GetWebhookLogs returns all of the webhooks sent to a specific client
func (c *Client) GetWebhookLogs() (map[string]interface{}, error) {
log.info("========== GET WEBHOOK LOGS ==========")
url := buildURL(path["subscriptions"], "logs")
return c.do("GET", url, "", nil)
}
/********** TRANSACTION **********/
// GetTransactions returns all client transactions
func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET CLIENT TRANSACTIONS ==========")
url := buildURL(path["transactions"])
return c.do("GET", url, "", queryParams)
}
/********** USER **********/
// GetUsers returns a list of users
func (c *Client) GetUsers(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET CLIENT USERS ==========")
url := buildURL(path["users"])
return c.do("GET", url, "", queryParams)
}
// GetUser returns a single user
func (c *Client) GetUser(userID, fingerprint, ipAddress string, queryParams ...string) (*User, error) {
log.info("========== GET USER ==========")
url := buildURL(path["users"], userID)
res, err := c.do("GET", url, "", queryParams)
var user User
mapstructure.Decode(res, &user)
user.Response = res
request := Request{
clientID: c.ClientID,
clientSecret: c.ClientSecret,
fingerprint: fingerprint,
ipAddress: ipAddress,
}
user.request = request
return &user, err
}
// CreateUser creates a single user and returns the new user data
func (c *Client) CreateUser(data, fingerprint, ipAddress string, idempotencyKey ...string) (*User, error) {
log.info("========== CREATE USER ==========")
var user User
user.request = Request{
clientID: c.ClientID,
clientSecret: c.ClientSecret,
fingerprint: fingerprint,
ipAddress: ipAddress,
}
url := buildURL(path["users"])
res, err := user.do("POST", url, data, idempotencyKey)
mapstructure.Decode(res, &user)
user.Response = res
return &user, err
}
// GetUserDocumentTypes returns available user document types
func (c *Client) GetUserDocumentTypes() (map[string]interface{}, error) {
log.info("========== GET USER DOCUMENT TYPES ==========")
url := buildURL(path["users"], "document-types")
return c.do("GET", url, "", nil)
}
// GetUserEntityTypes returns available user entity types
func (c *Client) GetUserEntityTypes() (map[string]interface{}, error) {
log.info("========== GET USER ENTITY TYPES ==========")
url := buildURL(path["users"], "entity-types")
return c.do("GET", url, "", nil)
}
// GetUserEntityScopes returns available user entity scopes
func (c *Client) GetUserEntityScopes() (map[string]interface{}, error) {
log.info("========== GET USER ENTITY TYPES ==========")
url := buildURL(path["users"], "entity-scopes")
return c.do("GET", url, "", nil)
}