-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpot.go
132 lines (101 loc) · 2.9 KB
/
pot.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
func listPots(c *MonzoClient, id string) error {
path := "pots"
requestURL := fmt.Sprintf("%s/%s", c.endpoints["APIURL"], path)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
q := req.URL.Query()
q.Add("current_account_id", id)
req.URL.RawQuery = q.Encode()
rsp, err := c.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", rsp.StatusCode)
}
if rsp.Body == nil {
return fmt.Errorf("response body is empty")
}
var pots Pots
err = json.NewDecoder(rsp.Body).Decode(&pots)
if err != nil {
return err
}
for _, pot := range pots.Pots {
if !pot.Deleted {
fmt.Printf("Pot: %s - %s\n", pot.Name, pot.ID)
}
}
return nil
}
func depositToPot(c *MonzoClient, accountID, potID string, amount int64) error {
path := "pots/" + potID + "/deposit"
requestURL := fmt.Sprintf("%s/%s", c.endpoints["APIURL"], path)
dedupeID := fmt.Sprintf("dedupe_id_%d", time.Now().UnixNano())
form := url.Values{}
form.Set("source_account_id", accountID)
form.Set("amount", fmt.Sprintf("%d", amount))
form.Set("dedupe_id", dedupeID)
formData := form.Encode()
req, err := http.NewRequest("PUT", requestURL, bytes.NewBufferString(formData))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rsp, err := c.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", rsp.StatusCode)
}
if rsp.Body == nil {
return fmt.Errorf("response body is empty")
}
fmt.Println("Deposited to pot successfully!")
return nil
}
func withdrawFromPot(c *MonzoClient, accountID, potID string, amount int64) error {
path := "pots/" + potID + "/withdraw"
requestURL := fmt.Sprintf("%s/%s", c.endpoints["APIURL"], path)
dedupeID := fmt.Sprintf("dedupe_id_%d", time.Now().UnixNano())
form := url.Values{}
form.Set("destination_account_id", accountID)
form.Set("amount", fmt.Sprintf("%d", amount))
form.Set("dedupe_id", dedupeID)
formData := form.Encode()
req, err := http.NewRequest("PUT", requestURL, bytes.NewBufferString(formData))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rsp, err := c.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", rsp.StatusCode)
}
if rsp.Body == nil {
return fmt.Errorf("response body is empty")
}
fmt.Println("Withdrawn from pot successfully!")
return nil
}