add stations.go and update gitignore
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,6 +2,9 @@
|
|||||||
# If you prefer the allow list template instead of the deny list, see community template:
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
#
|
#
|
||||||
|
# VIM
|
||||||
|
.*.swp
|
||||||
|
|
||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
*.exe
|
*.exe
|
||||||
*.exe~
|
*.exe~
|
||||||
|
|||||||
69
publibike/stations.go
Normal file
69
publibike/stations.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// State struct to store station 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()
|
||||||
|
|
||||||
|
// 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
|
||||||
|
err = json.Unmarshal(body, &stations)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal station: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return station, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user