-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
256 lines (208 loc) · 4.54 KB
/
request.go
File metadata and controls
256 lines (208 loc) · 4.54 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package darksky
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
const (
scheme = "https"
host = "api.darksky.net"
basePath = "forecast"
languageOptionKey = "lang"
excludeOptionKey = "exclude"
extendOptionKey = "extend"
extendOptionValue = "hourly"
unitOptionKey = "units"
)
var (
// ErrLanguageNotSupported occurs when providing an option with an unsupported language.
ErrLanguageNotSupported = errors.New("language provided is not supported")
// ErrUnitNotSupported occurs when providing an option with an unsupported unit.
ErrUnitNotSupported = errors.New("unit provided is not supported")
// ErrExcludeOptionNotUnique occurs when passing non unique exclude options.
ErrExcludeOptionNotUnique = errors.New("exclude options must be unique within the same group")
// Supported languages
supportedLanguages = []string{
"ar",
"az",
"be",
"bg",
"bs",
"ca",
"cs",
"da",
"de",
"el",
"en",
"es",
"et",
"fi",
"fr",
"he",
"hr",
"hu",
"id",
"is",
"it",
"ja",
"ka",
"ko",
"kw",
"lv",
"nb",
"nl",
"no",
"pl",
"pt",
"ro",
"ru",
"sk",
"sl",
"sr",
"sv",
"te",
"tr",
"uk",
"x-pig-latin",
"zh",
"zh-tw",
}
// Supported exclude sections
supportedExclude = []string{
"currently",
"minutely",
"hourly",
"daily",
"alerts",
"flags",
}
// Supported units
supportedUnits = []string{
"auto",
"ca",
"uk2",
"us",
"si",
}
)
// Option represents options passed to the query to override default values.
type Option func(*url.Values) error
type excludeOptionError struct {
value string
}
func (oe excludeOptionError) Error() string {
return fmt.Sprintf("Unsupported value for exclude option : %s", oe.value)
}
func newOptionError(value string) *excludeOptionError {
return &excludeOptionError{
value: value,
}
}
// LanguageOption to have the API response in the specified language.
func LanguageOption(lang string) Option {
return func(v *url.Values) error {
var supported bool
var lowerLang = strings.ToLower(lang)
for _, sl := range supportedLanguages {
if sl == lowerLang {
supported = true
}
}
if !supported {
return ErrLanguageNotSupported
}
v.Set(languageOptionKey, lowerLang)
return nil
}
}
// ExcludeOption for when you don't need all the payload, you can choose to exclude some parts.
func ExcludeOption(ex ...string) Option {
return func(v *url.Values) error {
lowerExcludes := toLower(ex)
for _, e := range lowerExcludes {
var supported bool
var count int
for _, supex := range supportedExclude {
if e == supex {
supported = true
}
}
if !supported {
return newOptionError(e)
}
for _, excl := range lowerExcludes {
if excl == e {
count++
}
}
if count > 1 {
return ErrExcludeOptionNotUnique
}
}
v.Set(excludeOptionKey, "["+strings.Join(lowerExcludes, ",")+"]")
return nil
}
}
// ExtendOption will gives you more data hourly.
func ExtendOption() Option {
return func(v *url.Values) error {
v.Set(extendOptionKey, extendOptionValue)
return nil
}
}
// UnitOption to decide what unit type you want the data to be formatted to.
func UnitOption(u string) Option {
return func(v *url.Values) error {
var supported bool
lowerUnit := strings.ToLower(u)
for _, su := range supportedUnits {
if su == lowerUnit {
supported = true
}
}
if !supported {
return ErrUnitNotSupported
}
v.Set(unitOptionKey, lowerUnit)
return nil
}
}
func toLower(strs []string) []string {
lowStrs := make([]string, len(strs))
for i := 0; i < len(lowStrs); i++ {
lowStrs[i] = strings.ToLower(strs[i])
}
return lowStrs
}
func newForecastRequest(secret string, lat, lng float64, opts []Option) (*http.Request, error) {
path := fmt.Sprintf("/%s/%s/%3.4f,%3.4f", basePath, secret, lat, lng)
return newRequest(path, opts)
}
func newTimeMachineRequest(secret string, lat, lng float64, t time.Time, opts []Option) (*http.Request, error) {
path := fmt.Sprintf("/%s/%s/%3.4f,%3.4f,%d", basePath, secret, lat, lng, int32(t.Unix()))
return newRequest(path, opts)
}
func newRequest(path string, opts []Option) (*http.Request, error) {
url := &url.URL{
Scheme: scheme,
Host: host,
Path: path,
}
q := url.Query()
for _, opt := range opts {
if err := opt(&q); err != nil {
return nil, err
}
}
url.RawQuery = q.Encode()
r, err := http.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
return nil, err
}
r.Header.Add("Accept-Encoding", "gzip")
r.Header.Add("Accept", "application/json")
return r, nil
}