-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
177 lines (156 loc) · 3.8 KB
/
api.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
package femtioelva
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
var (
// A square grid on top of Gothenburg
BOX = GeoBox(GBG_LAT, GBG_LON, 10_000)
)
type oAuth2Response struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
// other fields are not relevant yet.
}
// apiCall wraps the vasttrafik API, does basic error checking on the response.
// If the response is non-nil, the caller is responsible for closing its body.
func apiCall(method, path string, headers, params map[string]string) (*http.Response, error) {
url := &url.URL{
Scheme: "https",
Host: "api.vasttrafik.se",
Path: path,
}
req, err := http.NewRequest(method, url.String(), nil)
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Set(k, v)
}
q := req.URL.Query()
for k, v := range params {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
// request - let caller check error and defer close
log.Debug(req.URL.String())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return resp, err
}
if resp.StatusCode != http.StatusOK {
raw, _ := io.ReadAll(resp.Body)
log.Debug(string(raw))
resp.Body.Close()
return nil, fmt.Errorf("HTTP request not ok: %s", resp.Status)
}
return resp, nil
}
func GetAccessToken(apikey string) (string, error) {
secret := base64.URLEncoding.EncodeToString([]byte(apikey))
headers := map[string]string{
"Authorization": "Basic " + secret,
"Content-Type": "application/x-www-form-urlencoded",
}
params := map[string]string{
"format": "json",
"grant_type": "client_credentials",
}
resp, err := apiCall(http.MethodPost, "token", headers, params)
if err != nil {
return "", err
}
defer resp.Body.Close()
// decode
var authResp oAuth2Response
err = json.NewDecoder(resp.Body).Decode(&authResp)
if err != nil {
return "", err
}
if authResp.ExpiresIn < 60 {
return "", errors.New("token will expire in less than a minute")
}
return authResp.AccessToken, nil
}
type Vehicle struct {
// Some kind of ID.
// Occurs across requests but other fields can be different
// even if this is the same. Does it identify a vehicle?
Gid string
// "Y*1e6" in Västtrafiks API, increases north.
Lat float64
// "X*1e6" in Västtrafiks API, increases east.
Long float64
// Common name of transport.
Name string
// When this data was retrieved.
Time time.Time
}
// VasttrafikCoord matches the API which uses stringed int millionths.
func apiCoord(coord float64) string {
return strconv.Itoa(int(1e6 * coord))
}
func GetVehicleLocations(token string, box Box) ([]Vehicle, error) {
headers := map[string]string{
"Authorization": "Bearer " + token,
}
params := map[string]string{
"format": "json",
"miny": apiCoord(box.LowLat),
"maxy": apiCoord(box.HighLat),
"minx": apiCoord(box.LowLong),
"maxx": apiCoord(box.HighLong),
"onlyRealtime": "yes",
}
resp, err := apiCall(http.MethodGet, "bin/rest.exe/v2/livemap", headers, params)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode JSON with some anon. structs.
type VehicleJSON struct {
X string
Y string
Name string
Gid string
}
var lm struct {
Livemap struct {
Vehicles []VehicleJSON
}
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&lm)
if err != nil {
return nil, err
}
// Pack output
vs := []Vehicle{}
for _, v := range lm.Livemap.Vehicles {
// Store as the actual Long/Lat
x, err := strconv.Atoi(v.X)
if err != nil {
return nil, err
}
y, err := strconv.Atoi(v.Y)
if err != nil {
return nil, err
}
vs = append(vs, Vehicle{
Gid: v.Gid,
Lat: float64(y) * 1e-6,
Long: float64(x) * 1e-6,
Name: v.Name,
Time: time.Now(),
})
}
return vs, nil
}