-
Notifications
You must be signed in to change notification settings - Fork 0
/
ups.go
148 lines (118 loc) · 3.05 KB
/
ups.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ups
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httputil"
"time"
)
type Environment string
const (
Testing Environment = "https://wwwcie.ups.com"
Production Environment = "https://onlinetools.ups.com"
shipmentURL = "/api/shipments/v2403/ship"
oauthURL = "/security/v1/oauth"
)
type Client struct {
httpClient *http.Client
environment Environment
accessLicenseNumber string
// authorization
username string
password string
clientID string
clientSecret string
accessToken string
accessTokenIsValidUntil time.Time
logWriter io.Writer
}
type OptionFunction func(*Client)
func New(options ...OptionFunction) *Client {
c := &Client{
httpClient: http.DefaultClient,
}
for _, option := range options {
option(c)
}
return c
}
// WithEnvironment defines the used Environment for API requests.
func WithEnvironment(environment Environment) OptionFunction {
return func(c *Client) {
c.environment = environment
}
}
// WithUsernameAndPassword uses username and password for authentication.
func WithUsernameAndPassword(username, password string) OptionFunction {
return func(c *Client) {
c.username = username
c.password = password
}
}
// WithClientIDAndSecret uses an access token for authentication.
func WithClientIDAndSecret(clientID, clientSecret string) OptionFunction {
return func(c *Client) {
c.clientID = clientID
c.clientSecret = clientSecret
}
}
// WithAccessLicenseNumber define the access key used in API requests.
func WithAccessLicenseNumber(accessLicenseNumber string) OptionFunction {
return func(c *Client) {
c.accessLicenseNumber = accessLicenseNumber
}
}
// WithHTTPClient uses custom http.Client
func WithHTTPClient(client *http.Client) OptionFunction {
return func(c *Client) {
c.httpClient = client
}
}
// WithLogWriter will write http.Request and http.Response into provided writer
func WithLogWriter(writer io.Writer) OptionFunction {
return func(c *Client) {
c.logWriter = writer
}
}
func (c *Client) addAuthorization(ctx context.Context, req *http.Request) error {
if c.accessLicenseNumber != "" {
req.Header.Set("AccessLicenseNumber", c.accessLicenseNumber)
}
if c.clientID != "" && c.clientSecret != "" && c.accessTokenIsValidUntil.Before(time.Now()) {
err := c.getOAuthAccessToken(ctx)
if err != nil {
return err
}
}
if c.username != "" && c.password != "" {
req.Header.Set("Username", c.username)
req.Header.Set("Password", c.password)
}
if c.accessToken != "" {
req.Header.Set("Authorization", c.accessToken)
}
return nil
}
func (c *Client) logHTTPRequest(req *http.Request) error {
if c.logWriter == nil {
return nil
}
b, err := httputil.DumpRequestOut(req, true)
if err != nil {
return err
}
_, err = fmt.Fprint(c.logWriter, string(b))
return err
}
func (c *Client) logHTTPResponse(res *http.Response) error {
if c.logWriter == nil {
return nil
}
b, err := httputil.DumpResponse(res, true)
if err != nil {
return err
}
_, err = fmt.Fprint(c.logWriter, string(b))
return err
}