-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
68 lines (55 loc) · 1.26 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
package squarego
import (
"bytes"
"fmt"
"io"
"net/http"
"time"
)
// Service ...
type Service interface {
GetCustomerByID(customerID string) (Customer, error)
UpdateCustomerByID(customerID string, object Customer) (Customer, error)
GetOrderByID(locationID, orderID string) (Order, error)
GetRecentPayments(time.Time) ([]Payment, error)
}
// NewService ...
func NewService(
endpoint string,
token string,
version string,
) Service {
return &service{
Client: &http.Client{},
Endpoint: endpoint,
Token: token,
Version: version,
}
}
type service struct {
Client *http.Client
Endpoint string
Token string
Version string
}
func (svc *service) createRequest(method, ressource string, data []byte) (*http.Response, error) {
var body io.Reader
if data != nil {
body = bytes.NewBuffer(data)
}
req, err := http.NewRequest(method,
fmt.Sprintf("%s/%s", svc.Endpoint, ressource),
body)
if err != nil {
return nil, err
}
req.Header.Set("Square-Version", svc.Version)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", svc.Token))
req.Header.Set("Accept", "application/json")
resp, err := svc.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}