-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
56 lines (52 loc) · 1.94 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Code generated by AfterShip SDK Generator. DO NOT EDIT.
package tracking
import (
"fmt"
"github.com/aftership/tracking-sdk-go/v6/component"
"github.com/aftership/tracking-sdk-go/v6/errorx"
"github.com/aftership/tracking-sdk-go/v6/service"
"net/url"
)
type Client struct {
options clientOptions
sender *component.HttpSender
Tracking *service.TrackingService
Courier *service.CourierService
EstimatedDeliveryDate *service.EstimatedDeliveryDateService
}
func New(opts ...ClientOption) (*Client, error) {
client := &Client{}
client.options = defaultClientOptions
for _, opt := range opts {
opt.apply(&client.options)
}
if client.options.timeoutMs < 0 {
return nil, errorx.NewSdkError(errorx.ErrInvalidOption, fmt.Sprintf("Invalid option: %s", "TIMEOUT"), "")
}
if client.options.maxRetry < 0 || client.options.maxRetry > 10 {
return nil, errorx.NewSdkError(errorx.ErrInvalidOption, fmt.Sprintf("Invalid option: %s", "MAX_RETRY"), "")
}
if len(client.options.apiKey) == 0 {
return nil, errorx.NewSdkError(errorx.ErrInvalidApiKey, errorx.GetErrorMessage(errorx.ErrInvalidApiKey), "")
}
if len(client.options.proxy) > 0 {
_, err := url.Parse(client.options.proxy)
if err != nil {
return nil, errorx.NewSdkError(errorx.ErrInvalidOption, fmt.Sprintf("Invalid option: %s", "PROXY"), "")
}
}
client.sender = component.NewHttpSender(
component.Config{
Timeout: client.options.timeoutMs,
Proxy: client.options.proxy,
MaxRetry: client.options.maxRetry,
UserAgent: client.options.userAgent,
Domain: client.options.domain,
},
component.NewAuthenticator(client.options.apiKey, client.options.apiSecret, client.options.authenticationType),
)
client.Tracking = service.NewTrackingService(client.sender)
client.Courier = service.NewCourierService(client.sender)
client.EstimatedDeliveryDate = service.NewEstimatedDeliveryDateService(client.sender)
return client, nil
}