-
Notifications
You must be signed in to change notification settings - Fork 10
/
metadata.go
190 lines (170 loc) · 5.23 KB
/
metadata.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package soda
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type metadata struct {
baseurl, identifier string
}
func (m metadata) url() (string, error) {
if m.baseurl == "" || len(m.identifier) != 9 || m.identifier[4] != '-' {
return "", fmt.Errorf("Cannot get metadata, is the resource URL used correct?")
}
return fmt.Sprintf("%s/views/%s", m.baseurl, m.identifier), nil
}
func (m metadata) do() (*Metadata, error) {
url, err := m.url()
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Received statuscode %d\nBody: %s", resp.StatusCode, b)
}
md := new(Metadata)
err = json.NewDecoder(resp.Body).Decode(md)
if err != nil {
return nil, err
}
return md, nil
}
//Get gets the metadata struct for this dataset
func (m metadata) Get() (*Metadata, error) {
return m.do()
}
//GetColumns gets only the column info from the metadata for this dataset
func (m metadata) GetColumns() ([]Column, error) {
md, err := m.do()
if err != nil {
return []Column{}, err
}
return md.Columns, nil
}
//newMetadata splits a resource url like https://data.ct.gov/resource/hma6-9xbg
//into https://data.ct.gov and hma6-9xbg
func newMetadata(resourceurl string) metadata {
m := metadata{}
u, err := url.Parse(resourceurl)
if err != nil {
return m
}
m.baseurl = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
ps := strings.Split(strings.TrimSuffix(u.Path, "/"), "/")
m.identifier = ps[len(ps)-1]
return m
}
//Format describes column formats
type Format struct {
PrecisionStyle string `json:"precisionStyle"`
Align string `json:"align"`
NoCommas string `json:"noCommas"`
}
//Column describes one data column
type Column struct {
DataTypeName string `json:"dataTypeName"`
FieldName string `json:"fieldName"`
Format Format `json:"format"`
ID int `json:"id"`
Name string `json:"name"`
Position int `json:"position"`
RenderTypeName string `json:"renderTypeName"`
TableColumnID int `json:"tableColumnId"`
Width int `json:"width"`
}
//Metadata contains the resource metadata
type Metadata struct {
AverageRating int `json:"averageRating"`
Category string `json:"category"`
Columns []Column `json:"columns"`
CreatedAt Timestamp `json:"createdAt"`
DisplayType string `json:"displayType"`
DownloadCount int `json:"downloadCount"`
Flags []string `json:"flags"`
Grants []struct {
Flags []string `json:"flags"`
Inherited bool `json:"inherited"`
Type string `json:"type"`
} `json:"grants"`
ID string `json:"id"`
IndexUpdatedAt Timestamp `json:"indexUpdatedAt"`
License struct {
Name string `json:"name"`
} `json:"license"`
LicenseID string `json:"licenseId"`
Metadata struct {
AvailableDisplayTypes []string `json:"availableDisplayTypes"`
CustomFields struct {
Licentie struct {
Licentie string `json:"Licentie"`
} `json:"Licentie"`
} `json:"custom_fields"`
RdfSubject string `json:"rdfSubject"`
RenderTypeConfig struct {
Visible struct {
Table bool `json:"table"`
} `json:"visible"`
} `json:"renderTypeConfig"`
RowLabel string `json:"rowLabel"`
} `json:"metadata"`
Name string `json:"name"`
NewBackend bool `json:"newBackend"`
NumberOfComments int `json:"numberOfComments"`
Oid int `json:"oid"`
Owner struct {
DisplayName string `json:"displayName"`
ID string `json:"id"`
Rights []string `json:"rights"`
RoleName string `json:"roleName"`
ScreenName string `json:"screenName"`
} `json:"owner"`
PublicationAppendEnabled bool `json:"publicationAppendEnabled"`
PublicationDate Timestamp `json:"publicationDate"`
PublicationGroup int `json:"publicationGroup"`
PublicationStage string `json:"publicationStage"`
Query struct{} `json:"-"` //TODO
Ratings struct {
Rating int `json:"rating"`
} `json:"ratings"`
Rights []string `json:"rights"`
RowsUpdatedAt Timestamp `json:"rowsUpdatedAt"`
RowsUpdatedBy string `json:"rowsUpdatedBy"`
TableAuthor struct {
DisplayName string `json:"displayName"`
ID string `json:"id"`
Rights []string `json:"rights"`
RoleName string `json:"roleName"`
ScreenName string `json:"screenName"`
} `json:"tableAuthor"`
TableID int `json:"tableId"`
Tags []string `json:"tags"`
TotalTimesRated int `json:"totalTimesRated"`
ViewCount int `json:"viewCount"`
ViewLastModified Timestamp `json:"viewLastModified"`
ViewType string `json:"viewType"`
}
//Timestamp is a time.Time struct unmarshalled from a unix epoch time
type Timestamp time.Time
//UnmarshalJSON sets t from a timestamp
func (t *Timestamp) UnmarshalJSON(b []byte) error {
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
*t = Timestamp(time.Unix(n, 0))
return nil
}
//Time returns t as time.Time
func (t Timestamp) Time() time.Time {
return time.Time(t)
}