sendcloud

package module
v0.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 9 Imported by: 0

README

build

sendcloud-go

An API-client for Sendcloud written in Golang.

Overview GoDoc

This package currently supports:

  • parcels
  • parcel documents
  • labels
  • methods
  • addresses
  • integrations

Install

go get github.com/afosto/sendcloud-go

Examples

Some examples on how to use the client are found below.

To initialize the client:

api := client.API{}
api.Init("api_key", "api_secret")

To list shipping methods:

methods, err := api.Method.GetMethods()
if err != nil {
    log.Fatal(err)
}

for _, m := range methods {
    log.Print(*m)
}

Create a parcel:

params := &sendcloud.ParcelParams{
	Name:             "Sendcloud-GO",
	CompanyName:      "Afosto SaaS BV",
	Street:           "Grondzijl",
	HouseNumber:      "16",
	City:             "Groningen",
	PostalCode:       "9731DG",
	PhoneNumber:      "0507119519",
	EmailAddress:     "peter@afosto.io",
	CountryCode:      "NL",
	IsLabelRequested: true,
	Method:           8,
	ExternalID:       uuid.New().String(),
}
parcel, err := api.Parcel.New(params)

resolve a service point id from postnl,dhl,dpd to a sendcloud service point id

spid, err := api.ServicePoint.GetServicePoint(servicepoint.Matcher{
    SPID:        "NL-972301",
    Carrier:     "dhl",
    Country:     "NL",
    PostalCode:  "9731AR",
    HouseNumber: "41",
    Latitude:    53.226994,
    Longitude:   6.608120,
})

fmt.PrintLn(spid) 

this service point id can then be placed on an parcel on creation

Contributing

All contributions / suggestions are welcome. Please raise an issue or submit a PR.

Thanks

This package was heavily inspired by stripe-go.

Author

This package is developed by Afosto SaaS BV.

License

MIT.

Documentation

Overview

Sendcloud-go is an api client for Sendcloud

It currently supports parcels, labels, integrations, methods and sender addresses and will make interaction easier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequest added in v0.4.5

func NewRequest(ctx context.Context, method, uri string, payload Payload, apiKey, apiSecret string) (*http.Request, error)

NewRequest creates and prepares a *http.Request with the given method, url, payload and credentials, so it's ready to be sent to Sendcloud.

func Request

func Request(method, uri string, payload Payload, apiKey, apiSecret string, r Response) error

Request sends a request to Sendcloud with given method, path, payload and credentials.

func ValidateResponse added in v0.4.5

func ValidateResponse(response *http.Response) error

ValidateResponse validates a received response from Sendcloud. It is valid and returns nil when the status code is between 200 and 299.

Types

type AddressDivided

type AddressDivided struct {
	Street      string `json:"street"`
	HouseNumber string `json:"house_number"`
}

type Carrier

type Carrier struct {
	Code string `json:"code"`
}

type Country

type Country struct {
	Code   string
	Amount int64
}

type CountryResponse

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"`
	LeadTimeHours *float64 `json:"lead_time_hours"`
}

type CreateParcelItemRequest added in v0.4.0

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 added in v0.4.0

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

type CustomsShipmentType added in v0.4.0

type CustomsShipmentType int
const (
	CustomsShipmentTypeGift CustomsShipmentType = iota
	CustomsShipmentTypeDocuments
	CustomsShipmentTypeCommercialGoods
	CustomsShipmentTypeCommercialSample
	CustomsShipmentTypeReturnedGoods
)

type Document added in v0.4.5

type Document struct {
	Format DocumentFormat
	Body   []byte
}

Document represents a document file that can be downloaded from the api.

type DocumentFormat added in v0.4.5

type DocumentFormat string

DocumentFormat is any of the formats a Document can be in.

const (
	DocumentPdf DocumentFormat = "application/pdf"
	DocumentZpl DocumentFormat = "application/zpl"
	DocumentPng DocumentFormat = "image/png"
)

func (DocumentFormat) Name added in v0.4.5

