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
/
Copy pathitem.go
120 lines (110 loc) · 3.01 KB
/
item.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
package plaid
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type itemResponse struct {
AccessToken string `json:"access_token"`
RequestID string `json:"requestID"`
Item `json:"item"`
}
type rotateResponse struct {
NewAccessToken string `json:"new_access_token"`
RequestID string `json:"request_id"`
}
type updateVersionRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
AccessTokenV1 string `json:"access_token_v1"`
}
type webhookUpdateRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
AccessToken string `json:"access_token"`
Webhook string `json:"webhook"`
}
// GetItem fetches an item and is the first call made for the library
func GetItem(clnt Client, accessToken string) (Item, error) {
bts, err := get(fmt.Sprintf("%v/item/get", clnt.envURL), clnt, Item{
AccessToken: accessToken,
})
if err != nil {
return Item{}, err
}
ir := itemResponse{}
err = json.Unmarshal(bts, &ir)
return ir.Item, err
}
// RotateAccessToken fetches an item and is the first call made for the library
func RotateAccessToken(clnt Client, accessToken string) (Item, error) {
bts, err := get(fmt.Sprintf("%v/item/access_token/invalidate", clnt.envURL), clnt, Item{
AccessToken: accessToken,
})
if err != nil {
return Item{}, err
}
rr := rotateResponse{}
if err = json.Unmarshal(bts, &rr); err != nil {
return Item{}, err
}
return GetItem(clnt, rr.NewAccessToken)
}
// UpdateAccessTokenVersion updates legacy access tokens to use the new API
func UpdateAccessTokenVersion(clnt Client, accessToken string) (Item, error) {
bts, err := json.Marshal(updateVersionRequest{
ClientID: clnt.clientID,
Secret: clnt.clientSecret,
AccessTokenV1: accessToken,
})
if err != nil {
return Item{}, err
}
res, err := http.Post(fmt.Sprintf("%v/item/access_token/update_version", clnt.envURL), "application/json", bytes.NewBuffer(bts))
if err != nil {
return Item{}, err
}
bts, err = ioutil.ReadAll(res.Body)
if err != nil {
return Item{}, err
}
if res.StatusCode != http.StatusOK {
return Item{}, formatError(bts)
}
ir := itemResponse{}
if err = json.Unmarshal(bts, &ir); err != nil {
return Item{}, err
}
return GetItem(clnt, ir.AccessToken)
}
// UpdateWebhook updates an item's webhook
func (i Item) UpdateWebhook(clnt Client, newURL string) error {
bts, err := json.Marshal(webhookUpdateRequest{
ClientID: clnt.clientID,
Secret: clnt.clientSecret,
AccessToken: i.AccessToken,
Webhook: newURL,
})
if err != nil {
return err
}
res, err := http.Post(fmt.Sprintf("%v/item/webhook/update", clnt.envURL), "application/json", bytes.NewBuffer(bts))
if err != nil {
return err
}
bts, err = ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return formatError(bts)
}
return nil
}
// Delete removes an Item
func (i Item) Delete(clnt Client) error {
_, err := get(fmt.Sprintf("%v/item/delete", clnt.envURL), clnt, i)
return err
}