-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv1.go
360 lines (305 loc) · 10.6 KB
/
v1.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package squareupw
import (
"encoding/json"
"log"
)
//EmployeeRole.Permission
const (
RegisterAccessSalesHistory = "REGISTER_ACCESS_SALES_HISTORY"
RegisterApplyRestrictedDiscounts = "REGISTER_APPLY_RESTRICTED_DISCOUNTS"
RegisterChangeSettings = "REGISTER_CHANGE_SETTINGS"
RegisterEditItem = "REGISTER_EDIT_ITEM"
RegisterIssueRefunds = "REGISTER_ISSUE_REFUNDS"
RegisterOpenCashDrawerOutsideSale = "REGISTER_OPEN_CASH_DRAWER_OUTSIDE_SALE"
RegisterViewSummaryReports = "REGISTER_VIEW_SUMMARY_REPORTS"
)
//Merchant represents Merchant model.
type Merchant struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
CountryCode string `json:"country_code"`
LanguageCode string `json:"language_code"`
AccountType string `json:"account_type"`
AccountCapabilities []string `json:"account_capabilities"`
CurrencyCode string `json:"currency_code"`
BusinessName string `json:"business_name"`
BusinessAddress GlobalAddress `json:"business_address"`
BusinessPhone PhoneNumber `json:"business_phone"`
BusinessType string `json:"business_type"`
ShippingAddress GlobalAddress `json:"shipping_address"`
LocationDetails MerchantLocationDetails `json:"location_details"`
MarketURL string `json:"market_url"`
}
//GlobalAddress represents GlobalAddress model.
type GlobalAddress struct {
AddressLine1 string `json:"address_line_1"`
AddressLine2 string `json:"address_line_2"`
AddressLine3 string `json:"address_line_3"`
AddressLine4 string `json:"address_line_4"`
AddressLine5 string `json:"address_line_5"`
Locality string `json:"locality"`
Sublocality string `json:"sublocality"`
Sublocality1 string `json:"sublocality_1"`
Sublocality2 string `json:"sublocality_2"`
Sublocality3 string `json:"sublocality_3"`
Sublocality4 string `json:"sublocality_4"`
Sublocality5 string `json:"sublocality_5"`
AdministrativeDistrictLevel1 string `json:"administrative_district_level_1"`
AdministrativeDistrictLevel2 string `json:"administrative_district_level_2"`
AdministrativeDistrictLevel3 string `json:"administrative_district_level_3"`
PostalCode string `json:"postal_code"`
CountryCode string `json:"country_code"`
AddressCoordinates Coordinates `json:"address_coordinates"`
}
//Employee represents Employee model.
type Employee struct {
ID string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
RoleIds []string `json:"role_ids"`
AuthorizedLocationIds []string `json:"authorized_location_ids"`
Email string `json:"email"`
Status string `json:"status"`
ExternalID string `json:"external_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
//EmployeeRole represents Employee role model.
type EmployeeRole struct {
ID string `json:"id"`
Name string `json:"name"`
Permissions []string `json:"permissions"`
IsOwner bool `json:"is_owner"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
//PhoneNumber represents PhoneNumber model.
type PhoneNumber struct {
CallingCode string `json:"calling_code"`
Number string `json:"number"`
}
//MerchantLocationDetails represents MerchantLocationDetails model.
type MerchantLocationDetails struct {
Nickname string `json:"nickname"`
}
//Coordinates represents Coordinates model.
type Coordinates struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
}
//CreateEmployeeParams represents params for CreateEmployee method.
type CreateEmployeeParams struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email,omitempty"`
*CommonOptionalEmployeeParams
}
//UpdateEmployeeParams represents params for UpdateEmployee method.
type UpdateEmployeeParams struct {
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
*CommonOptionalEmployeeParams
}
//CommonOptionalEmployeeParams represents common params for other *EmployeeParams.
type CommonOptionalEmployeeParams struct {
ExternalID string `json:"external_id,omitempty"`
RoleIds []string `json:"role_ids,omitempty"`
}
//ListEmployeesParams represents params for ListEmployees method.
type ListEmployeesParams struct {
Order string `param:"order"`
BeginUpdatedAt string `param:"begin_updated_at"`
EndUpdatedAt string `param:"end_updated_at"`
BeginCreatedAt string `param:"begin_created_at"`
EndCreatedAt string `param:"end_created_at"`
Status string `param:"status"`
ExternalID string `param:"external_id"`
Limit string `param:"limit"`
}
//RoleParams represents params for *Role methods.
type RoleParams struct {
Name string `json:"name"`
Permissions []string `json:"permissions"`
IsOwner bool `json:"is_owner"`
}
//ListRolesParams represents params for ListRoles methods.
type ListRolesParams struct {
Order string `param:"order"`
Limit string `param:"limit"`
}
func apiURL() string {
return BaseURL + "/v1"
}
//RetrieveBusiness provides a business's account information.
func (a API) RetrieveBusiness() (resp Merchant, err error) {
url := apiURL() + "/me"
method := MethodGet
_, body, err := a.Send(method, url, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//ListLocations provides details for a business's locations, including their IDs.
func (a API) ListLocations() (resp []Merchant, err error) {
url := apiURL() + "/me/locations"
method := MethodGet
_, body, err := a.Send(method, url, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//CreateEmployee creates an employee for a business.
func (a API) CreateEmployee(p CreateEmployeeParams) (resp Employee, err error) {
url := apiURL() + "/me/employees"
method := MethodPost
data, err := json.Marshal(p)
if err != nil {
return
}
_, body, err := a.Send(method, url, data)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//UpdateEmployee modifies the details of an employee.
func (a API) UpdateEmployee(id string, p UpdateEmployeeParams) (resp Employee, err error) {
url := apiURL() + "/me/employees/" + id
method := MethodPut
data, err := json.Marshal(p)
if err != nil {
return
}
_, body, err := a.Send(method, url, data)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//ListEmployees provides summary information for all of a business's employees.
//This endpoint might paginate its results. You should check `link`.
//If it not empty - you can perform additional request via ListEmployeesByLink method.
func (a API) ListEmployees(p ListEmployeesParams) (resp []Employee, link string, err error) {
endpointURL := apiURL() + "/me/employees"
body, link, err := a.list(&p, MethodGet, endpointURL, "")
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//ListEmployeesByLink is ListEmployees alias which get explicit url.
func (a API) ListEmployeesByLink(url string) (resp []Employee, link string, err error) {
p := ListEmployeesParams{}
body, link, err := a.list(&p, MethodGet, "", url)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//RetrieveEmployee provides the details for a single employee.
func (a API) RetrieveEmployee(id string) (resp Employee, err error) {
url := apiURL() + "/me/employees/" + id
method := MethodGet
_, body, err := a.Send(method, url, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//CreateRole creates an employee role you can then assign to employees.
func (a API) CreateRole(p RoleParams) (resp EmployeeRole, err error) {
url := apiURL() + "/me/roles"
method := MethodPost
data, err := json.Marshal(p)
if err != nil {
return
}
log.Printf("%s", data)
_, body, err := a.Send(method, url, data)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//ListRoles provides summary information for all of a business's employee roles.
//This endpoint might paginate its results. You should check `link`.
//If it not empty - you can perform additional request via ListRolesByLink method.
func (a API) ListRoles(p ListRolesParams) (resp []EmployeeRole, link string, err error) {
endpointURL := apiURL() + "/me/roles"
body, link, err := a.list(&p, MethodGet, endpointURL, "")
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//ListRolesByLink is ListRoles alias which get explicit url.
func (a API) ListRolesByLink(url string) (resp []EmployeeRole, link string, err error) {
p := ListRolesParams{}
body, link, err := a.list(&p, MethodGet, "", url)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//RetrieveRole provides the details for a single employee role.
func (a API) RetrieveRole(id string) (resp EmployeeRole, err error) {
url := apiURL() + "/me/roles/" + id
method := MethodGet
_, body, err := a.Send(method, url, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//UpdateRole modifies the details of an employee role.
func (a API) UpdateRole(id string, p RoleParams) (resp EmployeeRole, err error) {
url := apiURL() + "/me/roles/" + id
method := MethodPut
data, err := json.Marshal(p)
if err != nil {
return
}
_, body, err := a.Send(method, url, data)
if err != nil {
return
}
err = json.Unmarshal(body, &resp)
return
}
//list method is DRY method for all other List* methods.
func (a API) list(p interface{}, method, endpointURL, url string) (body []byte, link string, err error) {
if len(url) < 1 {
queryString, e := GetQueryStringByStruct(p, "param", true)
if e != nil {
err = e
return
}
if len(queryString) > 1 {
queryString = "?" + queryString
}
url = endpointURL + queryString
}
log.Println(url)
resp, body, err := a.Send(method, url, nil)
if err != nil {
return
}
if linkHeader, ok := resp.Header["Link"]; ok {
link, err = ExtractURLFromLinkHeader(linkHeader)
}
return
}