move location overview to positions.go
This commit is contained in:
33
publibike/api/positions.go
Normal file
33
publibike/api/positions.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user