Skip to content

Commit

Permalink
Merge pull request #12 from byronwilliams/bw-line-items
Browse files Browse the repository at this point in the history
feat: specify parcel line items
  • Loading branch information
roelofjan-elsinga authored Jan 31, 2023
2 parents d659a47 + 5fda012 commit d53648c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 48 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 6 additions & 5 deletions method.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ type MethodResponse struct {
}

type CountryResponse struct {
Iso2 string `json:"iso_2"`
Iso3 string `json:"iso_3"`
ID int `json:"id"`
Price float64 `json:"price"`
Name string `json:"name"`
Iso2 string `json:"iso_2"`
Iso3 string `json:"iso_3"`
ID int `json:"id"`
Price float64 `json:"price"`
Name string `json:"name"`
LeadTimeHours *float64 `json:"lead_time_hours"`
}

//Get formatted response
Expand Down
146 changes: 104 additions & 42 deletions parcel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (

type LabelData []byte

type CustomsShipmentType int

const (
CustomsShipmentTypeGift = iota
CustomsShipmentTypeDocuments
CustomsShipmentTypeCommercialGoods
CustomsShipmentTypeCommercialSample
CustomsShipmentTypeReturnedGoods
)

type ParcelParams struct {
Name string
CompanyName string
Expand All @@ -26,6 +36,47 @@ type ParcelParams struct {
Weight string
OrderNumber string
SenderID int64
Items []CreateParcelItemRequest
// The currency of the total order value. Validated against a format of
// “XYZ” (ISO 4217).
TotalOrderValueCurrency *string
// The value paid by the buyer (via various payment methods supported by the
// shop(cash on delivery, pre-paid or post-paid), it will also be used for
// the cash on delivery amount for example “99.99”.
TotalOrderValue *string
// Shipping method name selected by buyer during the checkout
ShippingMethodCheckoutName *string
// Customs invoice number
CustomsInvoiceNr *string
// Customs shipment type
CustomsShipmentType *CustomsShipmentType
// When set to true configured shipping rules will be applied before creating the label and announcing the Parcel
ApplyShippingRules *bool
}

type CreateParcelItemRequest struct {
// Harmonized System Code Wikipedia Link. Providing a complete HS code with 8 characters increases the delivery rate.
HsCode string `json:"hs_code"`
// Weight of a single item in kilograms.
Weight string `json:"weight"`
// Quantity of items shipped.
Quantity int `json:"quantity"`
// Description of the item.
Description string `json:"description"`
// ISO-2 code of the country where the items were originally produced. External Link.
OriginCountry string `json:"origin_country,omitempty"`
// Value of a single item.
Value float64 `json:"value"`
// The SKU of the product.
SKU string `json:"sku,omitempty"`
// External ID of the item generated by a shop system or similar.
ItemId string `json:"item_id,omitempty"`
// The list of properties of the product. Used as a JSON object with {‘key’: ‘value’}.
Properties map[string]interface{} `json:"properties,omitempty"`
}

type CreateParcelShipmentRequest struct {
ID int64 `json:"id"`
}

type Parcel struct {
Expand Down Expand Up @@ -60,26 +111,31 @@ type ParcelRequestContainer struct {
}

type ParcelRequest struct {
Name string `json:"name"`
CompanyName string `json:"company_name"`
Address string `json:"address"`
Address2 string `json:"address_2"`
HouseNumber string `json:"house_number"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
CountryState string `json:"country_state"`
Country string `json:"country"`
Weight string `json:"weight,omitempty"`
Telephone string `json:"telephone"`
Email string `json:"email"`
RequestLabel bool `json:"request_label"`
ToServicePointID *int64 `json:"to_service_point,omitempty"`
OrderNumber string `json:"order_number"`
ExternalID *string `json:"external_reference,omitempty"`
SenderID *int64 `json:"sender_address,omitempty"`
Shipment struct {
ID int64 `json:"id"`
} `json:"shipment"`
Name string `json:"name"`
CompanyName string `json:"company_name"`
Address string `json:"address"`
Address2 string `json:"address_2"`
HouseNumber string `json:"house_number"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
CountryState string `json:"country_state"`
Country string `json:"country"`
Weight string `json:"weight,omitempty"`
Telephone string `json:"telephone"`
Email string `json:"email"`
RequestLabel bool `json:"request_label"`
ToServicePointID *int64 `json:"to_service_point,omitempty"`
OrderNumber string `json:"order_number"`
ExternalID *string `json:"external_reference,omitempty"`
SenderID *int64 `json:"sender_address,omitempty"`
Shipment *CreateParcelShipmentRequest `json:"shipment,omitempty"`
Items []CreateParcelItemRequest `json:"parcel_items,omitempty"`
TotalOrderValueCurrency *string `json:"total_order_value_currency,omitempty"`
TotalOrderValue *string `json:"total_order_value,omitempty"`
ShippingMethodCheckoutName *string `json:"shipping_method_checkout_name,omitempty"`
CustomsInvoiceNr *string `json:"customs_invoice_nr,omitempty"`
CustomsShipmentType *CustomsShipmentType `json:"customs_shipment_type,omitempty"`
ApplyShippingRules *bool `json:"apply_shipping_rules,omitempty"`
}

type LabelResponseContainer struct {
Expand Down Expand Up @@ -156,27 +212,33 @@ type Status struct {
Message string `json:"message"`
}

//Translate the params into an actual request body
// Translate the params into an actual request body
func (p *ParcelParams) GetPayload() interface{} {
parcel := ParcelRequest{
Name: p.Name,
CompanyName: p.CompanyName,
Address: p.Street,
Address2: p.AdditionalInfo,
HouseNumber: p.HouseNumber,
City: p.City,
PostalCode: p.PostalCode,
CountryState: p.State,
Country: p.CountryCode,
Telephone: p.PhoneNumber,
Email: p.EmailAddress,
RequestLabel: p.IsLabelRequested,
Shipment: struct {
ID int64 `json:"id"`
}{
ID: p.Method,
},
Name: p.Name,
CompanyName: p.CompanyName,
Address: p.Street,
Address2: p.AdditionalInfo,
HouseNumber: p.HouseNumber,
City: p.City,
PostalCode: p.PostalCode,
CountryState: p.State,
Country: p.CountryCode,
Telephone: p.PhoneNumber,
Email: p.EmailAddress,
RequestLabel: p.IsLabelRequested,
Items: p.Items,
TotalOrderValueCurrency: p.TotalOrderValueCurrency,
TotalOrderValue: p.TotalOrderValue,
ShippingMethodCheckoutName: p.ShippingMethodCheckoutName,
CustomsInvoiceNr: p.CustomsInvoiceNr,
CustomsShipmentType: p.CustomsShipmentType,
ApplyShippingRules: p.ApplyShippingRules,
}
if p.Method != 0 {
parcel.Shipment = &CreateParcelShipmentRequest{ID: p.Method}
}

if p.SenderID != 0 {
parcel.SenderID = &p.SenderID
}
Expand All @@ -197,7 +259,7 @@ func (p *ParcelParams) GetPayload() interface{} {
return ar
}

//Handle the response and return it as a Parcel{}
// Handle the response and return it as a Parcel{}
func (p *ParcelResponseContainer) GetResponse() interface{} {
parcel := Parcel{
ID: p.Parcel.ID,
Expand Down Expand Up @@ -232,7 +294,7 @@ func (p *ParcelResponseContainer) GetResponse() interface{} {
return &parcel
}

//Set the response
// Set the response
func (p *ParcelResponseContainer) SetResponse(body []byte) error {
err := json.Unmarshal(body, &p)
if err != nil {
Expand All @@ -241,12 +303,12 @@ func (p *ParcelResponseContainer) SetResponse(body []byte) error {
return nil
}

//Get formatted response
// Get formatted response
func (l LabelData) GetResponse() interface{} {
return l
}

//Set the response
// Set the response
func (l *LabelData) SetResponse(body []byte) error {
*l = body
return nil
Expand Down
9 changes: 8 additions & 1 deletion sendcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ func Request(method string, uri string, payload Payload, apiKey string, apiSecre
}

if response.StatusCode > 299 || response.StatusCode < 200 {
//Return error response
if !strings.Contains(response.Header.Get("content-type"), "application/json") {
return &Error{
Code: response.StatusCode,
Message: string(body),
}
}

// Return error response
errResponse := ErrorResponse{}
err = json.Unmarshal(body, &errResponse)
if err != nil {
Expand Down

0 comments on commit d53648c

Please sign in to comment.