func (df DocumentFormat) Name() string

func (DocumentFormat) String added in v0.4.5

func (df DocumentFormat) String() string

type Error

type Error struct {
	Code    int    `json:"code"`
	Request string `json:"request"`
	Message string `json:"message"`
}

func (*Error) Error

func (e *Error) Error() string

type ErrorResponse

type ErrorResponse struct {
	Error struct {
		Code    int    `json:"code"`
		Request string `json:"request"`
		Message string `json:"message"`
	} `json:"error"`
}

type Integration

type Integration struct {
	ID                    int64
	Name                  string
	URL                   string
	Type                  string
	IsServicePointEnabled bool
	ServicePointCarriers  []string
	IsWebhooksEnabled     bool
	WebhookURL            string
	UpdatedAt             time.Time
}

type IntegrationListResponseContainer

type IntegrationListResponseContainer []IntegrationResponseContainer

func (*IntegrationListResponseContainer) GetResponse

func (i *IntegrationListResponseContainer) GetResponse() interface{}

Get formatted response

func (*IntegrationListResponseContainer) SetResponse

func (i *IntegrationListResponseContainer) SetResponse(body []byte) error

Set the response

type IntegrationParams

type IntegrationParams struct {
	ID                int64
	Name              string
	URL               string
	IsWebhooksEnabled bool
	WebhookURL        string
}

func (*IntegrationParams) GetPayload

func (i *IntegrationParams) GetPayload() interface{}

type IntegrationRequest

type IntegrationRequest struct {
	ShopName             string   `json:"shop_name"`
	ShopURL              string   `json:"shop_url"`
	ServicePointEnabled  bool     `json:"service_point_enabled"`
	ServicePointCarriers []string `json:"service_point_carriers"`
	WebhookActive        bool     `json:"webhook_active"`
	WebhookURL           string   `json:"webhook_url"`
}

type IntegrationResponseContainer

type IntegrationResponseContainer struct {
	ID                   int64     `json:"id"`
	ShopName             string    `json:"shop_name"`
	ShopURL              string    `json:"shop_url"`
	System               string    `json:"system"`
	FailingSince         string    `json:"failing_since"`
	LastFetch            string    `json:"last_fetch"`
	LastUpdatedAt        time.Time `json:"last_updated_at"`
	ServicePointEnabled  bool      `json:"service_point_enabled"`
	ServicePointCarriers []string  `json:"service_point_carriers"`
	WebhookActive        bool      `json:"webhook_active"`
	WebhookURL           string    `json:"webhook_url"`
}

func (*IntegrationResponseContainer) GetResponse

func (r *IntegrationResponseContainer) GetResponse() interface{}

Get formatted response

func (*IntegrationResponseContainer) SetResponse

func (r *IntegrationResponseContainer) SetResponse(body []byte) error

Set the response

type LabelData

type LabelData []byte

func (LabelData) GetResponse

func (l LabelData) GetResponse() interface{}

Get formatted response

func (*LabelData) SetResponse

func (l *LabelData) SetResponse(body []byte) error

Set the response

type LabelResponse

type LabelResponse struct {
	NormalPrinter []string `json:"normal_printer"`
	LabelPrinter  string   `json:"label_printer"`
}

type LabelResponseContainer

type LabelResponseContainer struct {
	Label LabelResponse `json:"label"`
}

type Method

type Method struct {
	ID             int64
	Name           string
	CarrierCode    string
	IsServicePoint bool
	Amount         int64
	MinWeight      int64
	MaxWeight      int64
	Countries      []Country
}

type MethodListResponseContainer

type MethodListResponseContainer struct {
	ShippingMethods []MethodResponse `json:"shipping_methods"`
}

func (*MethodListResponseContainer) GetResponse

func (a *MethodListResponseContainer) GetResponse() interface{}

func (*MethodListResponseContainer) SetResponse

func (a *MethodListResponseContainer) SetResponse(body []byte) error

Set the response

type MethodResponse

