From ae431b71b59ea1b143fafcb7143cc0f9e27df7e7 Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Tue, 25 Feb 2020 22:45:50 +0100 Subject: [PATCH] A few code improvements, turns out I didn't need an interface after all. * Created a proper struct for the weather * Created methods to the weather struct to handle weather requests --- main.go | 65 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index d47d074..7afd844 100644 --- a/main.go +++ b/main.go @@ -293,18 +293,19 @@ func generateURL(domain string, v2 bool, location string, lang string, format st return link, headers } -// TODO: -// * Create interfaces to print and save and migrate code to them -// -// Figure out if we need to print or download a PNG image -// and call the server accordingly. -func getWheather(url string, headers map[string]string, download bool) { +type weather struct { + url string +} - req, err := http.NewRequest("GET", url, nil) +func (w weather) newRequest(headers map[string]string) *http.Request { + req, err := http.NewRequest("GET", w.url, nil) if err != nil { log.Fatal("Error reading request. ", err) } + return w.formatHeader(req, headers) +} +func (w weather) formatHeader(req *http.Request, headers map[string]string) *http.Request { req.Header.Set("Content-Type", "text/plain; charset=utf-8") // I had to read the wttr.in code to figure this one out. // It wasn't Golang, it was 2 wasted hours that I won't be getting back. @@ -313,7 +314,10 @@ func getWheather(url string, headers map[string]string, download bool) { for key, value := range headers { req.Header.Add(key, value) } + return req +} +func (w weather) get(req *http.Request, download bool) { client := &http.Client{} client.Jar = nil @@ -324,26 +328,41 @@ func getWheather(url string, headers map[string]string, download bool) { defer resp.Body.Close() if download { - splitURL := strings.Split(url, "/") - filename := splitURL[len(splitURL)-1] - - f, err := os.Create(filename) - if err != nil { - return - } - defer f.Close() - - _, err = io.Copy(f, resp.Body) + w.download(resp) } else { - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal("Error reading body. ", err) - } - fmt.Printf("%s\n", body) + w.print(resp) } } +func (w weather) print(resp *http.Response) { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal("Error reading body. ", err) + } + fmt.Printf("%s\n", body) +} + +func (w weather) download(resp *http.Response) { + splitURL := strings.Split(w.url, "/") + filename := splitURL[len(splitURL)-1] + + f, err := os.Create(filename) + if err != nil { + return + } + defer f.Close() + + _, err = io.Copy(f, resp.Body) +} + +// Wraper function around the weather struct +func getWheather(url string, headers map[string]string, download bool) { + + w := weather{url} + req := w.newRequest(headers) + w.get(req, download) +} + // This is the main function that glues everything together. func main() { var params cmdParams = cmdParams{}