-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast.go
41 lines (37 loc) · 989 Bytes
/
forecast.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
package gooby
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type prediction struct {
Quantity float64 `json:"quantity"`
Std float64 `json:"std"`
}
// ObForecast is used for unmarshaling forecast responses from the OpenBikes API
type ObForecast struct {
Bikes prediction `json:"bikes"`
City string `json:"city"`
Stands prediction `json:"spaces"`
Station string `json:"station"`
Status string `json:"status"`
Timestamp float64 `json:"timestamp"`
}
// Forecast for a station at a certain time
func Forecast(city, station string, timestamp int64) ObForecast {
var url = fmt.Sprintf("http://openbikes.co/api/prediction/%s/%s/%d", city, station, timestamp)
var res, err = http.Get(url)
if err != nil {
panic(err)
}
defer res.Body.Close()
var forecast ObForecast
// Read bytes
data, err := ioutil.ReadAll(res.Body)
// Unmarshal
if err == nil && data != nil {
err = json.Unmarshal(data, &forecast)
}
return forecast
}