-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
247 lines (204 loc) · 6.29 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
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
241
242
243
244
245
246
247
package mapbox
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"sync"
"time"
)
const (
ResponseOK = "Ok"
baseUrl = "https://api.mapbox.com"
v1 = "v1"
v5 = "v5"
)
type MapboxConfig struct {
Timeout time.Duration
APIKey string
// Optional http.Client can be defined in config if specific options are needed
// If not provided will default to the stdlib http.Client
Client HTTPClient
}
// RateLimit represents a set of operations that share a rate limit
// see https://docs.mapbox.com/api/overview/#rate-limits
type RateLimit string
const (
GeocodingRateLimit = "geocoding"
MatrixRateLimit = "matrix"
DirectionsRateLimit = "directions"
SearchboxRateLimit = "searchbox"
)
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
type Client struct {
httpClient HTTPClient
apiKey string
// Referer is needed when URL restrictions are enforced, see https://docs.mapbox.com/accounts/guides/tokens/#url-restrictions
Referer string
rateLimits map[RateLimit]time.Time
rateLimitMutex sync.RWMutex
}
// NewClient instantiates a new Mapbox client.
func NewClient(config *MapboxConfig) (*Client, error) {
// Default timeout
if config.Timeout == 0 {
config.Timeout = 30 * time.Second
}
if config.APIKey == "" {
return nil, fmt.Errorf("missing Mapbox API key")
}
var httpClient HTTPClient
if config.Client != nil {
httpClient = config.Client
} else {
httpClient = &http.Client{Timeout: config.Timeout}
}
return &Client{
httpClient: httpClient,
apiKey: config.APIKey,
rateLimits: make(map[RateLimit]time.Time),
}, nil
}
//////////////////////////////////////////////////////////////////
type ErrorResponse struct {
Message string `json:"message"`
Code string `json:"code"`
}
type Waypoint struct {
Distance float64 `json:"distance"`
Name string `json:"name"`
Location []float64
}
//////////////////////////////////////////////////////////////////
func (c *Client) DirectionsMatrix(ctx context.Context, req *DirectionsMatrixRequest) (*DirectionsMatrixResponse, error) {
if err := c.checkRateLimit(MatrixRateLimit); err != nil {
return nil, err
}
return directionsMatrix(ctx, c, req)
}
func (c *Client) ReverseGeocode(ctx context.Context, req *ReverseGeocodeRequest) (*GeocodeResponse, error) {
if err := c.checkRateLimit(GeocodingRateLimit); err != nil {
return nil, err
}
return reverseGeocode(ctx, c, req)
}
func (c *Client) ReverseGeocodeBatch(ctx context.Context, req ReverseGeocodeBatchRequest) (*GeocodeBatchResponse, error) {
if err := c.checkRateLimit(GeocodingRateLimit); err != nil {
return nil, err
}
return reverseGeocodeBatch(ctx, c, req)
}
func (c *Client) ForwardGeocode(ctx context.Context, req *ForwardGeocodeRequest) (*GeocodeResponse, error) {
if err := c.checkRateLimit(GeocodingRateLimit); err != nil {
return nil, err
}
return forwardGeocode(ctx, c, req)
}
func (c *Client) ForwardGeocodeBatch(ctx context.Context, req ForwardGeocodeBatchRequest) (*GeocodeBatchResponse, error) {
if err := c.checkRateLimit(GeocodingRateLimit); err != nil {
return nil, err
}
return forwardGeocodeBatch(ctx, c, req)
}
func (c *Client) Directions(ctx context.Context, req *DirectionsRequest) (*DirectionsResponse, error) {
if err := c.checkRateLimit(DirectionsRateLimit); err != nil {
return nil, err
}
return directions(ctx, c, req)
}
func (c *Client) SearchboxReverse(ctx context.Context, req *SearchboxReverseRequest) (*SearchboxReverseResponse, error) {
if err := c.checkRateLimit(SearchboxRateLimit); err != nil {
return nil, err
}
return searchboxReverse(ctx, c, req)
}
//////////////////////////////////////////////////////////////////
func (c *Client) get(ctx context.Context, relPath string, query url.Values) (*http.Response, error) {
return c.do(ctx, http.MethodGet, relPath, query, nil)
}
func (c *Client) post(ctx context.Context, relPath string, query url.Values, body io.Reader) (*http.Response, error) {
return c.do(ctx, http.MethodPost, relPath, query, body)
}
func (c *Client) do(ctx context.Context, httpVerb, relPath string, query url.Values, body io.Reader) (*http.Response, error) {
// remove empty entries
for k := range query {
if query.Get(k) == "" {
query.Del(k)
}
}
uri, err := url.JoinPath(baseUrl, relPath)
if err != nil {
return nil, err
}
if len(query) > 0 {
uri = fmt.Sprintf("%v?%v", uri, query.Encode())
}
req, err := http.NewRequestWithContext(ctx, httpVerb, uri, body)
if err != nil {
return nil, err
}
if c.Referer != "" {
req.Header.Set("Referer", c.Referer)
}
return c.httpClient.Do(req)
}
func (c *Client) handleResponse(apiResponse *http.Response, response interface{}, rateLimit RateLimit) error {
defer apiResponse.Body.Close()
// auth checking
if apiResponse.StatusCode == http.StatusUnauthorized {
return fmt.Errorf("unauthorized request. Provide Mapbox API key")
}
body, err := io.ReadAll(apiResponse.Body)
if err != nil {
return fmt.Errorf("failed to read body. %w", err)
}
// check for errors from Mapbox API (non 200 response)
if apiResponse.StatusCode >= 400 && apiResponse.StatusCode <= 599 {
var errorResponse ErrorResponse
err := json.Unmarshal(body, &errorResponse)
if err != nil {
return NewMapboxError(apiResponse.StatusCode, "")
}
// If rate limited, hold off till the next X-Rate-Limit-Reset
if apiResponse.StatusCode == 429 {
resetUnix, err := strconv.Atoi(apiResponse.Header.Get("X-Rate-Limit-Reset"))
if err == nil {
c.rateLimitMutex.Lock()
defer c.rateLimitMutex.Unlock()
c.rateLimits[rateLimit] = time.Unix(int64(resetUnix), 0)
}
}
return NewMapboxError(apiResponse.StatusCode, errorResponse.Message)
}
// convert to response
if err := json.Unmarshal(body, &response); err != nil {
return fmt.Errorf("failed to read body. %w", err)
}
return nil
}
func (c *Client) rateLimit(rl RateLimit) time.Time {
c.rateLimitMutex.RLock()
defer c.rateLimitMutex.RUnlock()
return c.rateLimits[rl]
}
func (c *Client) checkRateLimit(rl RateLimit) error {
reset := c.rateLimit(rl)
// No reset set
if reset.IsZero() {
return nil
}
// Reset reached
if reset.Before(time.Now()) {
c.rateLimitMutex.Lock()
defer c.rateLimitMutex.Unlock()
c.rateLimits[rl] = time.Time{}
return nil
}
// Reset still in future
return NewMapboxError(429, fmt.Sprintf("Rate limiting %v requests", rl))
}