From 75fda147ee35c9aa08066dedc599e2171f59cc01 Mon Sep 17 00:00:00 2001 From: aaron Date: Fri, 30 Aug 2024 11:09:18 +0200 Subject: [PATCH] move location overview to positions.go --- publibike/api/positions.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 publibike/api/positions.go diff --git a/publibike/api/positions.go b/publibike/api/positions.go new file mode 100644 index 0000000..8f50637 --- /dev/null +++ b/publibike/api/positions.go @@ -0,0 +1,33 @@ +package api + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +// GetAllStationLocations fetches data from /public/stations +// Use case: display the location of the stations on a map and whether or not bikes are available. +func GetAllStationLocations() ([]StationLoc, error) { + // Get the public data from the api endpoint + res, err := http.Get("https://api.publibike.ch/v1/public/stations") + if err != nil { + return nil, fmt.Errorf("failed to get stations from api: %w", err) + } + defer res.Body.Close() + + // Parse the JSON body + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + + var stations []StationLoc + err = json.Unmarshal(body, &stations) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal station: %w", err) + } + + return stations, nil +}