-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.go
253 lines (198 loc) · 8.64 KB
/
rest.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
package assistentby
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type method string
const (
getProfile method = "user"
getBankAccounts method = "bank-accounts?team_id="
storeBankAccount method = "bank-accounts"
updateBankAccount method = "bank-accounts/{id}"
deleteBankAccount method = "bank-accounts/{id}"
getBusinesses method = "businesses?team_id="
storeBusiness method = "businesses"
updateBusiness method = "businesses/{id}"
deleteBusiness method = "businesses/{id}"
getContractors method = "contractors?team_id="
storeContractor method = "contractors"
updateContractor method = "contractors/{id}"
deleteContractor method = "contractors/{id}"
getCurrencies method = "currencies"
getBanks method = "banks"
getDocuments method = "documents?team_id="
getDocumentQueue method = "documents/{id}/queue"
storeDocumentXml method = "documents/{id}/xml"
sendDocument method = "documents/{id}/send"
approveDocument method = "documents/{id}/approve"
confirmDocument method = "documents/{id}/confirm"
shareDocument method = "documents/{id}/share"
storeDocument method = "documents"
updateDocument method = "documents/{id}"
deleteDocument method = "documents/{id}"
getDocumentStatues method = "documents/statuses"
getDocumentTypes method = "documents/types?team_id="
getDocumentOperations method = "documents/operations?team_id="
getDocumentBills method = "documents/bills?team_id="
getOkveds method = "okveds"
getOkvedCategories method = "okveds/categories"
getOperations method = "operations?team_id="
storeOperation method = "operations"
updateOperation method = "operations/{id}"
deleteOperation method = "operations/{id}"
getLatestOperation method = "operations/latest"
getOperationFlows method = "operations/flows"
getOperationTypes method = "operations/types"
getOperationStats method = "operations/stats"
storePushTokens method = "push-tokens"
deletePushTokens method = "push-tokens/{id}"
getPayments method = "payments?team_id="
storePayment method = "payments"
storePaymentWord method = "payments"
updatePayment method = "payments/{id}"
deletePayment method = "payments/{id}"
getLatestPayment method = "payments/latest"
getPaymentStatuses method = "payments/statues"
getServiceCategories method = "service-categories?team_id="
getServices method = "services?team_id="
storeService method = "services"
updateService method = "services/{id}"
deleteService method = "services/{id}"
getLatestService method = "services/latest"
getEmployees method = "employees?team_id="
getMonthlyEmployees method = "monthly-employees?team_id="
getTaxes method = "taxes"
getTeams method = "teams?team_id="
getTeamIntegrations method = "teams/{id}/integrations"
getTeamWidgetToggle method = "teams/{id}/widgets/toggle"
getTeamRequsisites method = "teams/{id}/requisites"
getTeamImages method = "teams/{id}/images"
getTaxDepartments method = "tax-departments"
getSocialDepartments method = "social-departments"
getInsuranceDepartments method = "insurance-departments"
storeBankStatement method = "bank-statements"
getAccountRequisites method = "accounting-requisites"
getBusinessTasks method = "business-tasks?team_id="
getTickets method = "tickets?team_id="
storeTicket method = "tickets"
updateTicket method = "tickets/{ticketId}"
deleteTicket method = "tickets/{ticketId}"
getTicketComments method = "tickets/{id}/comments?team_id="
storeTicketComment method = "tickets/{id}/comments"
updateTicketComment method = "tickets/{id}/comments/{commentId}"
deleteTicketComment method = "tickets/{id}/comments/{commentId}"
getTicketDepartments method = "ticket-departments"
getUnits method = "units"
getVatRates method = "vat-rates"
getRoles method = "roles"
getConstants method = "constants"
)
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
type ClientApi struct {
c Doer // ClientApi
endpoints map[method]string
token string
baseUrl string
teamId string
}
func NewClient(teamId string, token string, baseApiUrl string) ClientApi {
if baseApiUrl == "" {
baseApiUrl = BaseApiUrl
}
return ClientApi{
c: &http.Client{},
endpoints: createEndpoints(baseApiUrl, teamId),
baseUrl: baseApiUrl,
teamId: teamId,
token: token,
}
}
func (c *ClientApi) validateResponse(res *http.Response) ([]byte, error) {
ct := res.Header.Get(http.CanonicalHeaderKey("Content-Type"))
if ct != "application/json" {
return nil, errors.New("response is not json")
}
if res.StatusCode > 201 {
return nil, errors.New("validation response error")
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (c *ClientApi) getEndpoint(method method) string {
endpoint, ok := c.endpoints[method]
if !ok {
panic(fmt.Errorf("endpoint %s not found", string(method)))
}
return endpoint
}
func createEndpoints(baseURI string, teamId string) map[method]string {
list := make(map[method]string)
list[getOperations] = fmt.Sprint(baseURI, "/", string(getOperations), string(teamId))
list[getBankAccounts] = fmt.Sprint(baseURI, "/", string(getBankAccounts), string(teamId))
list[getBusinesses] = fmt.Sprint(baseURI, "/", string(getBusinesses), string(teamId))
list[getContractors] = fmt.Sprint(baseURI, "/", string(getContractors), string(teamId))
list[getCurrencies] = fmt.Sprint(baseURI, "/", string(getCurrencies))
list[getBanks] = fmt.Sprint(baseURI, "/", string(getBanks))
list[getDocuments] = fmt.Sprint(baseURI, "/", string(getDocuments), string(teamId))
list[getDocumentStatues] = fmt.Sprint(baseURI, "/", string(getDocumentStatues), string(teamId))
list[getDocumentTypes] = fmt.Sprint(baseURI, "/", string(getDocumentTypes), string(teamId))
list[getLatestOperation] = fmt.Sprint(baseURI, "/", string(getLatestOperation), string(teamId))
list[getLatestPayment] = fmt.Sprint(baseURI, "/", string(getLatestPayment), string(teamId))
list[getLatestService] = fmt.Sprint(baseURI, "/", string(getLatestService), string(teamId))
list[getOkvedCategories] = fmt.Sprint(baseURI, "/", string(getOkvedCategories), string(teamId))
list[getOkveds] = fmt.Sprint(baseURI, "/", string(getOkveds), string(teamId))
list[getOperationFlows] = fmt.Sprint(baseURI, "/", string(getOperationFlows), string(teamId))
list[getOperationTypes] = fmt.Sprint(baseURI, "/", string(getOperationTypes), string(teamId))
list[getProfile] = fmt.Sprint(baseURI, "/", string(getProfile))
list[getPayments] = fmt.Sprint(baseURI, "/", string(getPayments), string(teamId))
list[getPaymentStatuses] = fmt.Sprint(baseURI, "/", string(getPaymentStatuses), string(teamId))
list[getServiceCategories] = fmt.Sprint(baseURI, "/", string(getServiceCategories), string(teamId))
list[getServices] = fmt.Sprint(baseURI, "/", string(getServices), string(teamId))
list[getLatestService] = fmt.Sprint(baseURI, "/", string(getLatestService), string(teamId))
list[getTaxes] = fmt.Sprint(baseURI, "/", string(getTaxes), string(teamId))
list[getTeams] = fmt.Sprint(baseURI, "/", string(getTeams), string(teamId))
list[getTicketComments] = fmt.Sprint(baseURI, "/", string(getTicketComments), string(teamId))
list[getTicketDepartments] = fmt.Sprint(baseURI, "/", string(getTicketDepartments), string(teamId))
list[getTickets] = fmt.Sprint(baseURI, "/", string(getTickets), string(teamId))
list[getUnits] = fmt.Sprint(baseURI, "/", string(getUnits), string(teamId))
list[getVatRates] = fmt.Sprint(baseURI, "/", string(getVatRates), string(teamId))
list[storeOperation] = fmt.Sprint(baseURI, "/", string(storeOperation))
list[storeBankAccount] = fmt.Sprint(baseURI, "/", string(storeBankAccount))
list[storeContractor] = fmt.Sprint(baseURI, "/", string(storeContractor))
return list
}
func (c *ClientApi) postRequest(url string, postData interface{}) ([]byte, error) {
j, _ := json.Marshal(postData)
form := bytes.NewBuffer(j)
req, err := http.NewRequest("POST", url, form)
req.Header.Set("Content-type", "application/json")
req.Header.Set("Authorization", c.token)
response, err := c.c.Do(req)
defer response.Body.Close()
res, err := c.validateResponse(response)
if err != nil {
return nil, err
}
return res, err
}
func (c *ClientApi) getRequest(url string, params string) ([]byte, error) {
req, err := http.NewRequest("GET", url+"?"+params, nil)
req.Header.Set("Content-type", "application/json")
req.Header.Set("Authorization", c.token)
response, err := c.c.Do(req)
defer response.Body.Close()
res, err := c.validateResponse(response)
if err != nil {
return nil, err
}
return res, err
}