-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxero.go
184 lines (150 loc) · 4.49 KB
/
xero.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package xero
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"github.com/google/go-querystring/query"
)
const (
baseURL = "https://api.xero.com/api.xro/2.0"
// Xero Rate limit response headers
// https://developer.xero.com/documentation/oauth2/limits
headerRateDayLimitRemain = "X-DayLimit-Remaining" // Number of remaining Day limit.
headerRateMinLimitRemain = "X-MinLimit-Remaining" // Number of remaining Minute limit.
headerRateLimit = "X-Rate-Limit-Problem" // Which limit you have reached.
headerRateRetry = "Retry-After" // How many seconds to wait before making another request.
mediaTypeJSON = "application/json"
)
// TenantID used when communicating with the Xero API.
type TenantID string
// HTTPClient interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// A Client manages communication with the Xero API.
type Client struct {
client HTTPClient
// Base URL for API requests.
BaseURL *url.URL
// Tenant used to do requests to API endpoints.
TenantID TenantID
// Reuse a single struct instead of allocating one for each service on the heap.
common service
// Services used for talking to different parts of the Xero API.
Invoices *InvoicesService
Accounts *AccountsService
}
type service struct {
client *Client
}
// NewClient returns a new Xero API client. If a nil httpClient is
// provided, a new http.Client will be used.
func NewClient(httpClient HTTPClient, tenantID TenantID) *Client {
if httpClient == nil {
httpClient = &http.Client{}
}
baseURL, _ := url.Parse(baseURL)
c := &Client{client: httpClient, BaseURL: baseURL, TenantID: tenantID}
c.common.client = c
// Available Xero API resources
c.Invoices = (*InvoicesService)(&c.common)
c.Accounts = (*AccountsService)(&c.common)
return c
}
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
invalidTokenError := &InvalidTokenError{}
err := parseResponse(res, invalidTokenError)
if err != nil {
return nil, err
}
return nil, invalidTokenError
case http.StatusBadRequest:
badRequestError := &BadRequestError{}
err := parseResponse(res, badRequestError)
if err != nil {
return nil, err
}
// Return a JSON string with the list of errors for more detailed view of what happened.
b, err := json.Marshal(badRequestError)
if err != nil {
return nil, err
}
return nil, errors.New(string(b))
}
// For POST and PUT requests
if v != nil {
if err := json.NewDecoder(res.Body).Decode(v); err != nil {
if !errors.Is(err, io.EOF) { // ignore EOF errors caused by empty response body
return nil, err
}
}
}
return res, nil
}
// NewRequest creates an API request. If specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) NewRequest(method string, url string, body interface{}) (*http.Request, error) {
u, err := c.BaseURL.Parse(url)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if body != nil {
buf = &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", mediaTypeJSON)
req.Header.Set("Xero-tenant-id", string(c.TenantID))
if body != nil {
req.Header.Set("Content-Type", mediaTypeJSON)
}
return req, nil
}
// parseResponse decodes the response body into the target
func parseResponse(res *http.Response, target interface{}) error {
err := json.NewDecoder(res.Body).Decode(target)
if err != nil {
return fmt.Errorf("failed to read error response body: %s", err)
}
return nil
}
// addOptions adds the parameters in opt as URL query parameters to s. opt
// must be a struct whose fields may contain "url" tags.
func addOptions(s string, opts interface{}) (string, error) {
v := reflect.ValueOf(opts)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qs, err := query.Values(opts)
if err != nil {
return s, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}