-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_api.go
65 lines (54 loc) · 1.34 KB
/
external_api.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
const (
baseURL = "https://opendata.cwb.gov.tw/api/v1" // 中央氣象局開放資料平臺
path = "/rest/datastore/F-D0047-063" // 臺北市未來1週天氣預報
locField = "locationName"
)
var (
queryParams = map[string]string{
"Authorization": "CWB-B565A7A8-D4E7-4CBC-8DA9-3CD30B123027",
"elementName": "WeatherDescription",
}
)
func requestWeatherInLoc(location string) error {
queryParams[locField] = location
req, err := http.NewRequest("GET", baseURL+path, nil)
if err != nil {
fmt.Println(err)
return err
}
q := req.URL.Query()
for k, v := range queryParams {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
fmt.Println(time.Now(), "Request URL:", req.URL.String())
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
return err
}
respBody := &respBody{}
if err := json.NewDecoder(resp.Body).Decode(respBody); err != nil {
fmt.Println(err)
return err
}
if locs := respBody.Records.Locations; len(locs) > 0 {
if loc := locs[0].Locations; len(loc) > 0 {
if we := loc[0].WeatherElements; len(we) > 0 {
if td := we[0].TimeData; len(td) > 0 {
if ev := td[0].ElementValues; len(ev) > 0 {
fmt.Println("Weather in", location, "in next 12 hours:", ev[0].Value)
}
}
}
}
}
return nil
}