aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorJulien Dessaux2021-05-04 11:59:13 +0200
committerJulien Dessaux2021-05-04 12:03:18 +0200
commitdc4fea013c5f89054ae348cab2935eebe28a4712 (patch)
treebf3455ab4e3953489495c5621a40c108d4dba5e3 /pkg
parentUpdated dependencies (diff)
downloadtrains-dc4fea013c5f89054ae348cab2935eebe28a4712.tar.gz
trains-dc4fea013c5f89054ae348cab2935eebe28a4712.tar.bz2
trains-dc4fea013c5f89054ae348cab2935eebe28a4712.zip
Cleaned the navitia - webui calls
Diffstat (limited to '')
-rw-r--r--pkg/model/departures.go6
-rw-r--r--pkg/navitia_api_client/departures.go16
-rw-r--r--pkg/navitia_api_client/departures_test.go8
3 files changed, 23 insertions, 7 deletions
diff --git a/pkg/model/departures.go b/pkg/model/departures.go
new file mode 100644
index 0000000..7359d1b
--- /dev/null
+++ b/pkg/model/departures.go
@@ -0,0 +1,6 @@
+package model
+
+type Departure struct {
+ Direction string
+ Arrival string
+}
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))
}
}