type MethodResponse struct {
	ServicePointInput string            `json:"service_point_input"`
	MaxWeight         string            `json:"max_weight"`
	Name              string            `json:"name"`
	Carrier           string            `json:"carrier"`
	Countries         []CountryResponse `json:"countries"`
	MinWeight         string            `json:"min_weight"`
	ID                int64             `json:"id"`
	Price             float64           `json:"price"`
}

func (*MethodResponse) ToMethod

func (sm *MethodResponse) ToMethod() *Method

Parse methods to a stricter format

type MethodResponseContainer

type MethodResponseContainer struct {
	ShippingMethod MethodResponse `json:"shipping_method"`
}

func (*MethodResponseContainer) GetResponse

func (m *MethodResponseContainer) GetResponse() interface{}

Get formatted response

func (*MethodResponseContainer) SetResponse

func (m *MethodResponseContainer) SetResponse(body []byte) error

Set the response

type Parcel

type Parcel struct {
	ID             int64       `json:"id"`
	ExternalID     *string     `json:"external_id"`
	Name           string      `json:"name"`
	CompanyName    string      `json:"company_name"`
	Email          string      `json:"email"`
	Street         string      `json:"street"`
	HouseNumber    string      `json:"house_number"`
	Address        string      `json:"address"`
	Address2       string      `json:"address_2"`
	City           string      `json:"city"`
	PostalCode     string      `json:"postal_code"`
	CountryCode    string      `json:"country_code"`
	Method         int64       `json:"method"`
	PhoneNumber    *string     `json:"phone_number"`
	TrackingNumber string      `json:"tracking_number"`
	TrackingUrl    string      `json:"tracking_url"`
	ServicePointID *int64      `json:"to_service_point"`
	Weight         string      `json:"weight"`
	Label          string      `json:"label"`
	OrderNumber    string      `json:"order_number"`
	IsReturn       bool        `json:"is_return"`
	Note           *string     `json:"note"`
	CarrierCode    string      `json:"carrier"`
	Data           interface{} `json:"data"`
	CreatedAt      time.Time   `json:"created_at"`
}

type ParcelListResponseContainer

type ParcelListResponseContainer struct {
	Parcels []*ParcelResponse `json:"parcel"`
}

type ParcelParams

type ParcelParams struct {
	Name             string
	CompanyName      string
	Street           string
	HouseNumber      string
	City             string
	PostalCode       string
	State            string
	CountryCode      string
	AdditionalInfo   string
	IsLabelRequested bool
	Method           int64
	EmailAddress     string
	PhoneNumber      string
	ExternalID       string
	ToServicePointID int64
	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
}

func (*ParcelParams) GetPayload

func (p *ParcelParams) GetPayload() interface{}

Translate the params into an actual request body

type ParcelRequest

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                   *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 ParcelRequestContainer

type ParcelRequestContainer struct {
	Parcel ParcelRequest `json:"parcel"`
}

type ParcelResponse

type ParcelResponse struct {
	ID                  int64           `json:"id"`
	Address             string          `json:"address"`
	Address2            string          `json:"address_2"`
	AddressDivided      AddressDivided  `json:"address_divided"`
	City                string          `json:"city"`
	CompanyName         string          `json:"company_name"`
	Country             CountryResponse `json:"country"`
	Data                interface{}     `json:"data"`
	DateCreated         string          `json:"date_created"`
	Email               string          `json:"email"`
	Name                string          `json:"name"`
	PostalCode          string          `json:"postal_code"`
	Reference           string          `json:"reference"`
	Shipment            Shipment        `json:"shipment"`
	Status              Status          `json:"status"`
	ToServicePointID    *int64          `json:"to_service_point"`
	Telephone           *string         `json:"telephone"`
	TrackingNumber      string          `json:"tracking_number"`
	TrackingUrl         string          `json:"tracking_url"`
	Weight              string          `json:"weight"`
	Label               LabelResponse   `json:"label"`
	OrderNumber         string          `json:"order_number"`
	InsuredValue        int64           `json:"insured_value"`
	TotalInsuredValue   int64           `json:"total_insured_value"`
	ToState             interface{}     `json:"to_state"`
	CustomsInvoiceNr    string          `json:"customs_invoice_nr"`
	CustomsShipmentType interface{}     `json:"customs_shipment_type"`
	Type                interface{}     `json:"type"`
	ShipmentUUID        *string         `json:"shipment_uuid"`
	ShippingMethod      int64           `json:"shipping_method"`
	ExternalOrderID     *string         `json:"external_order_id"`
	ExternalShipmentID  *string         `json:"external_shipment_id"`
	ExternalReference   *string         `json:"external_reference"`
	IsReturn            bool            `json:"is_return"`
	Note                *string         `json:"note"`
	Carrier             Carrier         `json:"carrier"`
}

