-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
299 lines (247 loc) · 7.97 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package rekki
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/pkg/errors"
)
type OrderList struct {
Orders []Order `json:"orders"`
}
type Order struct {
CustomerAccountNo string `json:"customer_account_no"`
ConfirmedAt *time.Time `json:"confirmed_at"`
ContactInfo string `json:"contact_info"`
ContactName string `json:"contact_name"`
LocationName string `json:"location_name"`
DeliveryAddress string `json:"delivery_address"`
PostCode string `json:"-"`
DeliveryOn SimpleDate `json:"delivery_on"`
InsertedAtTs int64 `json:"inserted_at_ts"`
Notes string `json:"notes"`
Reference string `json:"reference"`
SupplierNotes string `json:"supplier_notes"`
Items []OrderItem `json:"items"`
}
type OrderItem struct {
ID string `json:"id"`
Name string `json:"name"`
Price string `json:"price"`
PriceCents int64 `json:"price_cents"`
ProductCode string `json:"product_code"`
Quantity float64 `json:"quantity"`
Units string `json:"units"`
Spec string `json:"spec"`
}
// OrderMap is an alias for a map<string, Order>
type OrderMap map[string]Order
// OrderIntegrationError is a struct for setting errors for failures
type OrderIntegrationError struct {
Order Order `json:"order"`
Error string `json:"error"`
Attempts int `json:"attempts"`
}
type API interface {
ListNotIntegratedOrders(ctx context.Context, sinceTS int64) (OrderMap, error)
SetOrderIntegrated(ctx context.Context, orderReferences []string) error
SetOrderError(ctx context.Context, e OrderIntegrationError) error
ConfirmOrder(ctx context.Context, orderReference ...string) error
}
type externalSupplierAPI struct {
listURL string
setIntegratedURL string
setErrorURL string
confirmURL string
token string
client *http.Client
}
func NewAPI(client *http.Client, host string, token string) (API, error) {
if client == nil {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true, // assume input is already compressed
}
client = &http.Client{Transport: tr}
}
api := externalSupplierAPI{token: token, client: client}
listURL, err := buildURL(host, "api/integration/v1/orders/list_not_integrated")
if err != nil {
return nil, errors.Wrapf(err, "unable to parse list url")
}
api.listURL = listURL
setURL, err := buildURL(host, "api/integration/v1/orders/set_integrated")
if err != nil {
return nil, errors.Wrapf(err, "unable to parse set integrated url")
}
api.setIntegratedURL = setURL
confirmURL, err := buildURL(host, "api/integration/v3/orders/confirm")
if err != nil {
return nil, errors.Wrapf(err, "unable to parse confirm url")
}
api.confirmURL = confirmURL
errURL, err := buildURL(host, "api/integration/v1/orders/set_error")
if err != nil {
return nil, errors.Wrapf(err, "unable to parse set error url")
}
api.setErrorURL = errURL
return &api, nil
}
func buildURL(host string, p string) (string, error) {
h, err := url.Parse(host)
if err != nil {
return "", errors.Wrapf(err, "unable to parse HOST:%s", host)
}
h.Path = path.Join(h.Path, p)
return h.String(), nil
}
type GetOrdersRequestBody struct {
SinceTS int64 `json:"since"`
}
// ListNotIntegratedOrders fetches orders with `{"since":0}`
func (a *externalSupplierAPI) ListNotIntegratedOrders(ctx context.Context, sinceTS int64) (OrderMap, error) {
reqBody, err := json.Marshal(&GetOrdersRequestBody{SinceTS: sinceTS})
if err != nil {
return nil, errors.Wrap(err, "unable to serialise body")
}
req, err := newRekkiRequest(ctx, a.listURL, a.token, bytes.NewBuffer(reqBody))
if err != nil {
return nil, errors.Wrap(err, "failed to create req for fetching orders")
}
res, err := a.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "req is failed for fetching orders")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request is failed %d - %s", res.StatusCode, string(body))
}
var r OrderList
if err := json.Unmarshal(body, &r); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal order")
}
// TODO We're iterating here already to assemble the map. Should this be
// responsability of the consumer and we just return a list?
orders := make(OrderMap)
for _, v := range r.Orders {
orders[v.Reference] = v
}
return orders, nil
}
// SetOrderIntegrated marks the order as succesfully integrated in ther Rekki platform.
func (a *externalSupplierAPI) SetOrderIntegrated(ctx context.Context, orderReferences []string) error {
var p struct {
Orders []string `json:"orders"`
}
p.Orders = orderReferences
body, err := json.Marshal(&p)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
r, err := newRekkiRequest(ctx, a.setIntegratedURL, a.token, bytes.NewReader(body))
if err != nil {
return errors.Wrap(err, "failed to create req for integrating the order")
}
res, err := a.client.Do(r)
if err != nil {
return errors.Wrap(err, "request failed")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("request is failed %d - %s", res.StatusCode, string(b))
}
return nil
}
// ConfirmOrder marks the order as confirmed in the Rekki platform.
func (a *externalSupplierAPI) ConfirmOrder(ctx context.Context, orderReference ...string) error {
var j struct {
Orders []string `json:"orders"`
}
j.Orders = orderReference
b, err := json.Marshal(&j)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
r, err := newRekkiRequest(ctx, a.confirmURL, a.token, bytes.NewReader(b))
if err != nil {
return errors.Wrap(err, "failed to create req for confirming the order")
}
resp, err := a.client.Do(r)
if err != nil {
return errors.Wrap(err, "request failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("request is failed %d - %s", resp.StatusCode, b)
}
return nil
}
// SetOrderError marks an orders as failed to integrate. Reasons can vary from technical error
// to uncomplete data.
func (a *externalSupplierAPI) SetOrderError(ctx context.Context, e OrderIntegrationError) error {
body, err := json.Marshal(&e)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
r, err := newRekkiRequest(ctx, a.setErrorURL, a.token, bytes.NewReader(body))
if err != nil {
return errors.Wrap(err, "failed to create req for setting the failed order integration")
}
res, err := a.client.Do(r)
if err != nil {
return errors.Wrap(err, "request failed")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("request is failed %d - %s", res.StatusCode, string(b))
}
return nil
}
func newRekkiRequest(ctx context.Context, url string, token string, r io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, r)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
bearer := "Bearer " + token
req.Header.Set("Authorization", bearer)
req.Header.Set("X-REKKI-Authorization-Type", "supplier_api_token")
return req, nil
}
// This alias has been created to deserialise the date
// since the response from the API is not RFC-valid.
type SimpleDate struct {
time.Time
}
func (ct *SimpleDate) MarshalJSON() ([]byte, error) {
if ct.Time.UnixNano() == 0 {
return []byte("null"), nil
}
ctLayout := "2006-01-02"
return []byte(fmt.Sprintf("\"%s\"", ct.Time.Format(ctLayout))), nil
}
func (sd *SimpleDate) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
sd.Time = time.Time{}
return
}
ctLayout := "2006-01-02"
sd.Time, err = time.Parse(ctLayout, s)
return
}