/
gtfs.go
389 lines (363 loc) · 10.7 KB
/
gtfs.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package gooctranspoapi
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
// ID will setup the request to return a specific row in a table by the id value.
func ID(id string) func(url.Values) error {
return func(v url.Values) error {
v.Set("id", id)
return nil
}
}
// ColumnAndValue will setup the request to return data from a specific column and value.
func ColumnAndValue(column, value string) func(url.Values) error {
return func(v url.Values) error {
v.Set("column", column)
v.Set("value", value)
return nil
}
}
// OrderBy will setup the request to sort the data by a specific column.
func OrderBy(orderBy string) func(url.Values) error {
return func(v url.Values) error {
v.Set("orderBy", orderBy)
return nil
}
}
// Direction will setup the request to direction of sorted records, asc and desc.
func Direction(direction string) func(url.Values) error {
return func(v url.Values) error {
if direction != "asc" && direction != "desc" {
return errors.New("direction only accepts asc or desc as parameters")
}
v.Set("direction", direction)
return nil
}
}
// Limit will setup the request to only return a maximum number of records.
func Limit(limit int) func(url.Values) error {
return func(v url.Values) error {
v.Set("limit", strconv.Itoa(limit))
return nil
}
}
func setTable(table string) func(url.Values) error {
return func(v url.Values) error {
v.Set("table", table)
return nil
}
}
func (c Connection) setupGTFSURL(options ...func(url.Values) error) (*url.URL, error) {
u, err := url.Parse(c.cAPIURLPrefix + "Gtfs")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("appID", c.ID)
v.Set("apiKey", c.Key)
v.Set("format", "json")
for _, opt := range options {
err := opt(v)
if err != nil {
return nil, err
}
}
u.RawQuery = v.Encode()
return u, nil
}
func (c Connection) performGTFSRequest(ctx context.Context, u *url.URL) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Close = true
err = c.Limiter.Wait(ctx)
if err != nil {
return nil, err
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
if resp != nil {
resp.Body.Close()
}
return nil, err
}
if resp.StatusCode != 200 {
resp.Body.Close()
return nil, fmt.Errorf("Non 200 HTTP response from API. %v %v", resp.Status, u.String())
}
return resp.Body, nil
}
// GTFSAgency is the GTFS agency table.
type GTFSAgency struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
AgencyName string `json:"agency_name"`
AgencyURL string `json:"agency_url"`
AgencyTimezone string `json:"agency_timezone"`
AgencyLang string `json:"agency_lang"`
AgencyPhone string `json:"agency_phone"`
} `json:"Gtfs"`
}
// GetGTFSAgency returns the GTFS agency table.
func (c Connection) GetGTFSAgency(ctx context.Context, options ...func(url.Values) error) (*GTFSAgency, error) {
options = append(options, setTable("agency"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSAgency{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}
// GTFSCalendar is the GTFS calendar table.
type GTFSCalendar struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
ServiceID string `json:"service_id"`
Monday string `json:"monday"`
Tuesday string `json:"tuesday"`
Wednesday string `json:"wednesday"`
Thursday string `json:"thursday"`
Friday string `json:"friday"`
Saturday string `json:"saturday"`
Sunday string `json:"sunday"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
} `json:"Gtfs"`
}
// GetGTFSCalendar returns the GTFS calendar table.
func (c Connection) GetGTFSCalendar(ctx context.Context, options ...func(url.Values) error) (*GTFSCalendar, error) {
options = append(options, setTable("calendar"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSCalendar{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}
// GTFSCalendarDates is GTFS calendar_dates table.
type GTFSCalendarDates struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
ServiceID string `json:"service_id"`
Date string `json:"date"`
ExceptionType string `json:"exception_type"`
} `json:"Gtfs"`
}
// GetGTFSCalendarDates returns the GTFS calendar_dates table
func (c Connection) GetGTFSCalendarDates(ctx context.Context, options ...func(url.Values) error) (*GTFSCalendarDates, error) {
options = append(options, setTable("calendar_dates"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSCalendarDates{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}
// GTFSRoutes is the GTFS routes table.
type GTFSRoutes struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
RouteID string `json:"route_id"`
RouteShortName string `json:"route_short_name"`
RouteLongName string `json:"route_long_name"`
RouteDesc string `json:"route_desc"`
RouteType string `json:"route_type"`
} `json:"Gtfs"`
}
// GetGTFSRoutes returns the GTFS routes table.
func (c Connection) GetGTFSRoutes(ctx context.Context, options ...func(url.Values) error) (*GTFSRoutes, error) {
options = append(options, setTable("routes"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSRoutes{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}
// GTFSStops is the GTFS stops table.
type GTFSStops struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Column string `json:"column"`
Value string `json:"value"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
StopID string `json:"stop_id"`
StopCode string `json:"stop_code"`
StopName string `json:"stop_name"`
StopDesc string `json:"stop_desc"`
StopLat string `json:"stop_lat"`
StopLon string `json:"stop_lon"`
ZoneID string `json:"zone_id"`
StopURL string `json:"stop_url"`
LocationType string `json:"location_type"`
ParentStation string `json:"parent_station"`
} `json:"Gtfs"`
}
// GetGTFSStops returns the GTFS stops table.
// It requires a stop_id, stop_code or id value specified, using ColumnAndValue() or ID() options.
func (c Connection) GetGTFSStops(ctx context.Context, options ...func(url.Values) error) (*GTFSStops, error) {
options = append(options, setTable("stops"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
if v.Get("column") != "stop_id" && v.Get("column") != "stop_code" && v.Get("id") == "" {
return nil, errors.New("a stop_id, stop_code or id value must be specified")
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSStops{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}
// GTFSStopTimes is the GTFS stop_times table.
type GTFSStopTimes struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Column string `json:"column"`
Value string `json:"value"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
TripID string `json:"trip_id"`
ArrivalTime string `json:"arrival_time"`
DepartureTime string `json:"departure_time"`
StopID string `json:"stop_id"`
StopSequence string `json:"stop_sequence"`
PickupType string `json:"pickup_type"`
DropOffType string `json:"drop_off_type"`
} `json:"Gtfs"`
}
// GetGTFSStopTimes returns the GTFS stop_times table.
// It requires a trip_id, stop_code or id value specified, using ColumnAndValue() or ID() options.
func (c Connection) GetGTFSStopTimes(ctx context.Context, options ...func(url.Values) error) (*GTFSStopTimes, error) {
options = append(options, setTable("stop_times"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
if v.Get("column") != "trip_id" && v.Get("column") != "stop_id" && v.Get("id") == "" {
return nil, errors.New("a trip_id, stop_id or id value must be specified")
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSStopTimes{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}
// GTFSTrips is the GTFS trips table.
type GTFSTrips struct {
Query struct {
Table string `json:"table"`
Direction string `json:"direction"`
Column string `json:"column"`
Value string `json:"value"`
Format string `json:"format"`
} `json:"Query"`
Gtfs []struct {
ID string `json:"id"`
RouteID string `json:"route_id"`
ServiceID string `json:"service_id"`
TripID string `json:"trip_id"`
TripHeadsign string `json:"trip_headsign"`
DirectionID string `json:"direction_id"`
BlockID string `json:"block_id"`
} `json:"Gtfs"`
}
// GetGTFSTrips returns the GTFS trips table.
// It requires a route_id or id value specified, using ColumnAndValue() or ID() options.
func (c Connection) GetGTFSTrips(ctx context.Context, options ...func(url.Values) error) (*GTFSTrips, error) {
options = append(options, setTable("trips"))
u, err := c.setupGTFSURL(options...)
if err != nil {
return nil, err
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
if v.Get("column") != "route_id" && v.Get("id") == "" {
return nil, errors.New("a route_id or id value must be specified")
}
respBody, err := c.performGTFSRequest(ctx, u)
if err != nil {
return nil, err
}
data := >FSTrips{}
err = json.NewDecoder(respBody).Decode(data)
respBody.Close()
return data, err
}