/
service_request.go
67 lines (55 loc) · 1.61 KB
/
service_request.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type serviceRequest struct {
Address string `json:"address"`
Agency string `json:"agency"`
CreatedDate string `json:"createddate"`
DueDate string `json:"duedate"`
Geolocation struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
} `json:"geolocation"`
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
Method string `json:"methodreceived"`
Neighborhood string `json:"neighborhood"`
CouncilDistrict string `json:"councildistrict"`
PoliceDistrict string `json:"policedistrict"`
ServiceRequestID string `json:"servicerequestnum"`
SRRecordID string `json:"srrecordid"`
Status string `json:"srstatus"`
ServiceName string `json:"srtype"`
StatusDate string `json:"statusdate"`
Zipcode string `json:"zipcode"`
}
func latestRequests(reqURL string, startDate time.Time) ([]serviceRequest, error) {
format := "2006-01-02T15:04:05.999"
timeStr := startDate.Format(format)
v := url.Values{}
v.Set("$where", fmt.Sprintf("statusdate > '%s'", timeStr))
v.Set("$order", "statusdate DESC")
log.Printf("checking time %s", timeStr)
resp, err := http.Get(reqURL + "?" + v.Encode())
if err != nil {
return nil, err
}
defer resp.Body.Close()
var buf strings.Builder
tee := io.TeeReader(resp.Body, &buf)
var requests []serviceRequest
err = json.NewDecoder(tee).Decode(&requests)
if err != nil {
log.Printf("json response: %s", buf.String())
return nil, err
}
return requests, nil
}