-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.go
57 lines (45 loc) · 1.21 KB
/
dispatch.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
package somersetcountywrapper
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
const URL = "https://api.somersetcounty-me.org/DispatchLog/"
func NewWrapper() *SomersetWrapper {
return &SomersetWrapper{}
}
func (d *SomersetWrapper) GetDispatch(date string) ([]DispatchLog, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", URL, date), nil)
if err != nil {
return nil, err
}
req.Header.Add("ACCEPT", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// This definitely seems weird, but we unmarshal it once with custom strings definitions so we can trim whitespace.
// Then below we remarshal it, and unmarshal it again to the struct we're returning externally.
var dispatchListInternal []dispatchLogInternal
err = json.Unmarshal(body, &dispatchListInternal)
if err != nil {
return nil, err
}
jsonStripped, err := json.Marshal(dispatchListInternal)
if err != nil {
return nil, err
}
var dispatchList []DispatchLog
err = json.Unmarshal(jsonStripped, &dispatchList)
if err != nil {
return nil, err
}
return dispatchList, nil
}