Error handling, api update

This commit is contained in:
Andrew Scott 2022-08-09 01:13:22 -04:00
parent a688c5a1d9
commit 27f7717c8a
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
3 changed files with 9 additions and 9 deletions

View file

@ -24,12 +24,12 @@ func getLocation(app *Application) {
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
log.Println(err) log.Printf("Error reading location from server: %v\n", err)
} }
err = json.Unmarshal(body, &app.Config) err = json.Unmarshal(body, &app.Config)
if err != nil { if err != nil {
log.Println(err) log.Printf("Error reading JSON from server: %v", err)
} }
loc := strings.Split(app.Config.Location, ",") loc := strings.Split(app.Config.Location, ",")

View file

@ -50,12 +50,12 @@ type Date struct {
} }
type Config struct { type Config struct {
Units string `json:"units"` Units string
Location string `json:"loc"` Location string `json:"loc"`
Longitude string `json:"lat"` Longitude string `json:"lat"`
Latitude string `json:"lon"` Latitude string `json:"lon"`
Date Date Date Date
ApiKey string `json:"appid"` ApiKey string
} }
type Application struct { type Application struct {

View file

@ -15,22 +15,22 @@ func getCurrent(app *Application) {
lon := "&lon=" + app.Config.Longitude lon := "&lon=" + app.Config.Longitude
units := "&units=" + app.Config.Units units := "&units=" + app.Config.Units
key := "&appid=" + app.Config.ApiKey key := "&appid=" + app.Config.ApiKey
url := "https://pro.openweathermap.org/data/2.5/weather?" + lat + lon + units + key url := "https://api.openweathermap.org/data/2.5/weather?" + lat + lon + units + key
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
log.Println(err) log.Printf("Error getting current forecast: %v", err)
} }
defer res.Body.Close() defer res.Body.Close()
fmt.Println(res.Body) fmt.Println(res.Body)
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
log.Println(err) log.Printf("Error reading response from server: %v\n", err)
} }
err = json.Unmarshal(body, &app.Forecast) err = json.Unmarshal(body, &app.Forecast)
if err != nil { if err != nil {
log.Println(err) log.Printf("Error reading JSON from server: %v\n", err)
} }
} }
@ -41,7 +41,7 @@ func getCurrentByLoc(app *Application) {
zip := app.Config.Location zip := app.Config.Location
units := "&units=" + app.Config.Units units := "&units=" + app.Config.Units
key := "&appid=" + app.Config.ApiKey key := "&appid=" + app.Config.ApiKey
url := "https://pro.openweathermap.org/data/2.5/weather?q=" + zip + units + key url := "https://api.openweathermap.org/data/2.5/weather?q=" + zip + units + key
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {