aboutsummaryrefslogtreecommitdiff
path: root/pkg/navitia_api_client
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/navitia_api_client')
-rw-r--r--pkg/navitia_api_client/departures.go16
-rw-r--r--pkg/navitia_api_client/departures_test.go8
2 files changed, 17 insertions, 7 deletions
diff --git a/pkg/navitia_api_client/departures.go b/pkg/navitia_api_client/departures.go
index cd75191..3b7053d 100644
--- a/pkg/navitia_api_client/departures.go
+++ b/pkg/navitia_api_client/departures.go
@@ -5,6 +5,8 @@ import (
"fmt"
"net/http"
"time"
+
+ "git.adyxax.org/adyxax/trains/pkg/model"
)
type DeparturesResponse struct {
@@ -43,14 +45,14 @@ type DeparturesResponse struct {
} `json:"context"`
}
-func (c *Client) GetDepartures(trainStop string) (departures *DeparturesResponse, err error) {
+func (c *Client) GetDepartures(trainStop string) (departures []model.Departure, err error) {
request := fmt.Sprintf("%s/coverage/sncf/stop_areas/%s/departures", c.baseURL, trainStop)
start := time.Now()
c.mutex.Lock()
defer c.mutex.Unlock()
if cachedResult, ok := c.cache[request]; ok {
if start.Sub(cachedResult.ts) < 60*1000*1000*1000 {
- return cachedResult.result.(*DeparturesResponse), nil
+ return cachedResult.result.([]model.Departure), nil
}
}
req, err := http.NewRequest("GET", request, nil)
@@ -62,9 +64,17 @@ func (c *Client) GetDepartures(trainStop string) (departures *DeparturesResponse
return nil, err
}
defer resp.Body.Close()
- if err = json.NewDecoder(resp.Body).Decode(&departures); err != nil {
+ var data DeparturesResponse
+ if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
+ for i := 0; i < len(data.Departures); i++ {
+ t, err := time.Parse("20060102T150405", data.Departures[i].StopDateTime.ArrivalDateTime)
+ if err != nil { // TODO fail better
+ panic(err)
+ }
+ departures = append(departures, model.Departure{data.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05")})
+ }
c.cache[request] = cachedResult{
ts: start,
result: departures,
diff --git a/pkg/navitia_api_client/departures_test.go b/pkg/navitia_api_client/departures_test.go
index e7d771f..9b2f52c 100644
--- a/pkg/navitia_api_client/departures_test.go
+++ b/pkg/navitia_api_client/departures_test.go
@@ -42,8 +42,8 @@ func TestGetDepartures(t *testing.T) {
if err != nil {
t.Fatalf("could not get normal-crepieux departures : %s", err)
}
- if len(departures.Departures) != 10 {
- t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures))
+ if len(departures) != 10 {
+ t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures))
}
// test the cache (assuming the test takes less than 60 seconds (and it really should) it will be accurate)
ts.Close()
@@ -51,7 +51,7 @@ func TestGetDepartures(t *testing.T) {
if err != nil {
t.Fatalf("could not get normal-crepieux departures : %s", err)
}
- if len(departures.Departures) != 10 {
- t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures))
+ if len(departures) != 10 {
+ t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures))
}
}