-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextbus.go
111 lines (89 loc) · 2.27 KB
/
nextbus.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
package nextbus
import (
"encoding/xml"
"net/http"
"net/url"
)
type Client struct {
URL string
client *http.Client
}
func NewClient() (*Client, error) {
return &Client{
URL: "http://webservices.nextbus.com/service/publicXMLFeed",
client: http.DefaultClient,
}, nil
}
func (c *Client) AgencyList() (*AgencyListResponse, error) {
var resp AgencyListResponse
err := c.command("agencyList", &resp, nil)
return &resp, err
}
func (c *Client) RouteList(agency string) (*RouteListResponse, error) {
var resp RouteListResponse
err := c.command("routeList", &resp, map[string]string{
"a": agency,
})
return &resp, err
}
func (c *Client) RouteConfig(agency, routeTag string) (*RouteConfigResponse, error) {
var resp RouteConfigResponse
err := c.command("routeConfig", &resp, map[string]string{
"a": agency,
"r": routeTag,
})
return &resp, err
}
func (c *Client) PredictionsForStopTag(agency, routeTag, stopTag string) (*PredictionsResponse, error) {
var resp PredictionsResponse
err := c.command("predictions", &resp, map[string]string{
"a": agency,
"r": routeTag,
"s": stopTag,
})
return &resp, err
}
func (c *Client) PredictionsForStopId(agency, stopId string) (*PredictionsResponse, error) {
var resp PredictionsResponse
err := c.command("predictions", &resp, map[string]string{
"a": agency,
"stopId": stopId,
})
return &resp, err
}
func (c *Client) Schedule(agency, routeTag string) (*ScheduleResponse, error) {
return nil, errNotImplemented
}
func (c *Client) Messages(agency string, routeTags ...string) (*MessagesResponse, error) {
return nil, errNotImplemented
}
func (c *Client) VehicleLocations(agency, routeTag string, lastUpdatedMs uint64) (*VehicleLocationsResponse, error) {
return nil, errNotImplemented
}
func (c *Client) command(name string, r response, params map[string]string) error {
req, err := http.NewRequest("GET", c.URL, nil)
if err != nil {
return err
}
query := url.Values{}
query.Add("command", name)
for k, v := range params {
if v != "" {
query.Add(k, v)
}
}
req.URL.RawQuery = query.Encode()
resp, err := c.client.Do(req)
if err != nil {
return err
}
err = xml.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return err
}
err = r.responseError()
if err != nil {
return err
}
return nil
}