diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-07-13 23:32:21 -0400 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-07-13 23:32:21 -0400 |
commit | 6cac63ec95ce2d6b8a8fd3694f0784ef311690f5 (patch) | |
tree | 50facd2afd5878a883381706415f8f800babf309 /nws/nws.go | |
parent | 47d36edca0271bc1f3c76c2379788b813345abba (diff) |
nws: add `GetForecastGridData`
Diffstat (limited to 'nws/nws.go')
-rw-r--r-- | nws/nws.go | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -30,6 +30,7 @@ type PointProperties struct { GridY int Forecast string ForecastHourly string + ForecastGridData string RelativeLocation PointLocation } @@ -77,6 +78,24 @@ type FeatureCollection struct { Features []Feature } +type ForecastGrid struct { + Properties GridProperties +} + +type GridProperties struct { + RelativeHumidity GridHumidity +} + +type GridHumidity struct { + Uom string + Values []GridHumidityValue +} + +type GridHumidityValue struct { + ValidTime string + Value int +} + type Error struct { Title string Type string @@ -95,6 +114,7 @@ type ForecastBundle struct { var pCache *cache.Cache var fCache *cache.Cache var fhCache *cache.Cache +var fgCache *cache.Cache var aCache *cache.Cache var baseUrl *url.URL @@ -104,6 +124,7 @@ func init() { pCache = cache.NewCache() fCache = cache.NewCache() fhCache = cache.NewCache() + fgCache = cache.NewCache() aCache = cache.NewCache() // Parse NWS base url. @@ -326,6 +347,53 @@ func GetForecastHourly(point *Point) (*Forecast, *Error) { return forecast, nil } +// NWS forecast grid data endpoint. +func GetForecastGridData(point *Point) (*ForecastGrid, *Error) { + var nwsErr *Error + var expires time.Time + var body []byte + + if point == nil { + return nil, &Error{ + Title: "point is nil", + Type: "griddata-points-invalid", + Status: 500, + Detail: "point is nil", + } + } + if len(point.Properties.ForecastGridData) == 0 { + return nil, &Error{ + Title: "forecast grid data link is empty", + Type: "forecast-griddata-link-invalid", + Status: 500, + Detail: "forecast grid data link is empty", + } + } + + if body = fgCache.Get(point.Properties.ForecastGridData); len(body) == 0 { + // Get the forecast grid data + body, expires, nwsErr = get(point.Properties.ForecastGridData) + if nwsErr != nil { + return nil, nwsErr + } + // Cache it. + fgCache.Set(point.Properties.ForecastGridData, body, expires) + } + + // Unmarshal. + grid := new(ForecastGrid) + err := json.Unmarshal(body, grid) + if err != nil { + return nil, &Error{ + Title: "forecast grid data json unmarshal failed", + Type: "forecast-griddata-json-error", + Status: 500, + Detail: "forecast grid data json unmarshal failed", + } + } + return grid, nil +} + // NWS active alerts endpoint. func GetAlerts(lat, lng float32) (fc *FeatureCollection, err *Error) { // Alerts endpoint. |