From 802cb7f193c9966ea18a749c1487a95b0e922e67 Mon Sep 17 00:00:00 2001 From: aaron Date: Thu, 29 Aug 2024 23:52:27 +0200 Subject: [PATCH] add station objects and parser --- publibike/stations.go | 66 ++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/publibike/stations.go b/publibike/stations.go index c81b893..f6826b7 100644 --- a/publibike/stations.go +++ b/publibike/stations.go @@ -7,42 +7,56 @@ import ( "net/http" ) -// Station struct to store station data -type Station struct { - Id int `json:"id"` - Latitude float64 `json:"latitude"` - Longitude float64 `json:"longitude"` - State State `json:"state"` +// StationLoc struct to store station data +type StationLoc struct { + Id int `json:"id"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` + State State `json:"state"` + Name string `json:"name"` + address string `json:"address"` + zip string `json:"zip"` + city string `json:"city"` + Vehicles Vehicles `json:"vehicles"` + Network Network `json:"network"` + Sponsors Sponsors `json:"sponsors"` } -// State struct to store station state +// Representation of a state type State struct { Id int `json:"id"` Name string `json:"name"` } -// GetAllStations fetches data from /public/stations -func GetAllStations() ([]Station, 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() +// Representation of a vehicle +type Vehicles struct { + Id int `json:"id"` + name string `json:"name"` + ebike_battery_level float64 `json:"ebike_battery_level"` + VehicleType VehicleType `json:"type"` +} - // Parse the JSON body - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return nil, fmt.Errorf("failed to read response body: %w", err) - } +// Representation of a type +type VehicleType struct { + Id int `json:"id"` + Name string `json:"name"` +} - var stations []Station - err = json.Unmarshal(body, &stations) - if err != nil { - return nil, fmt.Errorf("failed to unmarshal station: %w", err) - } +// Representation of a network +type Network struct { + Id int `json:"id"` + Name string `json:"name"` + BackgroundImg string `json:"background_img"` + LogoImg string `json:"logo_img"` + Sponsors Sponsors `json:"sponsors"` +} - return station, nil +// Represents a sponsor item containing an image and link (translated) +type Sponsors struct { + Id int `json:"id"` + Name string `json:"name"` + Image string `json:"image"` + Url string `json:"url"` } // GetStationByID fetches data from /public/stations/{id}