This repository has been archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plaid.go
98 lines (85 loc) · 2.23 KB
/
plaid.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
package plaid
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Option allows users to set configurable options for the Plaid client
type Option func(*Client) error
// Client stores client info for Plaid and is passed to utilize Plaid products
type Client struct {
envURL string
clientID string
clientSecret string
}
const (
// Sandbox points to the Plaid Sandbox environment
Sandbox = "https://sandbox.plaid.com"
// Development points to the Plaid UAT environment
Development = "https://development.plaid.com/"
// Production points to the Plaid Production environment
Production = "https://production.plaid.com/"
)
// Configure sets up a plaid client and returns interfaces that
// can be used to request the various products
func Configure(clientID, clientSecret string, ops ...Option) (Client, error) {
clnt := Client{
envURL: Sandbox,
clientSecret: clientSecret,
clientID: clientID,
}
for _, op := range ops {
if err := op(&clnt); err != nil {
return Client{}, err
}
}
return clnt, nil
}
/*** Options ***/
// SetEnvironment sets the remote URL to which the client will send requests
func SetEnvironment(url string) Option {
return func(c *Client) error {
if url != Development && url != Production && url != Sandbox {
return fmt.Errorf("Must select either %v, %v, or %v for url", Development, Sandbox, Production)
}
c.envURL = url
return nil
}
}
// SetWebhooks sets up the plaid client to handle webhooks
func SetWebhooks() Option {
return func(c *Client) error {
return nil
}
}
/*** Abstract http methods ***/
// getRequest ...
type getRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
AccessToken string `json:"access_token"`
}
func get(url string, clnt Client, i Item) ([]byte, error) {
bts, err := json.Marshal(getRequest{
ClientID: clnt.clientID,
Secret: clnt.clientSecret,
AccessToken: i.AccessToken,
})
if err != nil {
return nil, err
}
res, err := http.Post(url, "application/json", bytes.NewBuffer(bts))
if err != nil {
return nil, err
}
bts, err = ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, formatError(bts)
}
return bts, nil
}