Skip to content

Commit 5f77985

Browse files
Merge pull request #16 from afosto/feature/return-labels
feature/return-labels
2 parents 1061427 + 43b3593 commit 5f77985

File tree

7 files changed

+380
-4
lines changed

7 files changed

+380
-4
lines changed

client/api.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"github.com/afosto/sendcloud-go/integration"
55
"github.com/afosto/sendcloud-go/method"
66
"github.com/afosto/sendcloud-go/parcel"
7+
"github.com/afosto/sendcloud-go/products"
8+
"github.com/afosto/sendcloud-go/returns"
79
"github.com/afosto/sendcloud-go/sender"
810
"github.com/afosto/sendcloud-go/servicepoint"
911
)
@@ -14,13 +16,17 @@ type API struct {
1416
Sender *sender.Client
1517
ServicePoint *servicepoint.Client
1618
Integration *integration.Client
19+
Return *returns.Client
20+
Product *products.Client
1721
}
1822

19-
//Initialize the client
23+
// Initialize the client
2024
func (a *API) Init(apiKey string, apiSecret string) {
2125
a.Parcel = parcel.New(apiKey, apiSecret)
2226
a.Method = method.New(apiKey, apiSecret)
2327
a.Sender = sender.New(apiKey, apiSecret)
2428
a.ServicePoint = servicepoint.New(apiKey, apiSecret)
2529
a.Integration = integration.New(apiKey, apiSecret)
30+
a.Return = returns.New(apiKey, apiSecret)
31+
a.Product = products.New(apiKey, apiSecret)
2632
}

method/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func New(apiKey string, apiSecret string) *Client {
1717
}
1818
}
1919

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

30-
//Get a single method
30+
func (c *Client) GetReturnMethods() ([]*sendcloud.Method, error) {
31+
smr := sendcloud.MethodListResponseContainer{}
32+
err := sendcloud.Request("GET", "/api/v2/shipping_methods?is_return=true", nil, c.apiKey, c.apiSecret, &smr)
33+
if err != nil {
34+
return nil, err
35+
}
36+
return smr.GetResponse().([]*sendcloud.Method), nil
37+
}
38+
39+
// Get a single method
3140
func (c *Client) GetMethod(id int64) (*sendcloud.Method, error) {
3241
mr := sendcloud.MethodResponseContainer{}
3342
err := sendcloud.Request("GET", "/api/v2/shipping_methods/"+strconv.Itoa(int(id)), nil, c.apiKey, c.apiSecret, &mr)

parcel.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type Parcel struct {
9898
TrackingUrl string `json:"tracking_url"`
9999
ServicePointID *int64 `json:"to_service_point"`
100100
Weight string `json:"weight"`
101+
Status Status `json:"status"`
101102
Label string `json:"label"`
102103
OrderNumber string `json:"order_number"`
103104
IsReturn bool `json:"is_return"`
@@ -261,7 +262,7 @@ func (p *ParcelParams) GetPayload() interface{} {
261262
return ar
262263
}
263264

264-
// Handle the response and return it as a Parcel{}
265+
// Handle the response and returns it as a Parcel{}
265266
func (p *ParcelResponseContainer) GetResponse() interface{} {
266267
parcel := Parcel{
267268
ID: p.Parcel.ID,
@@ -274,6 +275,7 @@ func (p *ParcelResponseContainer) GetResponse() interface{} {
274275
Address: p.Parcel.Address,
275276
Address2: p.Parcel.Address2,
276277
City: p.Parcel.City,
278+
Status: p.Parcel.Status,
277279
Method: p.Parcel.Shipment.ID,
278280
PostalCode: p.Parcel.PostalCode,
279281
CountryCode: p.Parcel.Country.Iso2,

product.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sendcloud
2+
3+
import "encoding/json"
4+
5+
type Product struct {
6+
Name string `json:"name"`
7+
Code string `json:"code"`
8+
Carrier string `json:"carrier"`
9+
ServicePointsCarrier string `json:"service_points_carrier"`
10+
WeightRange struct {
11+
MinWeight int `json:"min_weight"`
12+
MaxWeight int `json:"max_weight"`
13+
} `json:"weight_range"`
14+
Methods []struct {
15+
Id int `json:"id"`
16+
Name string `json:"name"`
17+
ShippingProductCode string `json:"shipping_product_code"`
18+
Properties struct {
19+
MinWeight int `json:"min_weight"`
20+
MaxWeight int `json:"max_weight"`
21+
MaxDimensions struct {
22+
Length int `json:"length"`
23+
Width int `json:"width"`
24+
Height int `json:"height"`
25+
Unit string `json:"unit"`
26+
} `json:"max_dimensions"`
27+
} `json:"properties"`
28+
LeadTimeHours struct {
29+
NL struct {
30+
NL int `json:"NL"`
31+
} `json:"NL"`
32+
} `json:"lead_time_hours"`
33+
} `json:"methods"`
34+
}
35+
36+
type ProductListResponseContainer struct {
37+
Products []Product `json:"products"`
38+
}
39+
40+
func (p *ProductListResponseContainer) SetResponse(body []byte) error {
41+
return json.Unmarshal(body, &p.Products)
42+
}
43+
44+
type ProductResponseContainer struct {
45+
Product Product `json:"product"`
46+
}
47+
48+
func (p *ProductResponseContainer) GetResponse() interface{} {
49+
return p.Product
50+
}
51+
52+
func (p *ProductListResponseContainer) GetResponse() interface{} {
53+
var products []*Product
54+
for i := range p.Products {
55+
products = append(products, &p.Products[i])
56+
}
57+
58+
return products
59+
}

products/client.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package products
2+
3+
import (
4+
"github.com/afosto/sendcloud-go"
5+
"net/url"
6+
)
7+
8+
type Client struct {
9+
apiKey string
10+
apiSecret string
11+
}
12+
13+
func New(apiKey string, apiSecret string) *Client {
14+
return &Client{
15+
apiKey: apiKey,
16+
apiSecret: apiSecret,
17+
}
18+
}
19+
20+
// get shipping products
21+
func (c *Client) GetShippingProducts(fromCountry string) ([]*sendcloud.Product, error) {
22+
smr := sendcloud.ProductListResponseContainer{}
23+
values := url.Values{}
24+
values.Set("from_country", fromCountry)
25+
reqUrl := "/api/v2/shipping-products?" + values.Encode()
26+
err := sendcloud.Request("GET", reqUrl, nil, c.apiKey, c.apiSecret, &smr)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return smr.GetResponse().([]*sendcloud.Product), nil
31+
}
32+
33+
// get return shipping products
34+
func (c *Client) GetReturnShippingProducts(fromCountry string) ([]*sendcloud.Product, error) {
35+
smr := sendcloud.ProductListResponseContainer{}
36+
values := url.Values{}
37+
values.Set("from_country", fromCountry)
38+
values.Set("returns", "true")
39+
reqUrl := "/api/v2/shipping-products?" + values.Encode()
40+
err := sendcloud.Request("GET", reqUrl, nil, c.apiKey, c.apiSecret, &smr)
41+
if err != nil {
42+
return nil, err
43+
}
44+
return smr.GetResponse().([]*sendcloud.Product), nil
45+
}

0 commit comments

Comments
 (0)