-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
73 lines (67 loc) · 1.94 KB
/
fetch.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
package ldb
import (
"bytes"
"encoding/xml"
"flag"
"io/ioutil"
"net/http"
)
// There's only ever going to be one access_token; use a flag.
var accessToken = flag.String("ldb_token", "", "National Rail LDB service access token")
var serviceAddress = flag.String("ldb_address", "", "National Rail LDB service address")
// NewGetDepartureBoardRequest returns an HTTP Request object ready to send to
// the LDB API to query services departing from and arriving at the specified
// stations.
func NewGetDepartureBoardRequest(rows int, from string, to string) (*http.Request, error) {
env := &envelope{
EncodingStyle: "http://schemas.xmlsoap.org/soap/encoding",
Header: &envelopeHeader{
AccessToken: &token{
TokenValue: *accessToken,
},
},
Body: &envelopeBody{
GetDepartureBoardRequest: &getDepartureBoardRequest{
NumRows: rows,
CRS: from,
FilterCRS: to,
},
},
}
return newRequest(env, "http://thalesgroup.com/RTTI/2012-01-13/ldb/GetDepartureBoard")
}
// ParseGetDepartureBoardResponse parses the response returned by the LDB API.
func ParseGetDepartureBoardResponse(resp *http.Response) (*GetDepartureBoardResponse, error) {
if env, err := parseEnvelope(resp); err != nil {
return nil, err
} else {
return env.Body.GetDepartureBoardResponse, nil
}
}
func newRequest(envelope *envelope, action string) (*http.Request, error) {
bytes := &bytes.Buffer{}
encoder := xml.NewEncoder(bytes)
if err := encoder.Encode(envelope); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", *serviceAddress, bytes)
if err != nil {
return nil, err
}
req.Header.Add("SOAPAction", action)
req.Header.Add("Content-Type", "text/xml")
return req, nil
}
func parseEnvelope(resp *http.Response) (*envelope, error) {
env := &envelope{}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(body, env)
if err != nil {
return nil, err
}
return env, nil
}