Skip to content

Commit f2dbf26

Browse files
feat: add optional sender address filter to GetMethods
1 parent e6e27ec commit f2dbf26

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

method/client.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package method
22

33
import (
4-
"github.com/afosto/sendcloud-go"
4+
"net/url"
55
"strconv"
6+
7+
"github.com/afosto/sendcloud-go"
68
)
79

810
type Client struct {
@@ -17,10 +19,38 @@ func New(apiKey string, apiSecret string) *Client {
1719
}
1820
}
1921

20-
// Get all shipment methods
21-
func (c *Client) GetMethods() ([]*sendcloud.Method, error) {
22+
type MethodOption func(*methodOptions)
23+
24+
type methodOptions struct {
25+
senderAddressID *int64
26+
}
27+
28+
func WithSenderAddress(id int64) MethodOption {
29+
return func(o *methodOptions) {
30+
o.senderAddressID = &id
31+
}
32+
}
33+
34+
// Get all shipment methods with optional filters
35+
func (c *Client) GetMethods(opts ...MethodOption) ([]*sendcloud.Method, error) {
36+
options := &methodOptions{}
37+
for _, opt := range opts {
38+
opt(options)
39+
}
40+
2241
smr := sendcloud.MethodListResponseContainer{}
23-
err := sendcloud.Request("GET", "/api/v2/shipping_methods", nil, c.apiKey, c.apiSecret, &smr)
42+
43+
values := url.Values{}
44+
if options.senderAddressID != nil {
45+
values.Set("sender_address", strconv.FormatInt(*options.senderAddressID, 10))
46+
}
47+
48+
reqURL := "/api/v2/shipping_methods"
49+
if len(values) > 0 {
50+
reqURL += "?" + values.Encode()
51+
}
52+
53+
err := sendcloud.Request("GET", reqURL, nil, c.apiKey, c.apiSecret, &smr)
2454
if err != nil {
2555
return nil, err
2656
}

0 commit comments

Comments
 (0)