Files
publibike/publibike/api/partners.go
2024-08-30 11:09:52 +02:00

32 lines
720 B
Go

package publibike
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// GetStationByID fetches data from /public/stations/{id}
func GetStationByID(stationID int) (Station, error) {
url := fmt.Sprintf("https://api.publibike.ch/v1/public/stations/%d", stationID)
resp, err := http.Get(url)
if err != nil {
return Station{}, fmt.Errorf("failed to get station: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Station{}, fmt.Errorf("failed to read response body: %w", err)
}
var station Station
err = json.Unmarshal(body, &response)
if err != nil {
return Station{}, fmt.Errorf("failed to unmarshal station: %w", err)
}
return station, nil
}