-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.go
More file actions
70 lines (58 loc) · 1.43 KB
/
data.go
File metadata and controls
70 lines (58 loc) · 1.43 KB
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
package covid19brazilimporter
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"time"
)
// Entry read from the data file
type Entry struct {
Region string
Cases int
Date time.Time
Deaths int
}
type dataReader interface {
read(fileURL string) (Regions, error)
supports(fileURL string) bool
}
var readers = []dataReader{XLSXReader{}}
const portalGeralURL = "https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral"
var headers = map[string]string{
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"X-Parse-Application-Id": "unAFkcaNDeXajurGB7LChj8SgQYS2ptm",
}
type file struct {
Name string `json:"name"`
URL string `json:"url"`
}
type result struct {
File file `json:"arquivo"`
UpdatedAt time.Time `json:"updatedAt"`
}
type results struct {
Results []result `json:"results"`
}
func GetMetaData() result {
req, _ := http.NewRequest("GET", portalGeralURL, nil)
for k, v := range headers {
req.Header.Add(k, v)
}
resp, _ := http.DefaultClient.Do(req)
data, _ := ioutil.ReadAll(resp.Body)
var results results
json.Unmarshal(data, &results)
return results.Results[0]
}
// ReadData checks the file extensions ans uses the correct
// method to read the data.
func ReadData(fileURL string) (Regions, error) {
for _, reader := range readers {
if reader.supports(fileURL) {
return reader.read(fileURL)
}
}
return nil, errors.New("unsupported data file")
}