Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaya-Sem committed Aug 18, 2024
1 parent 66ac959 commit 3a44e15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 48 deletions.
64 changes: 17 additions & 47 deletions cmd/api/irail-api.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package api

// https://docs.irail.be/

import (
"encoding/json"
"fmt"
"io"
"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()
Expand All @@ -27,53 +22,31 @@ 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
err := json.Unmarshal(jsonData, &response)
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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3a44e15

Please sign in to comment.