-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
123 lines (100 loc) · 2.92 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
package gocardless
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
uuid "github.com/satori/go.uuid"
)
const (
apiVersion = "2015-07-06"
baseLiveURL = `https://api.gocardless.com/`
baseSandboxURL = `https://api-sandbox.gocardless.com/`
)
// Client for interacting with the GoCardless Pro API
type Client struct {
// AccessToken is the bearer token used to authenticate requests to the GoCardless API
AccessToken string
// RemoteURL is the address of the GoCardless API
RemoteURL string
}
// NewClient instantiate a client struct with your access token and environment, then
// use the resource methods to access the API
func NewClient(accessToken string, env Environment) *Client {
c := &Client{
AccessToken: accessToken,
}
switch env {
case SandboxEnvironment:
c.RemoteURL = baseSandboxURL
case LiveEnvironment:
c.RemoteURL = baseLiveURL
default:
log.Fatalf("Invalid environment %s, use one of (%s, %s)", env, SandboxEnvironment, LiveEnvironment)
}
return c
}
func (c *Client) makeRequest(path, method string, body, dst interface{}) error {
req, err := c.newRequest(path, method, body)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusTooManyRequests:
return errors.New("StatusTooManyRequests")
}
res := newResponse(resp)
// bind response to struct
return res.bind(dst)
}
func (c *Client) newRequest(path, method string, body interface{}) (*http.Request, error) {
if strings.ToUpper(method) == http.MethodPatch {
return nil, errors.New(InvalidMethodError)
}
url := fmt.Sprintf("%s%s", c.RemoteURL, path)
var bs []byte
if body != nil {
bs, _ = json.Marshal(body)
}
data := ioutil.NopCloser(bytes.NewBuffer(bs))
req, err := http.NewRequest(method, url, data)
if err != nil {
return nil, err
}
// set default headers
c.setDefaultHeaders(req)
if method == http.MethodPost {
// Add Idempotency header key when creating a resouce
// https://developer.gocardless.com/api-reference/#making-requests-idempotency-keys
u, _ := uuid.NewV4()
req.Header.Add("Idempotency-Key", u.String())
}
return req, nil
}
func (c *Client) setDefaultHeaders(req *http.Request) {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.AccessToken))
req.Header.Add("GoCardless-Version", apiVersion)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
}
func (c *Client) get(path string, dst interface{}) error {
return c.makeRequest(path, http.MethodGet, nil, dst)
}
func (c *Client) post(path string, body, dst interface{}) error {
return c.makeRequest(path, http.MethodPost, body, dst)
}
func (c *Client) put(path string, body, dst interface{}) error {
return c.makeRequest(path, http.MethodPut, body, dst)
}
func (c *Client) delete(path string) error {
return c.makeRequest(path, http.MethodDelete, nil, nil)
}