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
|
// 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"
"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 Error struct {
Title string
Type string
Status int
Detail string
}
func (e Error) Error() string {
return fmt.Sprintf("%d: %s: %s", e.Status, e.Type, e.Detail)
}
// NWS `/points` endpoint.
//
// TODO: return Error instead of error
func Points(lat, lng float32) (*Point, error) {
url := fmt.Sprintf("https://api.weather.gov/points/%.4f,%.4f", lat, lng)
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("points: http get: %v", err)
}
// Parse response body.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("points: body: %v", err)
}
// Check if the request failed.
if resp.StatusCode != 200 {
perr := new(Error)
err := json.Unmarshal(body, perr)
if err != nil {
return nil, fmt.Errorf("points: json: %v", err)
}
return nil, fmt.Errorf("points: %v", perr)
}
// 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) {
if point == nil {
return nil, fmt.Errorf("forecast: point nil")
}
if len(point.Properties.Forecast) == 0 {
return nil, fmt.Errorf("forecast: link empty")
}
// Get the forecast
resp, err := client.Get(point.Properties.Forecast)
if err != nil {
return nil, fmt.Errorf("forecast: get: %v", err)
}
// Parse response body.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("forecast: body: %v", err)
}
// Check if the request failed.
if resp.StatusCode != 200 {
perr := new(Error)
err := json.Unmarshal(body, perr)
if err != nil {
return nil, fmt.Errorf("forecast: json: %v", err)
}
return nil, fmt.Errorf("forecast: %v", perr)
}
// 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) {
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")
}
// Get the forecast
resp, err := client.Get(point.Properties.ForecastHourly)
if err != nil {
return nil, fmt.Errorf("forecast hourly: get: %v", err)
}
// Parse response body.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("forecast hourly: body: %v", err)
}
// Check if the request failed.
if resp.StatusCode != 200 {
perr := new(Error)
err := json.Unmarshal(body, perr)
if err != nil {
return nil, fmt.Errorf("forecast hourly: json: %v", err)
}
return nil, fmt.Errorf("forecast: %v", perr)
}
// 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
}
|