-
Notifications
You must be signed in to change notification settings - Fork 1
/
govid.go
156 lines (117 loc) · 3.05 KB
/
govid.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package govid
import (
"encoding/json"
"github.com/valyala/fasthttp"
)
// The different endpoints of the API
const (
AllDataEndpoint = "https://coronavirus-19-api.herokuapp.com/all"
AllCountriesEndpoint = "https://coronavirus-19-api.herokuapp.com/countries"
CountryDataEndpoint = "https://coronavirus-19-api.herokuapp.com/countries/"
)
// Gets data about the global cases, recoveries and deaths
// Returns:
// (*GlobalData, error)
func GetGlobalData() (*GlobalData, error) {
// Create the request
req := fasthttp.AcquireRequest()
req.SetRequestURI(AllDataEndpoint)
resp := fasthttp.AcquireResponse()
// Release the request and response
defer fasthttp.ReleaseResponse(resp)
defer fasthttp.ReleaseRequest(req)
// Do the request
err := fasthttp.Do(req, resp)
if err != nil {
return nil, err
}
var body []byte
body = resp.Body()
// Deserialize the data
var data GlobalData
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return &data, nil
}
// Gets data about a given country.
// Params:
// country string
// Returns:
// (*CountryData, error)
func GetCountryData(country string) (*CountryData, error) {
// Create the request
req := fasthttp.AcquireRequest()
req.SetRequestURI(CountryDataEndpoint + country)
resp := fasthttp.AcquireResponse()
// Release the request and response
defer fasthttp.ReleaseResponse(resp)
defer fasthttp.ReleaseRequest(req)
// Do the request
err := fasthttp.Do(req, resp)
if err != nil {
return nil, err
}
var body []byte
body = resp.Body()
// Deserialize the data
var data CountryData
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return &data, nil
}
// Gets a list which contains all the data about every available country
// Returns:
// ([]CountryData, error)
func GetAllCountriesData() ([]CountryData, error) {
// Create the request
req := fasthttp.AcquireRequest()
req.SetRequestURI(AllCountriesEndpoint)
resp := fasthttp.AcquireResponse()
// Release the request and response
defer fasthttp.ReleaseResponse(resp)
defer fasthttp.ReleaseRequest(req)
// Do the request
err := fasthttp.Do(req, resp)
if err != nil {
return nil, err
}
var body []byte
body = resp.Body()
// Deserialize the data
var data []CountryData
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return data, nil
}
// The same as GetAllCountriesData, but allows you to explicitly specify a limit on many elements
// to get.
// Params:
// limit int
// Returns:
// ([]CountryData, error)
func GetCountriesDataByLimit(limit int) ([]CountryData, error) {
arr, err := GetAllCountriesData()
if err != nil {
return nil, err
}
return arr[0:limit], nil
}
// Similar to GetCountriesDataByLimit, but lets you specify a start and end limit for more control.
// Params:
// endLimit int
// startLimit int
// Returns:
// ([]CountryData, error)
func GetCountriesDataByEndLimit(endLimit int, startLimit int) ([]CountryData, error) {
arr, err := GetAllCountriesData()
if err != nil {
return nil, err
}
return arr[startLimit:endLimit], nil
}