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/client.go2
-rw-r--r--pkg/navitia_api_client/stops.go (renamed from pkg/navitia_api_client/train_stops.go)18
-rw-r--r--pkg/navitia_api_client/stops_test.go (renamed from pkg/navitia_api_client/train_stops_test.go)18
3 files changed, 19 insertions, 19 deletions
diff --git a/pkg/navitia_api_client/client.go b/pkg/navitia_api_client/client.go
index ccd7198..2ca0e5d 100644
--- a/pkg/navitia_api_client/client.go
+++ b/pkg/navitia_api_client/client.go
@@ -11,7 +11,7 @@ import (
type Client interface {
GetDepartures(trainStop string) (departures []model.Departure, err error)
- GetTrainStops() (trainStops []model.TrainStop, err error)
+ GetStops() (trainStops []model.Stop, err error)
}
type NavitiaClient struct {
diff --git a/pkg/navitia_api_client/train_stops.go b/pkg/navitia_api_client/stops.go
index a31ccf7..f0cbcf8 100644
--- a/pkg/navitia_api_client/train_stops.go
+++ b/pkg/navitia_api_client/stops.go
@@ -8,7 +8,7 @@ import (
"git.adyxax.org/adyxax/trains/pkg/model"
)
-type TrainStopsResponse struct {
+type StopsResponse struct {
Pagination struct {
StartPage int `json:"start_page"`
ItemsOnPage int `json:"items_on_page"`
@@ -31,11 +31,11 @@ type TrainStopsResponse struct {
Context interface{} `json:"context"`
}
-func (c *NavitiaClient) GetTrainStops() (trainStops []model.TrainStop, err error) {
- return getTrainStopsPage(c, 0)
+func (c *NavitiaClient) GetStops() (trainStops []model.Stop, err error) {
+ return getStopsPage(c, 0)
}
-func getTrainStopsPage(c *NavitiaClient, i int) (trainStops []model.TrainStop, err error) {
+func getStopsPage(c *NavitiaClient, i int) (trainStops []model.Stop, err error) {
request := fmt.Sprintf("%s/coverage/sncf/stop_areas?count=1000&start_page=%d", c.baseURL, i)
req, err := http.NewRequest("GET", request, nil)
if err != nil {
@@ -47,24 +47,24 @@ func getTrainStopsPage(c *NavitiaClient, i int) (trainStops []model.TrainStop, e
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
- var data TrainStopsResponse
+ var data StopsResponse
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
- return nil, newJsonDecodeError("GetTrainStops ", err)
+ return nil, newJsonDecodeError("GetStops ", err)
}
for i := 0; i < len(data.StopAreas); i++ {
if data.StopAreas[i].Label != "" {
- trainStops = append(trainStops, model.TrainStop{data.StopAreas[i].ID, data.StopAreas[i].Label})
+ trainStops = append(trainStops, model.Stop{data.StopAreas[i].ID, data.StopAreas[i].Label})
}
}
if data.Pagination.ItemsOnPage+data.Pagination.ItemsPerPage*data.Pagination.StartPage < data.Pagination.TotalResult {
- tss, err := getTrainStopsPage(c, i+1)
+ tss, err := getStopsPage(c, i+1)
if err != nil {
return nil, err
}
trainStops = append(trainStops, tss...)
}
} else {
- err = newApiError(resp.StatusCode, "GetTrainStops")
+ err = newApiError(resp.StatusCode, "GetStops")
}
return
}
diff --git a/pkg/navitia_api_client/train_stops_test.go b/pkg/navitia_api_client/stops_test.go
index 9e1c982..0b94765 100644
--- a/pkg/navitia_api_client/train_stops_test.go
+++ b/pkg/navitia_api_client/stops_test.go
@@ -11,12 +11,12 @@ import (
"github.com/stretchr/testify/require"
)
-func TestGetTrainStops(t *testing.T) {
+func TestGetStops(t *testing.T) {
// Simple Test cases
testCases := []struct {
name string
inputNewCLient string
- expected []model.TrainStop
+ expected []model.Stop
expectedError interface{}
}{
{"invalid characters in token should fail", "}", nil, &HttpClientError{}},
@@ -25,7 +25,7 @@ func TestGetTrainStops(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
client := NewClient(tc.inputNewCLient)
- valid, err := client.GetTrainStops()
+ valid, err := client.GetStops()
if tc.expectedError != nil {
require.Error(t, err)
assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
@@ -40,7 +40,7 @@ func TestGetTrainStops(t *testing.T) {
testCasesFilename := []struct {
name string
inputFilename string
- expected []model.TrainStop
+ expected []model.Stop
expectedError interface{}
}{
{"invalid json should fail", "test_data/invalid.json", nil, &JsonDecodeError{}},
@@ -49,7 +49,7 @@ func TestGetTrainStops(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
client, ts := newTestClientFromFilename(t, tc.inputFilename)
defer ts.Close()
- valid, err := client.GetTrainStops()
+ valid, err := client.GetStops()
if tc.expectedError != nil {
require.Error(t, err)
assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
@@ -65,14 +65,14 @@ func TestGetTrainStops(t *testing.T) {
w.WriteHeader(http.StatusNotFound)
}))
client := newTestClient(ts)
- _, err := client.GetTrainStops()
+ _, err := client.GetStops()
if err == nil {
t.Fatalf("404 should raise an error")
}
// normal working request
client, ts = newTestClientFromFilename(t, "test_data/4-train-stops.json")
defer ts.Close()
- trainStops, err := client.GetTrainStops()
+ trainStops, err := client.GetStops()
if err != nil {
t.Fatalf("could not get train stops : %s", err)
}
@@ -87,7 +87,7 @@ func TestGetTrainStops(t *testing.T) {
testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=2", "test_data/4-train-stops-page-2.json"},
})
defer ts.Close()
- trainStops, err = client.GetTrainStops()
+ trainStops, err = client.GetStops()
if err != nil {
t.Fatalf("could not get train stops : %+v", err)
}
@@ -101,7 +101,7 @@ func TestGetTrainStops(t *testing.T) {
testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=1", "test_data/4-train-stops-page-1.json"},
})
defer ts.Close()
- trainStops, err = client.GetTrainStops()
+ trainStops, err = client.GetStops()
if err == nil {
t.Fatalf("should not be able to get train stops : %+v", trainStops)
}