move location overview to positions.go

This commit is contained in:
aaron
2024-08-30 11:09:18 +02:00
parent 8854cf167e
commit 75fda147ee

View File

@@ -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
}