From 7f08573637767b391241b8b807f26125b2d90ec2 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 8 Jul 2022 22:12:21 -0400 Subject: [PATCH] Added basic weather fetching --- cmd/client/weather.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/cmd/client/weather.go b/cmd/client/weather.go index 2348349..b33e766 100644 --- a/cmd/client/weather.go +++ b/cmd/client/weather.go @@ -1,9 +1,34 @@ package main -import "sync" +import ( + "encoding/json" + "io/ioutil" + "log" + "net/http" +) -func getToday(wg *sync.WaitGroup, cfg *config) { +func getCurrent(app *application) { - // errMsg := "\nSorry, couldn't get today's weather. Perhaps you've been rate limited?" + lat := "lat=" + app.Config.Latitude + lon := "&lon=" + app.Config.Longitude + units := "&units=" + app.Config.Units + key := "&appid=" + app.Config.ApiKey + url := "https://pro.openweathermap.org/data/2.5/weather?" + lat + lon + units + key + res, err := http.Get(url) + if err != nil { + log.Println(err) + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Println(err) + } + + err = json.Unmarshal(body, &app.Forecast) + if err != nil { + log.Println(err) + } + app.WaitGroup.Done() }