-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravel_planner.go
418 lines (349 loc) · 13.1 KB
/
travel_planner.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
package sl
import (
"context"
"errors"
"fmt"
)
// travelPlannerEndpoint is the endpoint to the travel planner api.
const travelPlannerEndpoint = "TravelplannerV3/%s.json"
var (
ErrNoTripFound = errors.New("No trip found")
)
// Trip represents a trip.
type Trip struct {
LegList struct {
Leg []struct {
Destination struct {
Date string `json:"date"`
ExtID string `json:"extId"`
HasMainMast bool `json:"hasMainMast"`
ID string `json:"id"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
MainMastExtID string `json:"mainMastExtId"`
MainMastID string `json:"mainMastId"`
Name string `json:"name"`
PrognosisType string `json:"prognosisType"`
Time string `json:"time"`
Track string `json:"track"`
Type string `json:"type"`
} `json:"Destination"`
JourneyDetailRef struct {
Ref string `json:"ref"`
} `json:"JourneyDetailRef"`
JourneyStatus string `json:"JourneyStatus"`
Origin struct {
Date string `json:"date"`
ExtID string `json:"extId"`
HasMainMast bool `json:"hasMainMast"`
ID string `json:"id"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
MainMastExtID string `json:"mainMastExtId"`
MainMastID string `json:"mainMastId"`
Name string `json:"name"`
PrognosisType string `json:"prognosisType"`
Time string `json:"time"`
Track string `json:"track"`
Type string `json:"type"`
} `json:"Origin"`
Product struct {
Admin string `json:"admin"`
CatCode string `json:"catCode"`
CatIn string `json:"catIn"`
CatOut string `json:"catOut"`
CatOutL string `json:"catOutL"`
CatOutS string `json:"catOutS"`
Line string `json:"line"`
Name string `json:"name"`
Num string `json:"num"`
Operator string `json:"operator"`
OperatorCode string `json:"operatorCode"`
} `json:"Product"`
Category string `json:"category"`
Direction string `json:"direction"`
Idx string `json:"idx"`
Name string `json:"name"`
Number string `json:"number"`
Reachable bool `json:"reachable"`
Type string `json:"type"`
} `json:"Leg"`
} `json:"LegList"`
ServiceDays []struct {
PlanningPeriodBegin string `json:"planningPeriodBegin"`
PlanningPeriodEnd string `json:"planningPeriodEnd"`
SDaysB string `json:"sDaysB"`
SDaysI string `json:"sDaysI"`
SDaysR string `json:"sDaysR"`
} `json:"ServiceDays"`
TariffResult struct {
FareSetItem []struct {
Desc string `json:"desc"`
FareItem []struct {
Cur string `json:"cur"`
Desc string `json:"desc"`
Name string `json:"name"`
Price int `json:"price"`
} `json:"fareItem"`
Name string `json:"name"`
} `json:"fareSetItem"`
} `json:"TariffResult"`
Checksum string `json:"checksum"`
CtxRecon string `json:"ctxRecon"`
Duration string `json:"duration"`
Idx int `json:"idx"`
TripID string `json:"tripId"`
}
// TripResponseData represents the travel planner trip response data SL API.
type TripResponseData struct {
ErrorCode string `json:"errorCode"`
ErrorText string `json:"errorText"`
Message string `json:"Message"`
Trip []*Trip `json:"Trip"`
ScrB string `json:"scrB"`
ScrF string `json:"scrF"`
}
// TravelPlannerService handles communication with the travel planner related
// methods of the SL API.
//
// SL API docs: https://www.trafiklab.se/node/16717/documentation
type TravelPlannerService service
// TripOptions specifies optional parameters to the TravelPlannerService.Trip.
type TripOptions struct {
// Number of minutes added to estimated turnaround time.
AddChangeTime int `url:"addChangeTime,omitempty"`
// Separated by; in the following format: avoidId | avoidStatus
// - avoidId, internal or external id for stop / station to avoid,
// - avoidStatus, one of NPAVO (do not pass), NCAVO (do not change). Optionally.
Avoid string `url:"avoid,omitempty"`
// Internal or external ID for stop / station to avoid change.
AvoidID string `url:"avoidID,omitempty"`
// Percentage of original estimated time to handle a change.
// Ex, 200 doubles the time the system will use for the traveler to catch up with a change.
// Default 100.
ChangeTimePercent int `url:"changeTimePercent,omitempty"`
// Parameter that specifies the starting point for searching later or earlier trips.
Context string `url:"context,omitempty"`
// Trip date. Example: 2014-08-23. Default is today.
Date string `url:"date,omitempty"`
// Destination station id. Example: 300109600, 9600.
DestID string `url:"destId,omitempty"`
// Can either be a website id or an alias, website or acronym. Examples: 300109001, 9001, TCE.
DestExtID string `url:"destExtId,omitempty"`
// The destination coordinate in latitude.
DestCoordLat string `url:"destCoordLat,omitempty"`
// The destination coordinate in longitude.
DestCoordLong string `url:"destCoordLong,omitempty"`
// Indicates whether a trip can start with a walking distance. For distance sharing, min and max number of meters can be specified as 1, [min distance], [max distance].
// Default is 1.
DestWalk string `url:"destWalk,omitempty"`
// API Key.
Key string `url:"key,omitempty"`
// Response language. Default 'sv', can be 'en' or 'de'.
Lang string `url:"lang,omitempty"`
// Line or lines separated by commas to be used to filter results, exclamation points are used for exclusion of lines.
// Example: lines=55,122
Lines string `url:"lines,omitempty"`
// Max change (0-11).
MaxChange int `url:"maxChange,omitempty"`
// Max change time in minutes.
MaxChangeTime int `url:"maxChangeTime,omitempty"`
// Min change time in minutes.
MinChangeTime int `url:"minChangeTime,omitempty"`
// Min number of trips by the specified start time, default 4.
// NumF and NumB together can not exceed 6.
NumB string `url:"numB,omitempty"`
// Min number of trips before the specified start time, default 1.
// NumF and NumB together can not exceed 6.
NumF string `url:"numF,omitempty"`
// Limit the number of returned trips. Note that this is an approximate number. Default = 5
NumTrips int `url:"numTrips,omitempty"`
// The station to start at. Example: 300109600, 9600.
OriginID string `url:"originId,omitempty"`
// Can either be a website id or an alias, website or acronym. Examples: 300109001, 9001, TCE.
OriginExtID string `url:"originExtId,omitempty"`
// The start coordinate in latitude.
OriginCoordLat string `url:"originCoordLat,omitempty"`
// The start coordinate in longitude.
OriginCoordLong string `url:"originCoordLong,omitempty"`
// Indicates whether a trip can start with a walking distance. For distance sharing, min and max number of meters can be specified as 1, [min distance], [max distance].
// Default is 1.
OriginWalk string `url:"originWalk,omitempty"`
// Indicates whether stops / stations passed on the trip should be retrieved. Default 0.
Passlist int `url:"passlist,omitempty"`
// Indicates whether detailed routes should be calculated for the results. 0 or 1. Default is 0.
Poly int `url:"poly,omitempty"`
// Combination value of desired traffic mode if not all will be used when traveling.
Products int `url:"products,omitempty"`
// By default, you are searching for the time you want the trip to resign.
// By setting searchForArrival = 1, you will instead travel based on the time you want to reach.
// Default = 0.
SearchForArrival int `url:"searchForArrival,omitempty"`
// Time. Example hour = 19:06. Default now.
Time string `url:"time,omitempty"`
// The station to pass through the station. Example: 300109600, 9600.
ViaID string `url:"viaId,omitempty"`
// Number of minutes to be spent on the via station indicated by ViaID.
ViaWaitTime int `url:"viaWaitTime,omitempty"`
}
// Trip does a trip request to SL API and response with the trip list or a error.
func (s *TravelPlannerService) Trip(ctx context.Context, opt *TripOptions) ([]*Trip, error) {
if len(opt.Key) == 0 {
return nil, ErrNoKey
}
r, err := addOptions(fmt.Sprintf(travelPlannerEndpoint, "trip"), opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", r, nil)
if err != nil {
return nil, err
}
var resp *TripResponseData
if _, err := s.client.Do(ctx, req, &resp); err != nil {
return nil, err
}
if len(resp.ErrorText) > 0 {
return nil, errors.New(resp.ErrorText)
}
if len(resp.Message) > 0 {
return nil, errors.New(resp.Message)
}
return resp.Trip, nil
}
// Journey represents a journey.
type Journey struct {
ErrorCode string `json:"errorCode"`
ErrorText string `json:"errorText"`
Message string `json:"Message"`
Directions struct {
Direction []struct {
RouteIdxFrom int `json:"routeIdxFrom"`
RouteIdxTo int `json:"routeIdxTo"`
Value string `json:"value"`
} `json:"Direction"`
} `json:"Directions"`
JourneyStatus string `json:"JourneyStatus"`
Names struct {
Name []struct {
Product struct {
Admin string `json:"admin"`
CatCode string `json:"catCode"`
CatIn string `json:"catIn"`
CatOut string `json:"catOut"`
CatOutL string `json:"catOutL"`
CatOutS string `json:"catOutS"`
Line string `json:"line"`
Name string `json:"name"`
Num string `json:"num"`
Operator string `json:"operator"`
OperatorCode string `json:"operatorCode"`
} `json:"Product"`
Category string `json:"category"`
Name string `json:"name"`
Number string `json:"number"`
RouteIdxFrom int `json:"routeIdxFrom"`
RouteIdxTo int `json:"routeIdxTo"`
} `json:"Name"`
} `json:"Names"`
ServiceDays []struct {
SDaysB string `json:"sDaysB"`
SDaysI string `json:"sDaysI"`
SDaysR string `json:"sDaysR"`
} `json:"ServiceDays"`
Stops struct {
Stop []struct {
DepDate string `json:"depDate"`
DepPrognosisType string `json:"depPrognosisType"`
DepTime string `json:"depTime"`
DepTrack string `json:"depTrack"`
ExtID string `json:"extId"`
HasMainMast bool `json:"hasMainMast"`
ID string `json:"id"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
MainMastExtID string `json:"mainMastExtId"`
MainMastID string `json:"mainMastId"`
Name string `json:"name"`
RouteIdx int `json:"routeIdx"`
} `json:"Stop"`
} `json:"Stops"`
LastPassRouteIdx int `json:"lastPassRouteIdx"`
LastPassStopRef int `json:"lastPassStopRef"`
Ref string `json:"ref"`
}
// JourneyOptions specifies optional parameters to the TravelPlannerService.Journey.
type JourneyOptions struct {
// Trip date. Example: 2014-08-23. Default is today.
Date string `url:"date,omitempty"`
// The reference from Trip, see above.
ID string `url:"id,omitempty"`
// API Key.
Key string `url:"key,omitempty"`
// Indicates whether detailed routes should be calculated for the results. 0 or 1. Default is 0.
Poly int `url:"poly,omitempty"`
}
// Journey does a journey request to SL API and response with the journey list or a error.
func (s *TravelPlannerService) Journey(ctx context.Context, opt *JourneyOptions) (*Journey, error) {
if len(opt.Key) == 0 {
return nil, ErrNoKey
}
r, err := addOptions(fmt.Sprintf(travelPlannerEndpoint, "journeydetail"), opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", r, nil)
if err != nil {
return nil, err
}
var resp *Journey
if _, err := s.client.Do(ctx, req, &resp); err != nil {
return nil, err
}
if len(resp.ErrorText) > 0 {
return nil, errors.New(resp.ErrorText)
}
if len(resp.Message) > 0 {
return nil, errors.New(resp.Message)
}
return resp, nil
}
// ReconstructionOptions specifies optional parameters to the TravelPlannerService.Reconstruction.
type ReconstructionOptions struct {
// Trip date. Example: 2014-08-23. Default is today.
Date string `url:"date,omitempty"`
// The value I CtxRecon as I get the response from travel.
Ctx string `url:"ctx,omitempty"`
// API Key.
Key string `url:"key,omitempty"`
// Indicates whether detailed routes should be calculated for the results. 0 or 1. Default is 0.
Poly int `url:"poly,omitempty"`
}
// Reconstruction does a reconstruction request to SL API and response with a trip or a error.
func (s *TravelPlannerService) Reconstruction(ctx context.Context, opt *ReconstructionOptions) (*Trip, error) {
if len(opt.Key) == 0 {
return nil, ErrNoKey
}
r, err := addOptions(fmt.Sprintf(travelPlannerEndpoint, "reconstruction"), opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", r, nil)
if err != nil {
return nil, err
}
var resp *TripResponseData
if _, err := s.client.Do(ctx, req, &resp); err != nil {
return nil, err
}
if len(resp.ErrorText) > 0 {
return nil, errors.New(resp.ErrorText)
}
if len(resp.Message) > 0 {
return nil, errors.New(resp.Message)
}
if len(resp.Trip) == 0 {
return nil, ErrNoTripFound
}
return resp.Trip[0], nil
}