-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (76 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
)
const (
apiURL = "https://weather.theonlinekenyan.com/api/forecast/"
apiOkResponse = 200
)
type data struct {
Day string `json:"Day"`
ForcastDay struct {
Conditions string `json:"Conditions"`
High struct {
Celsius string `json:"Celsius"`
} `json:"High"`
Low struct {
Celsius string `json:"Celsius"`
} `json:"Low"`
} `json:"ForecastDay"`
}
type response struct {
Status int `json:"Status"`
Success bool `json:"Success"`
Message string `json:"Message"`
Data []*data `json:"Data"`
}
func main() {
town, err := parseTown()
if err != nil {
log.Fatal(err.Error())
}
endpoint := apiURL + strings.ToLower(town)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
log.Fatalf("failed to create request: %s", err.Error())
}
client := http.DefaultClient
client.Timeout = 10 * time.Second
res, err := client.Do(req)
if err != nil {
log.Fatalf("failed request to %s: %s", endpoint, err.Error())
}
reqResponse := &response{}
err = json.NewDecoder(res.Body).Decode(reqResponse)
res.Body.Close()
if err != nil {
log.Fatalf("failed to decode response body: %s", err.Error())
}
if reqResponse.Status != apiOkResponse {
log.Fatalf("received unexpected response status. expected %d, got %d for %s town", apiOkResponse, reqResponse.Status, town)
}
data := reqResponse.Data[0]
const shortForm = "2006-01-02 00:00:00"
t, err := time.Parse(shortForm, data.Day)
if err != nil {
log.Fatalf("Failed to parse day: %s", err.Error())
}
fmt.Printf("Town: %s\n", town)
fmt.Printf("Date: %s\n", t.Format("02 Jan 2006"))
fmt.Printf("Conditions: %s\n", data.ForcastDay.Conditions)
fmt.Printf("Temprature: %s Celsius / %s Celsius\n", data.ForcastDay.High.Celsius, data.ForcastDay.Low.Celsius)
}
func parseTown() (string, error) {
townArg := os.Args[1]
townVal := os.Args[2]
if townArg != "-town" {
return "", fmt.Errorf("unknown argument passed %s. Argument should be in format `weather -town {Town}`", townArg)
}
return townVal, nil
}