-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
364 lines (310 loc) · 8.55 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
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
361
362
363
364
package dpd
import (
"github.com/fiorix/wsdl2go/soap"
)
const (
trackingByDPDOrderNamespace = "http://dpd.ru/ws/tracing/2012-06-25"
//CityCODPaymentLimit represents default C.O.D. payment limit for all dpd cities in which C.O.D. is available
CityCODPaymentLimit = 250000
)
//Client for DPD soap api
type Client struct {
auth *Auth
services *soapClients
}
//ServiceUrls DPD services urls
type ServiceUrls struct {
CalculatorURL string
GeographyURL string
OrderURL string
TrackingURL string
}
type soapClients struct {
geography *soap.Client
order *soap.Client
calculator *soap.Client
tracking *soap.Client
}
//NewClient creates new client
func NewClient(clientNumber int64, clientKey string, urls ServiceUrls) *Client {
return &Client{
auth: &Auth{
ClientNumber: &clientNumber,
ClientKey: &clientKey,
},
services: &soapClients{
geography: &soap.Client{
Namespace: geographyNamespace,
URL: urls.GeographyURL,
},
order: &soap.Client{
Namespace: orderNamespace,
URL: urls.OrderURL,
},
calculator: &soap.Client{
Namespace: calculatorNamespace,
URL: urls.CalculatorURL,
},
tracking: &soap.Client{
Namespace: trackingNamespace,
URL: urls.TrackingURL,
},
},
}
}
//GetParcelShops return list of pickup/delivery points with restrictions about weight and dimensions
func (cl *Client) GetParcelShops(req *ParcelShopRequest) ([]*ParcelShop, error) {
req.Auth = cl.auth
req.Namespace = ""
a := struct {
operationGetParcelShops `xml:"tns:getParcelShops"`
}{
operationGetParcelShops{
&getParcelShops{
req,
geographyNamespace,
},
},
}
y := struct {
operationGetParcelShopsResponse `xml:"getParcelShopsResponse"`
}{}
if err := cl.services.geography.RoundTripWithAction("GetParcelShops", a, &y); err != nil {
return nil, err
}
return y.operationGetParcelShopsResponse.GetParcelShopsResponse.Return.ParcelShop, nil
}
//GetTerminalsSelfDelivery2 return terminals list. They don't have any restrictions
func (cl *Client) GetTerminalsSelfDelivery2() ([]*Terminal, error) {
auth := cl.auth
auth.Namespace = new(string)
a := struct {
operationGetTerminalsSelfDelivery2 `xml:"tns:getTerminalsSelfDelivery2"`
}{
operationGetTerminalsSelfDelivery2{
&getTerminalsSelfDelivery2Request{
Auth: auth,
Namespace: geographyNamespace,
},
},
}
y := struct {
operationGetTerminalsSelfDelivery2Response `xml:"getTerminalsSelfDelivery2Response"`
}{}
if err := cl.services.geography.RoundTripWithAction("getTerminalsSelfDelivery2Request", a, &y); err != nil {
return nil, err
}
return y.Response.Return.Terminal, nil
}
//GetCitiesCashPay countryCode must comply with ISO 3166-1 alpha-2
func (cl *Client) GetCitiesCashPay(countryCode string) ([]*City, error) {
a := struct {
operationGetCitiesCashPay `xml:"tns:getCitiesCashPay"`
}{
operationGetCitiesCashPay{
&getCitiesCashPay{
Namespace: geographyNamespace,
Request: &citiesCashPayRequest{
Auth: cl.auth,
CountryCode: &countryCode,
},
},
},
}
y := struct {
operationGetCitiesCashPayResponse `xml:"getCitiesCashPayResponse"`
}{}
if err := cl.services.geography.RoundTripWithAction("GetCitiesCashPay", a, &y); err != nil {
return nil, err
}
return y.GetCitiesCashPayResponse.Return, nil
}
//GetServiceCost2 return cost of delivery for Russia and Customs Union
func (cl *Client) GetServiceCost2(req *CalculateRequest) ([]*ServiceCostResponse, error) {
req.Auth = cl.auth
a := struct {
operationGetServiceCost2 `xml:"tns:getServiceCost2"`
}{
operationGetServiceCost2{
&getServiceCostRequest{
Namespace: calculatorNamespace,
Request: req,
},
},
}
y := struct {
operationGetServiceCost2Response `xml:"getServiceCost2Response"`
}{}
if err := cl.services.calculator.RoundTripWithAction("GetServiceCost2", a, &y); err != nil {
return nil, err
}
return y.GetServiceCostResponse.Return, nil
}
//CreateOrder create a delivery order
func (cl *Client) CreateOrder(req *CreateOrderRequest) ([]*OrderStatus, error) {
req.Auth = cl.auth
a := struct {
operationCreateOrder `xml:"tns:createOrder"`
}{
operationCreateOrder{
&createOrder{
Namespace: orderNamespace,
Orders: req,
},
},
}
y := struct {
operationCreateOrderResponse `xml:"createOrderResponse"`
}{}
if err := cl.services.order.RoundTripWithAction("CreateOrder", a, &y); err != nil {
return nil, err
}
return y.CreateOrderResponse.Return, nil
}
//AddParcels change order with parcel addition
func (cl *Client) AddParcels(req *UpdateOrderRequest) (*OrderCorrectionStatus, error) {
req.Auth = cl.auth
a := struct {
operationAddParcels `xml:"tns:addParcels"`
}{
operationAddParcels{
&addParcels{
req,
},
},
}
y := struct {
operationAddParcelsResponse `xml:"addParcelsResponse"`
}{}
if err := cl.services.order.RoundTripWithAction("AddParcels", a, &y); err != nil {
return nil, err
}
return y.AddParcelsResponse.Return, nil
}
//RemoveParcels change order with parcel deletion
func (cl *Client) RemoveParcels(req *UpdateOrderRequest) (*OrderCorrectionStatus, error) {
req.Auth = cl.auth
a := struct {
operationRemoveParcels `xml:"tns:removeParcels"`
}{
operationRemoveParcels{
&removeParcels{
req,
},
},
}
y := struct {
operationRemoveParcelsResponse `xml:"removeParcelsResponse"`
}{}
if err := cl.services.order.RoundTripWithAction("RemoveParcels", a, &y); err != nil {
return nil, err
}
return y.RemoveParcelsResponse.Return, nil
}
//CancelOrder cancel a delivery order
func (cl *Client) CancelOrder(req *CancelOrderRequest) ([]*OrderStatus, error) {
req.Auth = cl.auth
a := struct {
operationCancelOrder `xml:"tns:cancelOrder"`
}{
operationCancelOrder{
&cancelOrder{
req,
},
},
}
y := struct {
operationCancelOrderResponse `xml:"cancelOrderResponse"`
}{}
if err := cl.services.order.RoundTripWithAction("CancelOrder", a, &y); err != nil {
return nil, err
}
return y.CancelOrderResponse.Return, nil
}
//GetOrderStatus returns order creation status
func (cl *Client) GetOrderStatus(req []*InternalOrderNumber) ([]*OrderStatus, error) {
a := struct {
operationGetOrderStatus `xml:"tns:getOrderStatus"`
}{
operationGetOrderStatus{
&getOrderStatus{
Namespace: orderNamespace,
OrderStatus: &getOrderStatusRequest{
Auth: cl.auth,
Order: req,
},
},
},
}
y := struct {
operationGetOrderStatusResponse `xml:"getOrderStatusResponse"`
}{}
if err := cl.services.order.RoundTripWithAction("GetOrderStatus", a, &y); err != nil {
return nil, err
}
return y.GetOrderStatusResponse.Return, nil
}
//GetStatesByClient returns all statuses of client parcels since previous method call
func (cl *Client) GetStatesByClient() (*ParcelsStates, error) {
a := struct {
operationGetStatesByClient `xml:"tns:getStatesByClient"`
}{
operationGetStatesByClient{
&getStatesByClient{
&getStatesByClientRequest{
Auth: cl.auth,
},
},
},
}
y := struct {
operationGetStatesByClientResponse `xml:"getStatesByClientResponse"`
}{}
if err := cl.services.tracking.RoundTripWithAction("GetStatesByClient", a, &y); err != nil {
return nil, err
}
return y.GetStatesByClientResponse.Return, nil
}
//GetStatesByClientOrder returns list of states for all parcels for specified order
//Order identify by client side order number
func (cl *Client) GetStatesByClientOrder(req *TrackByClientOrderRequest) (*ParcelsStates, error) {
req.Auth = cl.auth
a := struct {
operationGetStatesByClientOrder `xml:"tns:getStatesByClientOrder"`
}{
operationGetStatesByClientOrder{
&getStatesByClientOrder{
req,
},
},
}
y := struct {
operationGetStatesByClientOrderResponse `xml:"getStatesByClientOrderResponse"`
}{}
if err := cl.services.tracking.RoundTripWithAction("GetStatesByClientOrder", a, &y); err != nil {
return nil, err
}
return y.GetStatesByClientOrderResponse.Return, nil
}
//GetStatesByDPDOrder returns states history for specified parcel
//Parcel identify by client side parcel number
func (cl *Client) GetStatesByDPDOrder(req *TrackByDPDOrderRequest) (*ParcelsStates, error) {
req.Auth = cl.auth
a := struct {
operationGetStatesByDPDOrder `xml:"tns:getStatesByDPDOrder"`
}{
operationGetStatesByDPDOrder{
&getStatesByDPDOrder{
Namespace: trackingByDPDOrderNamespace,
Request: req,
},
},
}
y := struct {
operationGetStatesByDPDOrderResponse `xml:"getStatesByDPDOrderResponse"`
}{}
if err := cl.services.tracking.RoundTripWithAction("GetStatesByDPDOrder", a, &y); err != nil {
return nil, err
}
return y.GetStatesByDPDOrderResponse.Return, nil
}