Code documentation

This commit is contained in:
Elia el Lazkani 2020-02-25 22:51:56 +01:00
parent ae431b71b5
commit 49cd6008b4

View file

@ -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]