summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-06-16 00:42:00 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-06-16 00:42:00 -0400
commit7e50bc27cab438421c79f7a6e5ad29d5132ec1cc (patch)
tree251f08fa8c3b4001be3a169ec3b84279b0840b5a
parent916168555462bda8c0659717f1e0897a29e4c8b4 (diff)
nws: add `GetAlerts`
-rw-r--r--nws/nws.go69
-rw-r--r--nws/nws_test.go8
2 files changed, 77 insertions, 0 deletions
diff --git a/nws/nws.go b/nws/nws.go
index c6940d0..a1fd4ef 100644
--- a/nws/nws.go
+++ b/nws/nws.go
@@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"io"
+ "net/url"
"time"
"ricketyspace.net/peach/cache"
@@ -59,6 +60,21 @@ 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
@@ -76,11 +92,20 @@ type ForecastBundle struct {
var pCache *cache.Cache
var fCache *cache.Cache
var fhCache *cache.Cache
+var baseUrl *url.URL
func init() {
+ var err error
+
pCache = cache.NewCache()
fCache = cache.NewCache()
fhCache = 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 {
@@ -231,6 +256,50 @@ func GetForecastHourly(point *Point) (*Forecast, error) {
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
+ }
+
+ // Build query.
+ q := url.Values{}
+ q.Add("status", "actual")
+ q.Add("message_type", "alert")
+ q.Add("point", fmt.Sprintf("%.4f,%.4f", lat, lng))
+ q.Add("urgency", "Immediate,Expected")
+ q.Add("certainty", "Observed,Likely,Possible")
+ u.RawQuery = q.Encode()
+
+ // Hit it.
+ body, _, err := get(u.String())
+ if err != nil {
+ return
+ }
+
+ // 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
diff --git a/nws/nws_test.go b/nws/nws_test.go
index a92ed7e..21127e0 100644
--- a/nws/nws_test.go
+++ b/nws/nws_test.go
@@ -242,3 +242,11 @@ func TestNWSGetWrapper(t *testing.T) {
return
}
}
+
+func TestAlerts(t *testing.T) {
+ _, err := GetAlerts(33.2938, -83.9674)
+ if err != nil {
+ t.Errorf("alerts: %v", err)
+ return
+ }
+}