-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
locations.go
142 lines (121 loc) · 3.7 KB
/
locations.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package gocorona
import (
"context"
"encoding/json"
"strconv"
"time"
"github.com/pkg/errors"
)
// Coordinates hols coordinates of a location
type Coordinates struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
}
// CaseCountWithTimestamp holds timestamp with CaseCount
type CaseCountWithTimestamp struct {
Timestamp time.Time
CaseCount int
}
// Timeline holds list of Case Counts with timestamp
type Timeline struct {
Data []CaseCountWithTimestamp
}
// LatestWithTimeline struct holds latest count with timelines
type LatestWithTimeline struct {
Latest int `json:"latest"`
Timeline Timeline `json:"timeline"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// coverts json keys from string to unix timestamp and
// it's values as case count.
func (t *LatestWithTimeline) UnmarshalJSON(data []byte) error {
type Alias LatestWithTimeline
aux := struct {
Timeline map[string]int `json:"timeline"`
*Alias
}{
Alias: (*Alias)(t),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
var temp CaseCountWithTimestamp
for k, v := range aux.Timeline {
timestamp, err := time.Parse(time.RFC3339, k)
if err != nil {
return err
}
temp.Timestamp = timestamp
temp.CaseCount = v
t.Timeline.Data = append(t.Timeline.Data, temp)
}
return nil
}
// Timelines holds latest data with timelines
type Timelines struct {
Confirmed LatestWithTimeline `json:"confirmed"`
Deaths LatestWithTimeline `json:"deaths"`
Recovered LatestWithTimeline `json:"recovered"`
}
// Location holds data of a location
type Location struct {
Coordinates Coordinates `json:"coordinates"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
ID int `json:"id"`
Latest Latest `json:"latest"`
Province string `json:"province"`
Timelines Timelines `json:"timelines,omitempty"`
}
// Locations holds response from endpoint /v2/locations
type Locations struct {
Locations []Location `json:"locations"`
}
// GetAllLocationData returns all cases from all locations.
// You can Exclude/Include timelines. Timelines are excluded by default.
func (c Client) GetAllLocationData(ctx context.Context, timelines bool) (data Locations, err error) {
t := "0"
if timelines {
t = "1"
}
endpoint := "/locations" + "?timelines=" + t
if err = c.makeGetRequest(ctx, endpoint, &data); err != nil {
return Locations{}, err
}
return data, nil
}
// GetDataByCountryCode returns all cases from different locations
// of a country by it's Country Code.
// Check alpha-2 country codes here: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
func (c Client) GetDataByCountryCode(ctx context.Context, countryCode string, timelines bool) (data Locations, err error) {
if countryCode == "" {
return Locations{}, errors.New("country code required")
}
t := "0"
if timelines {
t = "1"
}
endpoint := "/locations?country_code=" + countryCode + "&timelines=" + t
if err = c.makeGetRequest(ctx, endpoint, &data); err != nil {
return Locations{}, err
}
return data, nil
}
// LocationData holds data of a particular location
type LocationData struct {
Location Location `json:"location"`
}
// GetDataByLocationID returns data of a specific location by it's ID.
// You can Exclude/Include timelines. Timelines are excluded by default.
// You can Exclude/Include timelines. Timelines are excluded by default.
func (c Client) GetDataByLocationID(ctx context.Context, id int, timelines bool) (data LocationData, err error) {
t := "0"
if timelines {
t = "1"
}
endpoint := "/locations/" + strconv.Itoa(id) + "?timelines=" + t
if err = c.makeGetRequest(ctx, endpoint, &data); err != nil {
return LocationData{}, err
}
return data, nil
}