add station objects and parser
This commit is contained in:
@@ -7,42 +7,56 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Station struct to store station data
|
// StationLoc struct to store station data
|
||||||
type Station struct {
|
type StationLoc struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Latitude float64 `json:"latitude"`
|
Latitude float64 `json:"latitude"`
|
||||||
Longitude float64 `json:"longitude"`
|
Longitude float64 `json:"longitude"`
|
||||||
State State `json:"state"`
|
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 {
|
type State struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllStations fetches data from /public/stations
|
// Representation of a vehicle
|
||||||
func GetAllStations() ([]Station, error) {
|
type Vehicles struct {
|
||||||
// Get the public data from the api endpoint
|
Id int `json:"id"`
|
||||||
res, err := http.Get("https://api.publibike.ch/v1/public/stations")
|
name string `json:"name"`
|
||||||
if err != nil {
|
ebike_battery_level float64 `json:"ebike_battery_level"`
|
||||||
return nil, fmt.Errorf("failed to get stations from api: %w", err)
|
VehicleType VehicleType `json:"type"`
|
||||||
}
|
|
||||||
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 []Station
|
// Representation of a type
|
||||||
err = json.Unmarshal(body, &stations)
|
type VehicleType struct {
|
||||||
if err != nil {
|
Id int `json:"id"`
|
||||||
return nil, fmt.Errorf("failed to unmarshal station: %w", err)
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
return station, nil
|
// 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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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}
|
// GetStationByID fetches data from /public/stations/{id}
|
||||||
|
|||||||
Reference in New Issue
Block a user