Skip to content

Commit d53648c

Browse files
Merge pull request #12 from byronwilliams/bw-line-items
feat: specify parcel line items
2 parents d659a47 + 5fda012 commit d53648c

File tree

4 files changed

+119
-48
lines changed

4 files changed

+119
-48
lines changed

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
88
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
99
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
1010
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1112
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1213
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1314
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

method.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ type MethodResponse struct {
4141
}
4242

4343
type CountryResponse struct {
44-
Iso2 string `json:"iso_2"`
45-
Iso3 string `json:"iso_3"`
46-
ID int `json:"id"`
47-
Price float64 `json:"price"`
48-
Name string `json:"name"`
44+
Iso2 string `json:"iso_2"`
45+
Iso3 string `json:"iso_3"`
46+
ID int `json:"id"`
47+
Price float64 `json:"price"`
48+
Name string `json:"name"`
49+
LeadTimeHours *float64 `json:"lead_time_hours"`
4950
}
5051

5152
//Get formatted response

parcel.go

+104-42
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import (
77

88
type LabelData []byte
99

10+
type CustomsShipmentType int
11+
12+
const (
13+
CustomsShipmentTypeGift = iota
14+
CustomsShipmentTypeDocuments
15+
CustomsShipmentTypeCommercialGoods
16+
CustomsShipmentTypeCommercialSample
17+
CustomsShipmentTypeReturnedGoods
18+
)
19+
1020
type ParcelParams struct {
1121
Name string
1222
CompanyName string
@@ -26,6 +36,47 @@ type ParcelParams struct {
2636
Weight string
2737
OrderNumber string
2838
SenderID int64
39+
Items []CreateParcelItemRequest
40+
// The currency of the total order value. Validated against a format of
41+
// “XYZ” (ISO 4217).
42+
TotalOrderValueCurrency *string
43+
// The value paid by the buyer (via various payment methods supported by the
44+
// shop(cash on delivery, pre-paid or post-paid), it will also be used for
45+
// the cash on delivery amount for example “99.99”.
46+
TotalOrderValue *string
47+
// Shipping method name selected by buyer during the checkout
48+
ShippingMethodCheckoutName *string
49+
// Customs invoice number
50+
CustomsInvoiceNr *string
51+
// Customs shipment type
52+
CustomsShipmentType *CustomsShipmentType
53+
// When set to true configured shipping rules will be applied before creating the label and announcing the Parcel
54+
ApplyShippingRules *bool
55+
}
56+
57+
type CreateParcelItemRequest struct {
58+
// Harmonized System Code Wikipedia Link. Providing a complete HS code with 8 characters increases the delivery rate.
59+
HsCode string `json:"hs_code"`
60+
// Weight of a single item in kilograms.
61+
Weight string `json:"weight"`
62+
// Quantity of items shipped.
63+
Quantity int `json:"quantity"`
64+
// Description of the item.
65+
Description string `json:"description"`
66+
// ISO-2 code of the country where the items were originally produced. External Link.
67+
OriginCountry string `json:"origin_country,omitempty"`
68+
// Value of a single item.
69+
Value float64 `json:"value"`
70+
// The SKU of the product.
71+
SKU string `json:"sku,omitempty"`
72+
// External ID of the item generated by a shop system or similar.
73+
ItemId string `json:"item_id,omitempty"`
74+
// The list of properties of the product. Used as a JSON object with {‘key’: ‘value’}.
75+
Properties map[string]interface{} `json:"properties,omitempty"`
76+
}
77+
78+
type CreateParcelShipmentRequest struct {
79+
ID int64 `json:"id"`
2980
}
3081

3182
type Parcel struct {
@@ -60,26 +111,31 @@ type ParcelRequestContainer struct {
60111
}
61112

62113
type ParcelRequest struct {
63-
Name string `json:"name"`
64-
CompanyName string `json:"company_name"`
65-
Address string `json:"address"`
66-
Address2 string `json:"address_2"`
67-
HouseNumber string `json:"house_number"`
68-
City string `json:"city"`
69-
PostalCode string `json:"postal_code"`
70-
CountryState string `json:"country_state"`
71-
Country string `json:"country"`
72-
Weight string `json:"weight,omitempty"`
73-
Telephone string `json:"telephone"`
74-
Email string `json:"email"`
75-
RequestLabel bool `json:"request_label"`
76-
ToServicePointID *int64 `json:"to_service_point,omitempty"`
77-
OrderNumber string `json:"order_number"`
78-
ExternalID *string `json:"external_reference,omitempty"`
79-
SenderID *int64 `json:"sender_address,omitempty"`
80-
Shipment struct {
81-
ID int64 `json:"id"`
82-
} `json:"shipment"`
114+
Name string `json:"name"`
115+
CompanyName string `json:"company_name"`
116+
Address string `json:"address"`
117+
Address2 string `json:"address_2"`
118+
HouseNumber string `json:"house_number"`
119+
City string `json:"city"`
120+
PostalCode string `json:"postal_code"`
121+
CountryState string `json:"country_state"`
122+
Country string `json:"country"`
123+
Weight string `json:"weight,omitempty"`
124+
Telephone string `json:"telephone"`
125+
Email string `json:"email"`
126+
RequestLabel bool `json:"request_label"`
127+
ToServicePointID *int64 `json:"to_service_point,omitempty"`
128+
OrderNumber string `json:"order_number"`
129+
ExternalID *string `json:"external_reference,omitempty"`
130+
SenderID *int64 `json:"sender_address,omitempty"`
131+
Shipment *CreateParcelShipmentRequest `json:"shipment,omitempty"`
132+
Items []CreateParcelItemRequest `json:"parcel_items,omitempty"`
133+
TotalOrderValueCurrency *string `json:"total_order_value_currency,omitempty"`
134+
TotalOrderValue *string `json:"total_order_value,omitempty"`
135+
ShippingMethodCheckoutName *string `json:"shipping_method_checkout_name,omitempty"`
136+
CustomsInvoiceNr *string `json:"customs_invoice_nr,omitempty"`
137+
CustomsShipmentType *CustomsShipmentType `json:"customs_shipment_type,omitempty"`
138+
ApplyShippingRules *bool `json:"apply_shipping_rules,omitempty"`
83139
}
84140

85141
type LabelResponseContainer struct {
@@ -156,27 +212,33 @@ type Status struct {
156212
Message string `json:"message"`
157213
}
158214

159-
//Translate the params into an actual request body
215+
// Translate the params into an actual request body
160216
func (p *ParcelParams) GetPayload() interface{} {
161217
parcel := ParcelRequest{
162-
Name: p.Name,
163-
CompanyName: p.CompanyName,
164-
Address: p.Street,
165-
Address2: p.AdditionalInfo,
166-
HouseNumber: p.HouseNumber,
167-
City: p.City,
168-
PostalCode: p.PostalCode,
169-
CountryState: p.State,
170-
Country: p.CountryCode,
171-
Telephone: p.PhoneNumber,
172-
Email: p.EmailAddress,
173-
RequestLabel: p.IsLabelRequested,
174-
Shipment: struct {
175-
ID int64 `json:"id"`
176-
}{
177-
ID: p.Method,
178-
},
218+
Name: p.Name,
219+
CompanyName: p.CompanyName,
220+
Address: p.Street,
221+
Address2: p.AdditionalInfo,
222+
HouseNumber: p.HouseNumber,
223+
City: p.City,
224+
PostalCode: p.PostalCode,
225+
CountryState: p.State,
226+
Country: p.CountryCode,
227+
Telephone: p.PhoneNumber,
228+
Email: p.EmailAddress,
229+
RequestLabel: p.IsLabelRequested,
230+
Items: p.Items,
231+
TotalOrderValueCurrency: p.TotalOrderValueCurrency,
232+
TotalOrderValue: p.TotalOrderValue,
233+
ShippingMethodCheckoutName: p.ShippingMethodCheckoutName,
234+
CustomsInvoiceNr: p.CustomsInvoiceNr,
235+
CustomsShipmentType: p.CustomsShipmentType,
236+
ApplyShippingRules: p.ApplyShippingRules,
179237
}
238+
if p.Method != 0 {
239+
parcel.Shipment = &CreateParcelShipmentRequest{ID: p.Method}
240+
}
241+
180242
if p.SenderID != 0 {
181243
parcel.SenderID = &p.SenderID
182244
}
@@ -197,7 +259,7 @@ func (p *ParcelParams) GetPayload() interface{} {
197259
return ar
198260
}
199261

200-
//Handle the response and return it as a Parcel{}
262+
// Handle the response and return it as a Parcel{}
201263
func (p *ParcelResponseContainer) GetResponse() interface{} {
202264
parcel := Parcel{
203265
ID: p.Parcel.ID,
@@ -232,7 +294,7 @@ func (p *ParcelResponseContainer) GetResponse() interface{} {
232294
return &parcel
233295
}
234296

235-
//Set the response
297+
// Set the response
236298
func (p *ParcelResponseContainer) SetResponse(body []byte) error {
237299
err := json.Unmarshal(body, &p)
238300
if err != nil {
@@ -241,12 +303,12 @@ func (p *ParcelResponseContainer) SetResponse(body []byte) error {
241303
return nil
242304
}
243305

244-
//Get formatted response
306+
// Get formatted response
245307
func (l LabelData) GetResponse() interface{} {
246308
return l
247309
}
248310

249-
//Set the response
311+
// Set the response
250312
func (l *LabelData) SetResponse(body []byte) error {
251313
*l = body
252314
return nil

sendcloud.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ func Request(method string, uri string, payload Payload, apiKey string, apiSecre
7878
}
7979

8080
if response.StatusCode > 299 || response.StatusCode < 200 {
81-
//Return error response
81+
if !strings.Contains(response.Header.Get("content-type"), "application/json") {
82+
return &Error{
83+
Code: response.StatusCode,
84+
Message: string(body),
85+
}
86+
}
87+
88+
// Return error response
8289
errResponse := ErrorResponse{}
8390
err = json.Unmarshal(body, &errResponse)
8491
if err != nil {

0 commit comments

Comments
 (0)