Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/afosto/sendcloud-go/integration"
"github.com/afosto/sendcloud-go/method"
"github.com/afosto/sendcloud-go/parcel"
"github.com/afosto/sendcloud-go/products"
"github.com/afosto/sendcloud-go/returns"
"github.com/afosto/sendcloud-go/sender"
"github.com/afosto/sendcloud-go/servicepoint"
)
Expand All @@ -14,13 +16,17 @@ type API struct {
Sender *sender.Client
ServicePoint *servicepoint.Client
Integration *integration.Client
Return *returns.Client
Product *products.Client
}

//Initialize the client
// Initialize the client
func (a *API) Init(apiKey string, apiSecret string) {
a.Parcel = parcel.New(apiKey, apiSecret)
a.Method = method.New(apiKey, apiSecret)
a.Sender = sender.New(apiKey, apiSecret)
a.ServicePoint = servicepoint.New(apiKey, apiSecret)
a.Integration = integration.New(apiKey, apiSecret)
a.Return = returns.New(apiKey, apiSecret)
a.Product = products.New(apiKey, apiSecret)
}
13 changes: 11 additions & 2 deletions method/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func New(apiKey string, apiSecret string) *Client {
}
}

//Get all shipment methods
// Get all shipment methods
func (c *Client) GetMethods() ([]*sendcloud.Method, error) {
smr := sendcloud.MethodListResponseContainer{}
err := sendcloud.Request("GET", "/api/v2/shipping_methods", nil, c.apiKey, c.apiSecret, &smr)
Expand All @@ -27,7 +27,16 @@ func (c *Client) GetMethods() ([]*sendcloud.Method, error) {
return smr.GetResponse().([]*sendcloud.Method), nil
}

//Get a single method
func (c *Client) GetReturnMethods() ([]*sendcloud.Method, error) {
smr := sendcloud.MethodListResponseContainer{}
err := sendcloud.Request("GET", "/api/v2/shipping_methods?is_return=true", nil, c.apiKey, c.apiSecret, &smr)
if err != nil {
return nil, err
}
return smr.GetResponse().([]*sendcloud.Method), nil
}

// Get a single method
func (c *Client) GetMethod(id int64) (*sendcloud.Method, error) {
mr := sendcloud.MethodResponseContainer{}
err := sendcloud.Request("GET", "/api/v2/shipping_methods/"+strconv.Itoa(int(id)), nil, c.apiKey, c.apiSecret, &mr)
Expand Down
4 changes: 3 additions & 1 deletion parcel.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Parcel struct {
TrackingUrl string `json:"tracking_url"`
ServicePointID *int64 `json:"to_service_point"`
Weight string `json:"weight"`
Status Status `json:"status"`
Label string `json:"label"`
OrderNumber string `json:"order_number"`
IsReturn bool `json:"is_return"`
Expand Down Expand Up @@ -261,7 +262,7 @@ func (p *ParcelParams) GetPayload() interface{} {
return ar
}

// Handle the response and return it as a Parcel{}
// Handle the response and returns it as a Parcel{}
func (p *ParcelResponseContainer) GetResponse() interface{} {
parcel := Parcel{
ID: p.Parcel.ID,
Expand All @@ -274,6 +275,7 @@ func (p *ParcelResponseContainer) GetResponse() interface{} {
Address: p.Parcel.Address,
Address2: p.Parcel.Address2,
City: p.Parcel.City,
Status: p.Parcel.Status,
Method: p.Parcel.Shipment.ID,
PostalCode: p.Parcel.PostalCode,
CountryCode: p.Parcel.Country.Iso2,
Expand Down
59 changes: 59 additions & 0 deletions product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sendcloud

import "encoding/json"

type Product struct {
Name string `json:"name"`
Code string `json:"code"`
Carrier string `json:"carrier"`
ServicePointsCarrier string `json:"service_points_carrier"`
WeightRange struct {
MinWeight int `json:"min_weight"`
MaxWeight int `json:"max_weight"`
} `json:"weight_range"`
Methods []struct {
Id int `json:"id"`
Name string `json:"name"`
ShippingProductCode string `json:"shipping_product_code"`
Properties struct {
MinWeight int `json:"min_weight"`
MaxWeight int `json:"max_weight"`
MaxDimensions struct {
Length int `json:"length"`
Width int `json:"width"`
Height int `json:"height"`
Unit string `json:"unit"`
} `json:"max_dimensions"`
} `json:"properties"`
LeadTimeHours struct {
NL struct {
NL int `json:"NL"`
} `json:"NL"`
} `json:"lead_time_hours"`
} `json:"methods"`
}

type ProductListResponseContainer struct {
Products []Product `json:"products"`
}

func (p *ProductListResponseContainer) SetResponse(body []byte) error {
return json.Unmarshal(body, &p.Products)
}

type ProductResponseContainer struct {
Product Product `json:"product"`
}

func (p *ProductResponseContainer) GetResponse() interface{} {
return p.Product
}

func (p *ProductListResponseContainer) GetResponse() interface{} {
var products []*Product
for i := range p.Products {
products = append(products, &p.Products[i])
}

return products
}
45 changes: 45 additions & 0 deletions products/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package products

import (
"github.com/afosto/sendcloud-go"
"net/url"
)

type Client struct {
apiKey string
apiSecret string
}

func New(apiKey string, apiSecret string) *Client {
return &Client{
apiKey: apiKey,
apiSecret: apiSecret,
}
}

// get shipping products
func (c *Client) GetShippingProducts(fromCountry string) ([]*sendcloud.Product, error) {
smr := sendcloud.ProductListResponseContainer{}
values := url.Values{}
values.Set("from_country", fromCountry)
reqUrl := "/api/v2/shipping-products?" + values.Encode()
err := sendcloud.Request("GET", reqUrl, nil, c.apiKey, c.apiSecret, &smr)
if err != nil {
return nil, err
}
return smr.GetResponse().([]*sendcloud.Product), nil
}

// get return shipping products
func (c *Client) GetReturnShippingProducts(fromCountry string) ([]*sendcloud.Product, error) {
smr := sendcloud.ProductListResponseContainer{}
values := url.Values{}
values.Set("from_country", fromCountry)
values.Set("returns", "true")
reqUrl := "/api/v2/shipping-products?" + values.Encode()
err := sendcloud.Request("GET", reqUrl, nil, c.apiKey, c.apiSecret, &smr)
if err != nil {
return nil, err
}
return smr.GetResponse().([]*sendcloud.Product), nil
}
Loading