-
Notifications
You must be signed in to change notification settings - Fork 0
/
bart.go
53 lines (44 loc) · 1.05 KB
/
bart.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
package bart
import (
"encoding/xml"
"github.com/google/go-querystring/query"
"net/http"
)
const (
baseEndpointUrl = "http://api.bart.gov/api/"
cmdParam = "cmd"
bartApiKey = "MW9S-E7SL-26DU-VV8V"
keyParam = "key"
)
type BartClient struct {
Advisories *AdvisoryService
StationInfo *StationInfoService
RealTimeEstimates *RealTimeEstimateService
}
type BartResponseMeta struct {
Uri string `xml:"uri"`
Date string `xml:"date"`
Time string `xml:"time"`
Message string `xml:"message"`
}
func New() BartClient {
return BartClient{}
}
func makeRequest(endpoint string, command string, requestParams interface{}, response interface{}) error {
v, _ := query.Values(requestParams)
v.Add(cmdParam, command)
v.Add(keyParam, bartApiKey)
values := v.Encode()
url := baseEndpointUrl + endpoint + "?" + values
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
xmlDecoder := xml.NewDecoder(resp.Body)
err = xmlDecoder.Decode(response)
if err != nil {
return err
}
return nil
}