32 lines
720 B
Go
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
|
|
}
|