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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net>
// SPDX-License-Identifier: ISC
// Functions for accessing the National Weather Service API.
package nws
import (
"encoding/json"
"fmt"
"io"
"net/url"
"time"
"ricketyspace.net/peach/cache"
"ricketyspace.net/peach/client"
)
type PointLocationProperties struct {
City string
State string
}
type PointLocation struct {
Properties PointLocationProperties
}
type PointProperties struct {
GridId string
GridX int
GridY int
Forecast string
ForecastHourly string
RelativeLocation PointLocation
}
type Point struct {
Properties PointProperties
}
type ForecastPeriod struct {
Number int
Name string
StartTime string
EndTime string
IsDayTime bool
Temperature int
TemperatureUnit string
TemperatureTrend string
WindSpeed string
WindDirection string
ShortForecast string
DetailedForecast string
}
type ForecastProperties struct {
Periods []ForecastPeriod
}
type Forecast struct {
Properties ForecastProperties
}
type FeatureProperties struct {
Event string
Severity string
Description string
Instruction string
}
type Feature struct {
Properties FeatureProperties
}
type FeatureCollection struct {
Features []Feature
}
type Error struct {
Title string
Type string
Status int
Detail string
}
// NWS Forecast bundle.
type ForecastBundle struct {
Point *Point
Forecast *Forecast
ForecastHourly *Forecast
}
var pCache *cache.Cache
var fCache *cache.Cache
var fhCache *cache.Cache
var aCache *cache.Cache
var baseUrl *url.URL
func init() {
var err error
pCache = cache.NewCache()
fCache = cache.NewCache()
fhCache = cache.NewCache()
aCache = cache.NewCache()
// Parse NWS base url.
baseUrl, err = url.Parse("https://api.weather.gov")
if err != nil {
panic(`url parse: nws base url: ` + err.Error())
}
}
func (e Error) Error() string {
return fmt.Sprintf("%d: %s: %s", e.Status, e.Type, e.Detail)
}
// Gets NWS's forecast and hourly forecast.
func GetForecastBundle(lat, lng float32) (*ForecastBundle, *Error) {
p, err := Points(lat, lng)
if err != nil {
return nil, &Error{
Title: "unable get points",
Type: "points-failed",
Status: 500,
Detail: err.Error(),
}
}
f, err := GetForecast(p)
if err != nil {
return nil, &Error{
Title: "unable get forecast",
Type: "forecast-failed",
Status: 500,
Detail: err.Error(),
}
}
fh, err := GetForecastHourly(p)
if err != nil {
return nil, &Error{
Title: "unable get hourly forecast",
Type: "forecast-hourly-failed",
Status: 500,
Detail: err.Error(),
}
}
return &ForecastBundle{
Point: p,
Forecast: f,
ForecastHourly: fh,
}, nil
}
// NWS `/points` endpoint.
//
// TODO: return Error instead of error
func Points(lat, lng float32) (*Point, error) {
var nwsErr *Error
var expires time.Time
var body []byte
ll := fmt.Sprintf("%.4f,%.4f", lat, lng)
if body = pCache.Get(ll); len(body) == 0 {
url := fmt.Sprintf("https://api.weather.gov/points/%s", ll)
body, expires, nwsErr = get(url)
if nwsErr != nil {
return nil, fmt.Errorf("points: %v", nwsErr)
}
// Cache it.
pCache.Set(ll, body, expires)
}
// Unmarshal.
point := new(Point)
err := json.Unmarshal(body, point)
if err != nil {
return nil, fmt.Errorf("points: decode: %v", err)
}
if point.Properties.Forecast == "" {
return nil, fmt.Errorf("points: forecast empty")
}
if point.Properties.ForecastHourly == "" {
return nil, fmt.Errorf("points: forecasthourly empty")
}
return point, nil
}
// NWS forecast endpoint.
//
// TODO: return Error instead of error.
func GetForecast(point *Point) (*Forecast, error) {
var nwsErr *Error
var expires time.Time
var body []byte
if point == nil {
return nil, fmt.Errorf("forecast: point nil")
}
if len(point.Properties.Forecast) == 0 {
return nil, fmt.Errorf("forecast: link empty")
}
if body = fCache.Get(point.Properties.Forecast); len(body) == 0 {
// Get the forecast
body, expires, nwsErr = get(point.Properties.Forecast)
if nwsErr != nil {
return nil, fmt.Errorf("forecast: %v", nwsErr)
}
// Cache it.
fCache.Set(point.Properties.Forecast, body, expires)
}
// Unmarshal.
forecast := new(Forecast)
err := json.Unmarshal(body, forecast)
if err != nil {
return nil, fmt.Errorf("forecast: decode: %v", err)
}
if len(forecast.Properties.Periods) == 0 {
return nil, fmt.Errorf("forecast: periods empty")
}
return forecast, nil
}
// NWS forecast hourly endpoint.
//
// TODO: return Error instead of error
func GetForecastHourly(point *Point) (*Forecast, error) {
var nwsErr *Error
var expires time.Time
body := []byte{}
if point == nil {
return nil, fmt.Errorf("forecast hourly: point nil")
}
if len(point.Properties.ForecastHourly) == 0 {
return nil, fmt.Errorf("forecast hourly: link empty")
}
if body = fhCache.Get(point.Properties.ForecastHourly); len(body) == 0 {
// Get the hourly forecast.
body, expires, nwsErr = get(point.Properties.ForecastHourly)
if nwsErr != nil {
return nil, fmt.Errorf("forecast hourly: %v", nwsErr)
}
// Cache it.
fhCache.Set(point.Properties.ForecastHourly, body, expires)
}
// Unmarshal.
forecast := new(Forecast)
err := json.Unmarshal(body, forecast)
if err != nil {
return nil, fmt.Errorf("forecast hourly: decode: %v", err)
}
if len(forecast.Properties.Periods) == 0 {
return nil, fmt.Errorf("forecast hourly: periods empty")
}
return forecast, nil
}
// NWS active alerts endpoint.
func GetAlerts(lat, lng float32) (fc *FeatureCollection, err *Error) {
// Alerts endpoint.
u, uErr := baseUrl.Parse("/alerts/active")
if uErr != nil {
err = &Error{
Title: "alerts url parsing failed",
Type: "url-parse-error",
Status: 500,
Detail: uErr.Error(),
}
return
}
// Point: lat,lng.
ll := fmt.Sprintf("%.4f,%.4f", lat, lng)
// Build query.
q := url.Values{}
q.Add("status", "actual")
q.Add("message_type", "alert")
q.Add("point", ll)
q.Add("urgency", "Immediate,Expected")
q.Add("certainty", "Observed,Likely,Possible")
u.RawQuery = q.Encode()
// Hit it.
var expires time.Time
var body []byte
if body = aCache.Get(ll); len(body) == 0 {
body, expires, err = get(u.String())
if err != nil {
return
}
// Cache it.
aCache.Set(ll, body, expires)
}
// Unmarshal.
fc = new(FeatureCollection)
jErr := json.Unmarshal(body, fc)
if jErr != nil {
err = &Error{
Title: "feature collection decode failed",
Type: "json-decode-error",
Status: 500,
Detail: jErr.Error(),
}
return
}
return
}
// HTTP GET a NWS endpoint.
func get(url string) ([]byte, time.Time, *Error) {
// Default response expiration time
expires := time.Now()
tries := 5
retryDelay := 100 * time.Millisecond
for {
resp, err := client.Get(url)
if err != nil {
return nil, expires, &Error{
Title: fmt.Sprintf("http get failed: %v", url),
Type: "http-get",
Status: 500,
Detail: err.Error(),
}
}
if tries > 0 && resp.StatusCode != 200 {
tries -= 1
// Wait before re-try.
time.Sleep(retryDelay)
retryDelay *= 2 // Exponential back-off delay.
continue // Re-try
}
// Parse response body.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, expires, &Error{
Title: fmt.Sprintf("parsing body: %v", url),
Type: "response-body",
Status: 500,
Detail: err.Error(),
}
}
// Check if the request failed.
if resp.StatusCode != 200 {
nwsErr := Error{}
err := json.Unmarshal(body, &nwsErr)
if err != nil {
return nil, expires, &Error{
Title: fmt.Sprintf("json decode: %v", url),
Type: "json-decode",
Status: 500,
Detail: err.Error(),
}
}
return nil, expires, &nwsErr
}
// Parse expiration time of the response.
expiresHeader := resp.Header.Get("expires")
if len(expiresHeader) < 1 {
return nil, expires, &Error{
Title: "expiration header empty",
Type: "expiration-header",
Status: 500,
Detail: "response expiration header is empty",
}
}
expires, err := time.Parse(time.RFC1123, expiresHeader)
if err != nil {
return nil, expires, &Error{
Title: "expiration header could not be parsed",
Type: "expiration-header-parse-failed",
Status: 500,
Detail: err.Error(),
}
}
// Response OK.
return body, expires, nil
}
}
|