/
builder.go
240 lines (206 loc) · 8.01 KB
/
builder.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package wireup
import (
"crypto/tls"
"fmt"
"math/rand"
"net"
"net/http"
"net/url"
"time"
"github.com/smartystreets/smartystreets-go-sdk"
internal "github.com/smartystreets/smartystreets-go-sdk/internal/sdk"
international_autocomplete "github.com/smartystreets/smartystreets-go-sdk/international-autocomplete-api"
international_street "github.com/smartystreets/smartystreets-go-sdk/international-street-api"
"github.com/smartystreets/smartystreets-go-sdk/us-autocomplete-api"
autocomplete_pro "github.com/smartystreets/smartystreets-go-sdk/us-autocomplete-pro-api"
us_enrichment "github.com/smartystreets/smartystreets-go-sdk/us-enrichment-api"
"github.com/smartystreets/smartystreets-go-sdk/us-extract-api"
us_reverse_geo "github.com/smartystreets/smartystreets-go-sdk/us-reverse-geo-api"
"github.com/smartystreets/smartystreets-go-sdk/us-street-api"
"github.com/smartystreets/smartystreets-go-sdk/us-zipcode-api"
)
// clientBuilder is responsible for accepting credentials and other configuration options to combine
// all components necessary to assemble a fully functional Client for use in an application.
type clientBuilder struct {
credential sdk.Credential
baseURL *url.URL
proxy *url.URL
retries int
timeout time.Duration
debug bool
close bool
trace bool
headers http.Header
idleConns int
http2Disabled bool
client *http.Client
licenses []string
}
func newClientBuilder() *clientBuilder {
return &clientBuilder{
credential: &internal.NopCredential{},
retries: 4,
timeout: time.Second * 10,
headers: initializeHeadersWithUserAgent(),
}
}
func initializeHeadersWithUserAgent() http.Header {
headers := make(http.Header)
headers.Add("User-Agent", fmt.Sprintf("smartystreets (sdk:go@%s)", sdk.VERSION))
return headers
}
func (b *clientBuilder) withSecretKeyCredential(authID, authToken string) *clientBuilder {
b.credential = sdk.NewSecretKeyCredential(authID, authToken)
return b
}
func (b *clientBuilder) withWebsiteKeyCredential(key, hostnameOrIP string) *clientBuilder {
b.credential = sdk.NewWebsiteKeyCredential(key, hostnameOrIP)
return b
}
func (b *clientBuilder) withCustomBaseURL(address string) *clientBuilder {
parsed, err := url.Parse(address)
if err != nil {
panic(fmt.Sprint("Could not parse provided address:", err.Error()))
}
b.baseURL = parsed
return b
}
func (b *clientBuilder) withMaxRetry(retries int) *clientBuilder {
if retries < 0 {
panic(fmt.Sprintf("Please provide a non-negative number of retry attempts (you supplied %d).", retries))
}
b.retries = retries
return b
}
func (b *clientBuilder) withTimeout(duration time.Duration) *clientBuilder {
if duration < 0 {
panic(fmt.Sprintf("Please provide a non-negative duration (you supplied %s).", duration.String()))
}
b.timeout = duration
return b
}
func (b *clientBuilder) withDebugHTTPOutput() *clientBuilder {
b.debug = true
return b
}
func (b *clientBuilder) withHTTPRequestTracing() *clientBuilder {
b.trace = true
return b
}
func (b *clientBuilder) withCustomHeader(key, value string) *clientBuilder {
b.headers.Add(key, value)
return b
}
func (b *clientBuilder) withoutKeepAlive() *clientBuilder {
b.close = true
return b
}
func (b *clientBuilder) disableHTTP2() *clientBuilder {
b.http2Disabled = true
return b
}
func (b *clientBuilder) viaProxy(address string) *clientBuilder {
proxy, err := url.Parse(address)
if err != nil {
panic(fmt.Sprint("Could not parse provided address:", err.Error()))
}
b.proxy = proxy
return b
}
func (b *clientBuilder) withMaxIdleConnections(max int) *clientBuilder {
b.idleConns = max
return b
}
func (b *clientBuilder) buildUSStreetAPIClient() *street.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USStreetAPI)
return street.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildUSZIPCodeAPIClient() *zipcode.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USZIPCodeAPI)
return zipcode.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildUSAutocompleteAPIClient() *autocomplete.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USAutocompleteAPI)
return autocomplete.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildUSAutocompleteProAPIClient() *autocomplete_pro.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USAutocompleteProAPI)
return autocomplete_pro.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildUSEnrichmentAPIClient() *us_enrichment.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USEnrichmentAPI)
return us_enrichment.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildUSExtractAPIClient() *extract.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USExtractAPI)
return extract.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildInternationalStreetAPIClient() *international_street.Client {
b.ensureBaseURLNotNil(defaultBaseURL_InternationalStreetAPI)
return international_street.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildInternationalAutocompleteAPIClient() *international_autocomplete.Client {
b.ensureBaseURLNotNil(defaultBaseURL_InternationalAutocompleteAPI)
return international_autocomplete.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) buildUSReverseGeocodingAPIClient() *us_reverse_geo.Client {
b.ensureBaseURLNotNil(defaultBaseURL_USReverseGeocodingAPI)
return us_reverse_geo.NewClient(b.buildHTTPSender())
}
func (b *clientBuilder) ensureBaseURLNotNil(u *url.URL) {
if b.baseURL == nil {
b.baseURL = u
}
}
func (b *clientBuilder) buildHTTPSender() *internal.HTTPSender {
client := b.buildHTTPClient()
return internal.NewHTTPSender(client)
}
func (b *clientBuilder) buildHTTPClient() (wrapped internal.HTTPClient) {
// inner-most
wrapped = b.buildClient()
wrapped = internal.NewTracingClient(wrapped, b.trace)
wrapped = internal.NewDebugOutputClient(wrapped, b.debug)
wrapped = internal.NewRetryClient(wrapped, b.retries, rand.New(rand.NewSource(time.Now().UnixNano())), time.Sleep)
wrapped = internal.NewSigningClient(wrapped, b.credential)
wrapped = internal.NewCustomHeadersClient(wrapped, b.headers)
wrapped = internal.NewBaseURLClient(wrapped, b.baseURL)
wrapped = internal.NewKeepAliveCloseClient(wrapped, b.close)
wrapped = internal.NewLicenseClient(wrapped, b.licenses...)
// outer-most
return wrapped
}
func (b *clientBuilder) buildClient() *http.Client {
if b.client != nil {
return b.client
}
return &http.Client{Timeout: b.timeout, Transport: b.buildTransport()}
}
func (b *clientBuilder) buildTransport() *http.Transport {
transport := &http.Transport{}
if b.proxy != nil {
transport.Proxy = http.ProxyURL(b.proxy)
}
if b.idleConns > 0 {
transport.MaxIdleConnsPerHost = b.idleConns
}
if b.http2Disabled { // https://golang.org/pkg/net/http/ ("Programs that must disable HTTP/2 can do so by setting Transport.TLSNextProto to a non-nil, empty map.")
transport.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper, 0)
}
transport.DialContext = (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext
return transport
}
var (
defaultBaseURL_InternationalStreetAPI = &url.URL{Scheme: "https", Host: "international-street.api.smarty.com"}
defaultBaseURL_InternationalAutocompleteAPI = &url.URL{Scheme: "https", Host: "international-autocomplete.api.smarty.com"}
defaultBaseURL_USStreetAPI = &url.URL{Scheme: "https", Host: "us-street.api.smarty.com"}
defaultBaseURL_USZIPCodeAPI = &url.URL{Scheme: "https", Host: "us-zipcode.api.smarty.com"}
defaultBaseURL_USAutocompleteAPI = &url.URL{Scheme: "https", Host: "us-autocomplete.api.smarty.com"}
defaultBaseURL_USEnrichmentAPI = &url.URL{Scheme: "https", Host: "us-enrichment.api.smarty.com"}
defaultBaseURL_USExtractAPI = &url.URL{Scheme: "https", Host: "us-extract.api.smarty.com"}
defaultBaseURL_USReverseGeocodingAPI = &url.URL{Scheme: "https", Host: "us-reverse-geo.api.smarty.com"}
defaultBaseURL_USAutocompleteProAPI = &url.URL{Scheme: "https", Host: "us-autocomplete-pro.api.smarty.com"}
)