-
Notifications
You must be signed in to change notification settings - Fork 0
/
gooctranspoapi.go
601 lines (538 loc) · 17.1 KB
/
gooctranspoapi.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Package gooctranspoapi provides a Go wrapper around the OC Transpo
// Live Next Bus Arrival Data Feed API.
package gooctranspoapi
import (
"context"
"encoding/xml"
"errors"
"fmt"
"golang.org/x/net/html/charset"
"golang.org/x/time/rate"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// APIURLPrefix is the address at which the API is available.
const APIURLPrefix = "https://api.octranspo1.com/v1.3/"
// Connection holds the Application ID and API key needed to make requests.
// It also has a rate limiter, used by the Connection's methods to
// limit calls on the API. The HTTP Client is a public field, so that it
// can be swapped out with a custom HTTP Client if needed.
type Connection struct {
ID string
Key string
Limiter *rate.Limiter
HTTPClient *http.Client
cAPIURLPrefix string
}
// NewConnection returns a new connection without a rate limit.
func NewConnection(id, key string) Connection {
return Connection{
ID: id,
Key: key,
Limiter: rate.NewLimiter(rate.Inf, 0),
HTTPClient: http.DefaultClient,
cAPIURLPrefix: APIURLPrefix,
}
}
// NewConnectionWithRateLimit returns a new connection with a rate limit set.
// This is helpful for ensuring you don't go over the daily call limit,
// which is usually 10,000 requests per day.
// It you use the connection over 24 hours, a connection with a perSec rate
// of 0.11572 would make around 9998 requests.
func NewConnectionWithRateLimit(id, key string, perSec float64, burst int) Connection {
return Connection{
ID: id,
Key: key,
Limiter: rate.NewLimiter(rate.Limit(perSec), burst),
HTTPClient: http.DefaultClient,
cAPIURLPrefix: APIURLPrefix,
}
}
func (c Connection) performRequest(ctx context.Context, u url.URL, v url.Values) (io.ReadCloser, error) {
req, err := http.NewRequest("POST", u.String(), strings.NewReader(v.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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
}
// RouteSummaryForStop is a simplified version of the data returned by
// a request to GetRouteSummaryForStop.
type RouteSummaryForStop struct {
StopNo string
StopDescription string
Error string
Routes []Route
}
// Route is used by RouteSummaryForStop to store route data.
type Route struct {
RouteNo string
DirectionID string
Direction string
RouteHeading string
}
// RawRouteSummaryForStop is a wrapper around the XML data returned by
// a request to GetRouteSummaryForStop.
type rawRouteSummaryForStop struct {
XMLName xml.Name `xml:"Envelope"`
Text string `xml:",chardata"`
Soap string `xml:"soap,attr"`
Xsi string `xml:"xsi,attr"`
Xsd string `xml:"xsd,attr"`
Body struct {
Text string `xml:",chardata"`
GetRouteSummaryForStopResponse struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
GetRouteSummaryForStopResult struct {
Text string `xml:",chardata"`
StopNo struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"StopNo"`
StopDescription struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"StopDescription"`
Error struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"Error"`
Routes struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Route []struct {
Text string `xml:",chardata"`
RouteNo string `xml:"RouteNo"`
DirectionID string `xml:"DirectionID"`
Direction string `xml:"Direction"`
RouteHeading string `xml:"RouteHeading"`
} `xml:"Route"`
} `xml:"Routes"`
} `xml:"GetRouteSummaryForStopResult"`
} `xml:"GetRouteSummaryForStopResponse"`
} `xml:"Body"`
}
// Cook takes a raw XML RouteSummaryForStop and simplifies it.
func (d *rawRouteSummaryForStop) cook() (*RouteSummaryForStop, error) {
cooked := &RouteSummaryForStop{}
cooked.StopNo = d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.StopNo.Text
cooked.StopDescription = d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.StopDescription.Text
errorText, err := checkErrorCode(d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.Error.Text)
if err != nil {
return nil, err
}
cooked.Error = errorText
for _, r := range d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.Routes.Route {
cr := Route{}
cr.RouteNo = r.RouteNo
cr.DirectionID = r.DirectionID
cr.Direction = r.Direction
cr.RouteHeading = r.RouteHeading
cooked.Routes = append(cooked.Routes, cr)
}
return cooked, nil
}
// GetRouteSummaryForStop returns the routes for a given stop number.
func (c Connection) GetRouteSummaryForStop(ctx context.Context, stopNo string) (*RouteSummaryForStop, error) {
u, err := url.Parse(c.cAPIURLPrefix + "GetRouteSummaryForStop")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("appID", c.ID)
v.Set("apiKey", c.Key)
v.Set("stopNo", stopNo)
respBody, err := c.performRequest(ctx, *u, v)
if err != nil {
return nil, err
}
dec := xml.NewDecoder(respBody)
dec.CharsetReader = charset.NewReaderLabel
dec.Strict = false
data := &rawRouteSummaryForStop{}
err = dec.Decode(data)
respBody.Close()
if err != nil {
return nil, err
}
return data.cook()
}
// NextTripsForStop is a simplified version of the data returned by
// a request to GetNextTripsForStop
type NextTripsForStop struct {
StopNo string
StopLabel string
Error string
RouteDirections []RouteDirection
}
// RouteDirection is used by NextTripsForStop to store route direction data.
type RouteDirection struct {
RouteNo string
RouteLabel string
Direction string
Error string
RequestProcessingTime time.Time
Trips []Trip
}
// Trip stores trip data, and includes the adjusted schedule time.
type Trip struct {
TripDestination string
TripStartTime string
AdjustedScheduleTime int
AdjustmentAge float64
LastTripOfSchedule
BusType string
Latitude
Longitude
GPSSpeed
}
// LastTripOfSchedule stores both the data and if the data was set by the API
type LastTripOfSchedule struct {
Set bool
Value bool
}
// Latitude stores both the data and if the data was set by the API
type Latitude struct {
Set bool
Value float64
}
// Longitude stores both the data and if the data was set by the API
type Longitude struct {
Set bool
Value float64
}
// GPSSpeed stores both the data and if the data was set by the API
type GPSSpeed struct {
Set bool
Value float64
}
// rawNextTripsForStop is a wrapper around the XML data returned by
// a request to GetNextTripsForStop.
type rawNextTripsForStop struct {
XMLName xml.Name `xml:"Envelope"`
Text string `xml:",chardata"`
Soap string `xml:"soap,attr"`
Xsi string `xml:"xsi,attr"`
Xsd string `xml:"xsd,attr"`
Body struct {
Text string `xml:",chardata"`
GetNextTripsForStopResponse struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
GetNextTripsForStopResult struct {
Text string `xml:",chardata"`
StopNo struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"StopNo"`
StopLabel struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"StopLabel"`
Error struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"Error"`
Route struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
RouteDirection []struct {
Text string `xml:",chardata"`
RouteNo string `xml:"RouteNo"`
RouteLabel string `xml:"RouteLabel"`
Direction string `xml:"Direction"`
Error string `xml:"Error"`
RequestProcessingTime string `xml:"RequestProcessingTime"`
Trips struct {
Text string `xml:",chardata"`
Trip []rawXMLTrip `xml:"Trip"`
} `xml:"Trips"`
} `xml:"RouteDirection"`
} `xml:"Route"`
} `xml:"GetNextTripsForStopResult"`
} `xml:"GetNextTripsForStopResponse"`
} `xml:"Body"`
}
type rawXMLTrip struct {
Text string `xml:",chardata"`
TripDestination string `xml:"TripDestination"`
TripStartTime string `xml:"TripStartTime"`
AdjustedScheduleTime string `xml:"AdjustedScheduleTime"`
AdjustmentAge string `xml:"AdjustmentAge"`
LastTripOfSchedule string `xml:"LastTripOfSchedule"`
BusType string `xml:"BusType"`
Latitude string `xml:"Latitude"`
Longitude string `xml:"Longitude"`
GPSSpeed string `xml:"GPSSpeed"`
}
// Cook takes a raw XML NextTripsForStop and simplifies it.
func (d *rawNextTripsForStop) cook() (*NextTripsForStop, error) {
cooked := &NextTripsForStop{}
cooked.StopNo = d.Body.GetNextTripsForStopResponse.GetNextTripsForStopResult.StopNo.Text
cooked.StopLabel = d.Body.GetNextTripsForStopResponse.GetNextTripsForStopResult.StopLabel.Text
errorText, err := checkErrorCode(d.Body.GetNextTripsForStopResponse.GetNextTripsForStopResult.Error.Text)
if err != nil {
return nil, err
}
cooked.Error = errorText
for _, rd := range d.Body.GetNextTripsForStopResponse.GetNextTripsForStopResult.Route.RouteDirection {
crd := RouteDirection{}
crd.RouteNo = rd.RouteNo
crd.RouteLabel = rd.RouteLabel
crd.Direction = rd.Direction
errorText, err := checkErrorCode(rd.Error)
if err != nil {
return nil, err
}
crd.Error = errorText
tz, err := time.LoadLocation("America/Toronto")
if err != nil {
return nil, err
}
parsedProcessingTime, err := time.ParseInLocation("20060102150405", rd.RequestProcessingTime, tz)
if err != nil {
return nil, err
}
crd.RequestProcessingTime = parsedProcessingTime
for _, t := range rd.Trips.Trip {
ct, err := t.convert()
if err != nil {
return nil, err
}
crd.Trips = append(crd.Trips, ct)
}
cooked.RouteDirections = append(cooked.RouteDirections, crd)
}
return cooked, nil
}
// GetNextTripsForStop returns the next three trips on the route for a given stop number.
func (c Connection) GetNextTripsForStop(ctx context.Context, routeNo, stopNo string) (*NextTripsForStop, error) {
u, err := url.Parse(c.cAPIURLPrefix + "GetNextTripsForStop")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("appID", c.ID)
v.Set("apiKey", c.Key)
v.Set("routeNo", routeNo)
v.Set("stopNo", stopNo)
respBody, err := c.performRequest(ctx, *u, v)
if err != nil {
return nil, err
}
dec := xml.NewDecoder(respBody)
dec.CharsetReader = charset.NewReaderLabel
dec.Strict = false
data := &rawNextTripsForStop{}
err = dec.Decode(data)
respBody.Close()
if err != nil {
return nil, err
}
return data.cook()
}
// NextTripsForStopAllRoutes is a simplified version of the data returned by
// a request to GetNextTripsForStopAllRoutes
type NextTripsForStopAllRoutes struct {
StopNo string
StopDescription string
Error string
Routes []RouteWithTrips
}
// RouteWithTrips is used by NextTripsForStopAllRoutes to store route data.
type RouteWithTrips struct {
RouteNo string
DirectionID string
Direction string
RouteHeading string
Trips []Trip
}
// NextTripsForStopAllRoutes is a wrapper around the XML data returned by
// a request to GetNextTripsForStopAllRoutes.
type rawNextTripsForStopAllRoutes struct {
XMLName xml.Name `xml:"Envelope"`
Text string `xml:",chardata"`
Soap string `xml:"soap,attr"`
Xsi string `xml:"xsi,attr"`
Xsd string `xml:"xsd,attr"`
Body struct {
Text string `xml:",chardata"`
GetRouteSummaryForStopResponse struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
GetRouteSummaryForStopResult struct {
Text string `xml:",chardata"`
StopNo struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"StopNo"`
StopDescription struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"StopDescription"`
Error struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"Error"`
Routes struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Route []struct {
Text string `xml:",chardata"`
RouteNo string `xml:"RouteNo"`
DirectionID string `xml:"DirectionID"`
Direction string `xml:"Direction"`
RouteHeading string `xml:"RouteHeading"`
Trips struct {
Text string `xml:",chardata"`
Trip []rawXMLTrip `xml:"Trip"`
} `xml:"Trips"`
} `xml:"Route"`
} `xml:"Routes"`
} `xml:"GetRouteSummaryForStopResult"`
} `xml:"GetRouteSummaryForStopResponse"`
} `xml:"Body"`
}
// Cook takes a raw XML NextTripsForStopAllRoutes and simplifies it.
func (d *rawNextTripsForStopAllRoutes) cook() (*NextTripsForStopAllRoutes, error) {
cooked := &NextTripsForStopAllRoutes{}
cooked.StopNo = d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.StopNo.Text
cooked.StopDescription = d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.StopDescription.Text
errorText, err := checkErrorCode(d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.Error.Text)
if err != nil {
return nil, err
}
cooked.Error = errorText
for _, rt := range d.Body.GetRouteSummaryForStopResponse.GetRouteSummaryForStopResult.Routes.Route {
crt := RouteWithTrips{}
crt.RouteNo = rt.RouteNo
crt.DirectionID = rt.DirectionID
crt.Direction = rt.Direction
crt.RouteHeading = rt.RouteHeading
for _, t := range rt.Trips.Trip {
ct, err := t.convert()
if err != nil {
return nil, err
}
crt.Trips = append(crt.Trips, ct)
}
cooked.Routes = append(cooked.Routes, crt)
}
return cooked, nil
}
// GetNextTripsForStopAllRoutes returns the next three trips for all routes for a given stop number.
func (c Connection) GetNextTripsForStopAllRoutes(ctx context.Context, stopNo string) (*NextTripsForStopAllRoutes, error) {
u, err := url.Parse(c.cAPIURLPrefix + "GetNextTripsForStopAllRoutes")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("appID", c.ID)
v.Set("apiKey", c.Key)
v.Set("stopNo", stopNo)
respBody, err := c.performRequest(ctx, *u, v)
if err != nil {
return nil, err
}
dec := xml.NewDecoder(respBody)
dec.CharsetReader = charset.NewReaderLabel
dec.Strict = false
data := &rawNextTripsForStopAllRoutes{}
err = dec.Decode(data)
respBody.Close()
if err != nil {
return nil, err
}
return data.cook()
}
func checkErrorCode(errorText string) (string, error) {
switch errorText {
case "1":
return "", errors.New("error returned from API - Invalid API key")
case "2":
return "", errors.New("error returned from API - Unable to query data source")
case "10":
return "", errors.New("error returned from API - Invalid stop number")
case "11":
return "", errors.New("error returned from API - Invalid route number")
case "12":
return "", errors.New("error returned from API - Stop does not service route")
default:
return errorText, nil
}
}
func (t rawXMLTrip) convert() (Trip, error) {
ct := Trip{}
ct.TripDestination = t.TripDestination
ct.TripStartTime = t.TripStartTime
pAdjustedScheduleTime, err := strconv.Atoi(t.AdjustedScheduleTime)
if err != nil {
return ct, err
}
ct.AdjustedScheduleTime = pAdjustedScheduleTime
pAdjustmentAge, err := strconv.ParseFloat(t.AdjustmentAge, 64)
if err != nil {
return ct, err
}
ct.AdjustmentAge = pAdjustmentAge
if t.LastTripOfSchedule == "" {
ct.LastTripOfSchedule = LastTripOfSchedule{Set: false}
} else {
pLastTripOfSchedule, err := strconv.ParseBool(t.LastTripOfSchedule)
if err != nil {
return ct, err
}
ct.LastTripOfSchedule = LastTripOfSchedule{Set: true, Value: pLastTripOfSchedule}
}
ct.BusType = t.BusType
if t.Latitude == "" {
ct.Latitude = Latitude{Set: false}
} else {
pLatitude, err := strconv.ParseFloat(t.Latitude, 64)
if err != nil {
return ct, err
}
ct.Latitude = Latitude{Set: true, Value: pLatitude}
}
if t.Longitude == "" {
ct.Longitude = Longitude{Set: false}
} else {
pLongitude, err := strconv.ParseFloat(t.Longitude, 64)
if err != nil {
return ct, err
}
ct.Longitude = Longitude{Set: true, Value: pLongitude}
}
if t.GPSSpeed == "" {
ct.GPSSpeed = GPSSpeed{Set: false}
} else {
pGPSSpeed, err := strconv.ParseFloat(t.GPSSpeed, 64)
if err != nil {
return ct, err
}
ct.GPSSpeed = GPSSpeed{Set: true, Value: pGPSSpeed}
}
return ct, nil
}