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
This commit is contained in:
parent
f184474e60
commit
ae431b71b5
1 changed files with 42 additions and 23 deletions
49
main.go
49
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,7 +328,22 @@ func getWheather(url string, headers map[string]string, download bool) {
|
|||
defer resp.Body.Close()
|
||||
|
||||
if download {
|
||||
splitURL := strings.Split(url, "/")
|
||||
w.download(resp)
|
||||
} else {
|
||||
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)
|
||||
|
@ -334,14 +353,14 @@ func getWheather(url string, headers map[string]string, download bool) {
|
|||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
} else {
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal("Error reading body. ", err)
|
||||
}
|
||||
fmt.Printf("%s\n", 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.
|
||||
|
|
Loading…
Reference in a new issue