diff --git a/cmd/api/irail-api.go b/cmd/api/irail-api.go index 744ca03..21f3cd6 100644 --- a/cmd/api/irail-api.go +++ b/cmd/api/irail-api.go @@ -1,7 +1,5 @@ package api -// https://docs.irail.be/ - import ( "encoding/json" "fmt" @@ -9,14 +7,11 @@ import ( "net/http" ) -// https://docs.irail.be/#liveboard-liveboard-api-get - -func GetSNCBStationTimeTable(stationName string, time string, arrdep string) ([]byte, error) { - url := fmt.Sprintf("https://api.irail.be/liveboard/?station=%s&lang=nl&format=json", stationName) - +// makeAPIRequest is a generic function to make HTTP GET requests +func makeAPIRequest(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { - return nil, err + return nil, fmt.Errorf("error making request: %v", err) } defer func(Body io.ReadCloser) { err := Body.Close() @@ -27,12 +22,17 @@ func GetSNCBStationTimeTable(stationName string, time string, arrdep string) ([] body, err := io.ReadAll(resp.Body) if err != nil { - return nil, err + return nil, fmt.Errorf("error reading response body: %v", err) } - return body, nil } +// GetSNCBStationTimeTable fetches the timetable for a specific station +func GetSNCBStationTimeTable(stationName string, time string, arrdep string) ([]byte, error) { + url := fmt.Sprintf("https://api.irail.be/liveboard/?station=%s&lang=nl&format=json", stationName) + return makeAPIRequest(url) +} + // ParseiRailDepartures handles fetching of timetable departures func ParseiRailDepartures(jsonData []byte) ([]TimetableDeparture, error) { var response StationTimetableResponse @@ -40,40 +40,13 @@ func ParseiRailDepartures(jsonData []byte) ([]TimetableDeparture, error) { if err != nil { return nil, fmt.Errorf("failed to unmarshal JSON: %v - input data: %s", err, string(jsonData)) } - return response.Departures.Departure, nil } -func GetSNCBStationsJSON() []byte { +// GetSNCBStationsJSON fetches the list of SNCB stations +func GetSNCBStationsJSON() ([]byte, error) { const url string = "https://api.irail.be/stations/?format=json&lang=nl" - req, err := http.NewRequest("GET", url, nil) - if err != nil { - fmt.Println("Error creating request:", err) - return nil - } - - client := &http.Client{} - - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error making request:", err) - return nil - } - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - fmt.Println("Error closing response body:", err) - } - }(resp.Body) - - // Read the response body - body, err := io.ReadAll(resp.Body) - if err != nil { - fmt.Println("Error reading response body:", err) - return nil - } - - return body + return makeAPIRequest(url) } type Station struct { @@ -82,17 +55,14 @@ type Station struct { StandardName string `json:"standardname"` } -var result struct { - Stations []Station `json:"station"` -} - -// https://docs.irail.be/#stations-stations-api-get - +// ParseStations parses the JSON data of stations func ParseStations(jsonData []byte) ([]Station, error) { + var result struct { + Stations []Station `json:"station"` + } err := json.Unmarshal(jsonData, &result) if err != nil { return nil, fmt.Errorf("failed to unmarshal JSON: %v - input data: %s", err, string(jsonData)) } - return result.Stations, nil } diff --git a/main.go b/main.go index edee969..c331e74 100644 --- a/main.go +++ b/main.go @@ -73,7 +73,7 @@ func handleConnection(stationFrom string, stationTo string) { } func handleSearch() { - stationsJSON := api.GetSNCBStationsJSON() + stationsJSON, err := api.GetSNCBStationsJSON() stations, err := api.ParseStations(stationsJSON) if err != nil { panic(err)