type ParcelResponseContainer

type ParcelResponseContainer struct {
	Parcel ParcelResponse `json:"parcel"`
}

func (*ParcelResponseContainer) GetResponse

func (p *ParcelResponseContainer) GetResponse() interface{}

Handle the response and return it as a Parcel{}

func (*ParcelResponseContainer) SetResponse

func (p *ParcelResponseContainer) SetResponse(body []byte) error

Set the response

type Payload

type Payload interface {
	GetPayload() interface{}
}

type Response

type Response interface {
	GetResponse() interface{}
	SetResponse(body []byte) error
}

type Sender

type Sender struct {
	ID          int64
	CompanyName string
	Email       string
	PhoneNumber string
	Street      string
	HouseNumber string
	PostCode    string
	City        string
	CountryCode string
}

type SenderResponse

type SenderResponse struct {
	ID          int64  `json:"id"`
	CompanyName string `json:"company_name"`
	ContactName string `json:"contact_name"`
	Email       string `json:"email"`
	Telephone   string `json:"telephone"`
	Street      string `json:"street"`
	HouseNumber string `json:"house_number"`
	PostalBox   string `json:"postal_box"`
	PostalCode  string `json:"postal_code"`
	City        string `json:"city"`
	Country     string `json:"country"`
}

type SenderResponseContainer

type SenderResponseContainer struct {
	SenderAddresses []SenderResponse `json:"sender_addresses"`
}

func (*SenderResponseContainer) GetResponse

func (a *SenderResponseContainer) GetResponse() interface{}

Get formatted response

func (*SenderResponseContainer) SetResponse

func (a *SenderResponseContainer) SetResponse(body []byte) error

Set the response

type ServicePoint

type ServicePoint struct {
	ID                    int         `json:"id"`
	Code                  string      `json:"code"`
	IsActive              bool        `json:"is_active"`
	ExtraData             interface{} `json:"extra_data"`
	Name                  string      `json:"name"`
	Street                string      `json:"street"`
	HouseNumber           string      `json:"house_number"`
	PostalCode            string      `json:"postal_code"`
	City                  string      `json:"city"`
	Latitude              string      `json:"latitude"`
	Longitude             string      `json:"longitude"`
	Email                 string      `json:"email"`
	Phone                 string      `json:"phone"`
	Homepage              string      `json:"homepage"`
	Carrier               string      `json:"carrier"`
	Country               string      `json:"country"`
	FormattedOpeningTimes struct {
		Num0 []string `json:"0"`
		Num1 []string `json:"1"`
		Num2 []string `json:"2"`
		Num3 []string `json:"3"`
		Num4 []string `json:"4"`
		Num5 []string `json:"5"`
		Num6 []string `json:"6"`
	} `json:"formatted_opening_times"`
	OpenTomorrow bool `json:"open_tomorrow"`
}

func (*ServicePoint) Identifier

func (s *ServicePoint) Identifier() string

type ServicePointList

type ServicePointList []ServicePoint

func (ServicePointList) GetResponse

func (s ServicePointList) GetResponse() interface{}

func (*ServicePointList) SetResponse

func (s *ServicePointList) SetResponse(body []byte) error

type Shipment

type Shipment struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
}

type Status

type Status struct {
	ID      int    `json:"id"`
	Message string `json:"message"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL