From 7e46b705af59f54fd04c1e604ef2065af2bdaf7f Mon Sep 17 00:00:00 2001 From: aaron Date: Fri, 30 Aug 2024 11:09:52 +0200 Subject: [PATCH] introduce partners and stations and main.go --- publibike/api/partners.go | 31 +++++++++++++ publibike/api/stations.go | 31 +++++++++++++ publibike/main.go | 7 +++ publibike/station_locations.go | 47 ------------------- publibike/stations.go | 83 ---------------------------------- 5 files changed, 69 insertions(+), 130 deletions(-) create mode 100644 publibike/api/partners.go create mode 100644 publibike/api/stations.go create mode 100644 publibike/main.go delete mode 100644 publibike/station_locations.go delete mode 100644 publibike/stations.go diff --git a/publibike/api/partners.go b/publibike/api/partners.go new file mode 100644 index 0000000..5846558 --- /dev/null +++ b/publibike/api/partners.go @@ -0,0 +1,31 @@ +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 +} diff --git a/publibike/api/stations.go b/publibike/api/stations.go new file mode 100644 index 0000000..5846558 --- /dev/null +++ b/publibike/api/stations.go @@ -0,0 +1,31 @@ +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 +} diff --git a/publibike/main.go b/publibike/main.go new file mode 100644 index 0000000..fb7d795 --- /dev/null +++ b/publibike/main.go @@ -0,0 +1,7 @@ +package publibike + +import "fmt" + +func main() { + fmt.Println("test") +} diff --git a/publibike/station_locations.go b/publibike/station_locations.go deleted file mode 100644 index 942acdc..0000000 --- a/publibike/station_locations.go +++ /dev/null @@ -1,47 +0,0 @@ -package publibike - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" -) - -// 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"` -} - -// State struct to store station state -type State struct { - Id int `json:"id"` - Name string `json:"name"` -} - -// 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 stationlocs []StationLoc - err = json.Unmarshal(body, &stationlocs) - if err != nil { - return nil, fmt.Errorf("failed to unmarshal station: %w", err) - } - - return station, nil -} diff --git a/publibike/stations.go b/publibike/stations.go deleted file mode 100644 index 68877e9..0000000 --- a/publibike/stations.go +++ /dev/null @@ -1,83 +0,0 @@ -package publibike - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" -) - -// StationLoc struct to store station data -type Station 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"` -} - -// Representation of a state -type State struct { - Id int `json:"id"` - Name string `json:"name"` -} - -// 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"` -} - -// Representation of a type -type VehicleType struct { - Id int `json:"id"` - Name string `json:"name"` -} - -// 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} -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 -}