-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
247 lines (226 loc) · 6.92 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 qcmobile
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"golang.org/x/sync/errgroup"
)
const (
_basePath = "/qc/services/carriers/"
_uri = "https://mobile.fmcsa.dot.gov" + _basePath
_searchDocketPath = "docket-number/"
_searchPath = "name/"
_cargoPath = "/cargo-carried"
_opClassPath = "/operation-classification"
_carrierDocketPath = "/docket-numbers"
_authPath = "/authority"
_oosPath = "/oos"
_basicsPath = "/basics"
)
var (
// ErrSystemMaintenance is returned when the api is failing due to system maintenance
ErrSystemMaintenance = errors.New("FMCSA Portal Unavailable due to Scheduled System Maintenance")
_maintenanceIndicator = []byte("<title>FMCSA System Maintenance Page</title>")
)
// Client - QC Mobile API Client interface
type Client interface {
SearchCarriersByName(ctx context.Context, name string, start, size int) ([]*CarrierDetails, error)
GetCompleteCarrierDetails(ctx context.Context, dotNumber string) (*CompleteCarrierDetails, error)
GetCarriersByDocket(ctx context.Context, docketNumber string) ([]*CarrierDetails, error)
GetCarrier(ctx context.Context, dotNumber string) (*CarrierDetails, error)
GetCargoCarried(ctx context.Context, dotNumber string) ([]*CargoClass, error)
GetOperationClassification(ctx context.Context, dotNumber string) ([]*OperationClass, error)
GetDocketNumbers(ctx context.Context, dotNumber string) ([]*Docket, error)
GetAuthority(ctx context.Context, dotNumber string) ([]*AuthorityDetails, error)
GetOOS(ctx context.Context, dotNumber string) ([]*OOSDetails, error)
GetBasics(ctx context.Context, dotNumber string) ([]*BasicsDetails, error)
}
// NewClient -
func NewClient(cfg Config) Client {
client := &client{
http: cfg.HTTPClient,
uri: _uri,
baseQuery: "?webKey=" + cfg.Key,
}
if client.http == nil {
client.http = &http.Client{}
}
return client
}
type client struct {
http *http.Client
uri string
baseQuery string
}
// SearchCarriersByName -
func (c *client) SearchCarriersByName(ctx context.Context, carrierName string, start, size int) ([]*CarrierDetails, error) {
path := _searchPath + carrierName
query := "start=" + strconv.Itoa(start) + "&size=" + strconv.Itoa(size)
var response searchResponse
if err := c.doGet(ctx, path, query, &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetCarriersByDocket -
func (c *client) GetCarriersByDocket(ctx context.Context, docketNumber string) ([]*CarrierDetails, error) {
path := _searchDocketPath + docketNumber
var response getCarriersByDocketResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
func (c *client) GetCompleteCarrierDetails(ctx context.Context, dotNumber string) (*CompleteCarrierDetails, error) {
var (
output = new(CompleteCarrierDetails)
group errgroup.Group
)
group.Go(func() error {
c, err := c.GetCarrier(ctx, dotNumber)
if err == nil {
output.Carrier = c.Carrier
}
return err
})
group.Go(func() error {
var err error
output.CargosCarried, err = c.GetCargoCarried(ctx, dotNumber)
return err
})
group.Go(func() error {
var err error
output.OperationClassifications, err = c.GetOperationClassification(ctx, dotNumber)
return err
})
group.Go(func() error {
var err error
output.Dockets, err = c.GetDocketNumbers(ctx, dotNumber)
return err
})
group.Go(func() error {
var err error
output.AuthorityDetails, err = c.GetAuthority(ctx, dotNumber)
return err
})
group.Go(func() error {
var err error
output.BasicsDetails, err = c.GetBasics(ctx, dotNumber)
return err
})
group.Go(func() error {
var err error
output.OOSDetails, err = c.GetOOS(ctx, dotNumber)
return err
})
if err := group.Wait(); err != nil {
return nil, err
}
return output, nil
}
// GetCarrier -
func (c *client) GetCarrier(ctx context.Context, dotNumber string) (*CarrierDetails, error) {
var response carrierResponse
if err := c.doGet(ctx, dotNumber, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetCargoCarried -
func (c *client) GetCargoCarried(ctx context.Context, dotNumber string) ([]*CargoClass, error) {
path := dotNumber + _cargoPath
var response cargoCarriedResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetOperationClassification -
func (c *client) GetOperationClassification(ctx context.Context, dotNumber string) ([]*OperationClass, error) {
path := dotNumber + _opClassPath
var response operationClassificationResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetDocketNumbers -
func (c *client) GetDocketNumbers(ctx context.Context, dotNumber string) ([]*Docket, error) {
path := dotNumber + _carrierDocketPath
var response docketNumbersResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetAuthority -
func (c *client) GetAuthority(ctx context.Context, dotNumber string) ([]*AuthorityDetails, error) {
path := dotNumber + _authPath
var response authorityResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetOOS -
func (c *client) GetOOS(ctx context.Context, dotNumber string) ([]*OOSDetails, error) {
path := dotNumber + _oosPath
var response oosResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
// GetBasics -
func (c *client) GetBasics(ctx context.Context, dotNumber string) ([]*BasicsDetails, error) {
path := dotNumber + _basicsPath
var response basicsResponse
if err := c.doGet(ctx, path, "", &response); err != nil {
return nil, err
}
return response.Content, nil
}
func (c *client) doGet(ctx context.Context, path, query string, output interface{}) error {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, c.buildURL(path, query), nil)
if err != nil {
return err
}
resp, err := c.http.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return tryExtractError(resp)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.New(resp.Status)
}
if err := json.Unmarshal(body, output); err != nil {
if bytes.Contains(body, _maintenanceIndicator) {
return ErrSystemMaintenance
}
return err
}
return nil
}
func (c *client) buildURL(path, query string) string {
if query != "" {
return c.uri + path + c.baseQuery + "&" + query
}
return c.uri + path + c.baseQuery
}
func tryExtractError(resp *http.Response) error {
if body, err := io.ReadAll(resp.Body); err == nil {
var errResponse errorResponse
if err := json.Unmarshal(body, &errResponse); err == nil {
return errors.New(resp.Status + ": " + errResponse.ErrMsg)
}
}
return errors.New(resp.Status)
}