From 49cd6008b4a2539abdb40f17ca00ab23557ce571 Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Tue, 25 Feb 2020 22:51:56 +0100 Subject: [PATCH] Code documentation --- main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main.go b/main.go index 7afd844..0424dcc 100644 --- a/main.go +++ b/main.go @@ -293,10 +293,12 @@ func generateURL(domain string, v2 bool, location string, lang string, format st return link, headers } +// We create a weather struct with the url type weather struct { url string } +// `weather` method to handle creating new requests with proper headers func (w weather) newRequest(headers map[string]string) *http.Request { req, err := http.NewRequest("GET", w.url, nil) if err != nil { @@ -305,6 +307,7 @@ func (w weather) newRequest(headers map[string]string) *http.Request { return w.formatHeader(req, headers) } +// `weather` method to format the headers and inject required ones 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. @@ -317,6 +320,8 @@ func (w weather) formatHeader(req *http.Request, headers map[string]string) *htt return req } +// `weather` method to create a client and get a weather response +// This method handles printing or downloading the weather func (w weather) get(req *http.Request, download bool) { client := &http.Client{} client.Jar = nil @@ -334,6 +339,7 @@ func (w weather) get(req *http.Request, download bool) { } } +// `weather` method to print the weather func (w weather) print(resp *http.Response) { body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -342,6 +348,7 @@ func (w weather) print(resp *http.Response) { fmt.Printf("%s\n", body) } +// `weather` method to download the weather func (w weather) download(resp *http.Response) { splitURL := strings.Split(w.url, "/") filename := splitURL[len(splitURL)-1]