-
Notifications
You must be signed in to change notification settings - Fork 2
/
scope.go
103 lines (80 loc) · 4.25 KB
/
scope.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
package navitia
import (
"golang.org/x/net/context"
"github.com/govitia/navitia/types"
)
// A Scope is a coverage-scoped question, allowing you to query information about a specific region.
//
// It is needed for every non-global request you wish to make, and helps have better results with some global request too!
type Scope struct {
region types.ID
session *Session
}
// ArrivalsSA requests the arrivals for a given StopArea in a given region.
func (scope *Scope) ArrivalsSA(ctx context.Context, req ConnectionsRequest, resource types.ID) (*ConnectionsResults, error) {
// Create the URL
scopeURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/stop_areas/" + string(resource) + "/" + arrivalsEndpoint
return scope.session.connections(ctx, scopeURL, req)
}
// ArrivalsSP requests the arrivals for a given StopPoint in a given region.
func (scope *Scope) ArrivalsSP(ctx context.Context, req ConnectionsRequest, resource types.ID) (*ConnectionsResults, error) {
// Create the URL
scopeURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/stop_points/" + string(resource) + "/" + arrivalsEndpoint
return scope.session.connections(ctx, scopeURL, req)
}
// ArrivalsC requests the arrivals from a point described by coordinates.
func (s *Session) ArrivalsC(ctx context.Context, req ConnectionsRequest, coords types.Coordinates) (*ConnectionsResults, error) {
// Create the URL
coordsQ := string(coords.ID())
scopeURL := s.APIURL + "/coverage/" + coordsQ + "/coords/" + coordsQ + "/" + arrivalsEndpoint
return s.connections(ctx, scopeURL, req)
}
// Departures computes a list of Departures according to the parameters given in a specific scope
func (scope *Scope) Departures(ctx context.Context, req DeparturesRequest) (*DeparturesResults, error) {
// there is a special case for departures stop areas, it needs to be added before any parameters
filterByVJ := ""
if req.StopArea != "" {
filterByVJ = "stop_areas/" + req.StopArea
}
// Create the URL
reqURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/" + filterByVJ + "/" + departuresEndpoint
return scope.session.departures(ctx, reqURL, req)
}
// DeparturesSA requests the departures for a given StopArea
func (scope *Scope) DeparturesSA(ctx context.Context, req ConnectionsRequest, resource types.ID) (*ConnectionsResults, error) {
// Create the URL
scopeURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/stop_areas/" + string(resource) + "/" + departuresEndpoint
return scope.session.connections(ctx, scopeURL, req)
}
// DeparturesSP requests the departures for a given StopPoint
func (scope *Scope) DeparturesSP(ctx context.Context, req ConnectionsRequest, resource types.ID) (*ConnectionsResults, error) {
// Create the URL
scopeURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/stop_points/" + string(resource) + "/" + departuresEndpoint
return scope.session.connections(ctx, scopeURL, req)
}
// Journeys computes a list of journeys according to the parameters given in a specific scope
func (scope *Scope) Journeys(ctx context.Context, req JourneyRequest) (*JourneyResults, error) {
// Create the URL
reqURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/" + journeysEndpoint
// Call
return scope.session.journeys(ctx, reqURL, req)
}
// Places searches in all geographical objects within a coverage using their names, returning a list of places.
// It is context aware.
func (scope *Scope) Places(ctx context.Context, params PlacesRequest) (*PlacesResults, error) {
// Create the URL
reqURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/" + placesEndpoint
// Call
return scope.session.places(ctx, reqURL, params)
}
// VehicleJourneys computes a list of VehicleJourneys according to the parameters given in a specific scope
func (scope *Scope) VehicleJourneys(ctx context.Context, req VehicleJourneyRequest) (*VehicleJourneyResults, error) {
// there is a special case for vehicle journey ID, it needs to be added before any parameters
filterByVJ := ""
if req.ID != "" {
filterByVJ = "/" + string(req.ID)
}
// Create the URL
reqURL := scope.session.APIURL + "/coverage/" + string(scope.region) + "/" + vehicleJourneysEndpoint + filterByVJ
return scope.session.vehicleJourneys(ctx, reqURL, req)
}