-
Notifications
You must be signed in to change notification settings - Fork 0
/
receipts.go
52 lines (43 loc) · 1.01 KB
/
receipts.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func createReceipt(c *MonzoClient, payload []byte) error {
path := "transaction-receipts"
requestURL := fmt.Sprintf("%s/%s", c.endpoints["APIURL"], path)
req, err := http.NewRequest("PUT", requestURL, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
req.Header.Set("Content-Type", "application/json")
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")
}
return nil
}
func createReceipts(c *MonzoClient, r []Receipt) error {
for i, receipt := range r {
rb, err := json.Marshal(receipt)
if err != nil {
return err
}
err = createReceipt(c, rb)
if err != nil {
return err
}
fmt.Printf("Uploaded %d/%d receipts\n", i+1, len(r))
}
return nil
}