-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
62 lines (58 loc) · 1.46 KB
/
http.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
// setQueryRange returns the query range t1 -> t2 and an error
// analyzes input to deterine if query is for single day, month, or year
func setQueryRange(t string, dur string) (string, string, error) {
t1, t2 := "", ""
switch dur {
case "day":
t1 = t + "T00:00:00"
tp, err := time.Parse(time.RFC3339, t+"T00:00:00Z")
if err != nil {
return "", "", err
}
t2 = strings.Split(fmt.Sprint(tp.AddDate(0, 0, 1)), " ")[0] + "T00:00:00"
case "month":
t1 = t + "T00:00:00"
tp, err := time.Parse(time.RFC3339, t1+"Z")
if err != nil {
return "", "", err
}
t2 = strings.Split(fmt.Sprint(tp.AddDate(0, 1, 0)), " ")[0] + "T00:00:00"
case "year":
t1 = t + "T00:00:00"
tp, err := time.Parse(time.RFC3339, t1+"Z")
if err != nil {
return "", "", err
}
t2 = strings.Split(fmt.Sprint(tp.AddDate(1, 0, 0)), " ")[0] + "T00:00:00"
}
return t1, t2, nil
}
func updateFire(t1 string, t2 string) ([]FireCall, error) {
f := []FireCall{}
api := "https://data.seattle.gov/resource/grwu-wqtk.json?$limit=1000000&$where="
queryString := url.QueryEscape("datetime between '" + t1 + "' and '" + t2 + "'")
resp, err := http.Get(api + queryString)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &f)
if err != nil {
return nil, err
}
return f, nil
}