Code documentation
This commit is contained in:
parent
ae431b71b5
commit
49cd6008b4
1 changed files with 7 additions and 0 deletions
7
main.go
7
main.go
|
@ -293,10 +293,12 @@ func generateURL(domain string, v2 bool, location string, lang string, format st
|
||||||
return link, headers
|
return link, headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We create a weather struct with the url
|
||||||
type weather struct {
|
type weather struct {
|
||||||
url string
|
url string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `weather` method to handle creating new requests with proper headers
|
||||||
func (w weather) newRequest(headers map[string]string) *http.Request {
|
func (w weather) newRequest(headers map[string]string) *http.Request {
|
||||||
req, err := http.NewRequest("GET", w.url, nil)
|
req, err := http.NewRequest("GET", w.url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -305,6 +307,7 @@ func (w weather) newRequest(headers map[string]string) *http.Request {
|
||||||
return w.formatHeader(req, headers)
|
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 {
|
func (w weather) formatHeader(req *http.Request, headers map[string]string) *http.Request {
|
||||||
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
// I had to read the wttr.in code to figure this one out.
|
// 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
|
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) {
|
func (w weather) get(req *http.Request, download bool) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
client.Jar = nil
|
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) {
|
func (w weather) print(resp *http.Response) {
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -342,6 +348,7 @@ func (w weather) print(resp *http.Response) {
|
||||||
fmt.Printf("%s\n", body)
|
fmt.Printf("%s\n", body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `weather` method to download the weather
|
||||||
func (w weather) download(resp *http.Response) {
|
func (w weather) download(resp *http.Response) {
|
||||||
splitURL := strings.Split(w.url, "/")
|
splitURL := strings.Split(w.url, "/")
|
||||||
filename := splitURL[len(splitURL)-1]
|
filename := splitURL[len(splitURL)-1]
|
||||||
|
|
Loading…
Reference in a new issue