summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.go6
-rw-r--r--nws/nws.go45
-rw-r--r--nws/nws_test.go8
3 files changed, 29 insertions, 30 deletions
diff --git a/main.go b/main.go
index 503938a..c19e133 100644
--- a/main.go
+++ b/main.go
@@ -119,12 +119,12 @@ func showWeather(w http.ResponseWriter, lat, lng float32) {
}
// get forecast
- f, err := nws.Forecast(point)
+ f, err := nws.GetForecast(point)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
- fh, err := nws.ForecastHourly(point)
+ fh, err := nws.GetForecastHourly(point)
if err != nil {
http.Error(w, err.Error(), 500)
return
@@ -164,7 +164,7 @@ func showSearch(w http.ResponseWriter, r *http.Request) {
}
}
-func NewWeather(point *nws.NWSPoint, f, fh *nws.NWSForecast) (*Weather, error) {
+func NewWeather(point *nws.Point, f, fh *nws.Forecast) (*Weather, error) {
w := new(Weather)
w.Location = fmt.Sprintf("%s, %s",
strings.ToLower(point.Properties.RelativeLocation.Properties.City),
diff --git a/nws/nws.go b/nws/nws.go
index 5b68169..8c28f19 100644
--- a/nws/nws.go
+++ b/nws/nws.go
@@ -2,7 +2,6 @@
// SPDX-License-Identifier: ISC
// Functions for accessing the National Weather Service API.
-// TODO: remove NWS prefix from all types.
package nws
import (
@@ -22,7 +21,7 @@ type PointLocation struct {
Properties PointLocationProperties
}
-type NWSPointProperties struct {
+type PointProperties struct {
GridId string
GridX int
GridY int
@@ -31,11 +30,11 @@ type NWSPointProperties struct {
RelativeLocation PointLocation
}
-type NWSPoint struct {
- Properties NWSPointProperties
+type Point struct {
+ Properties PointProperties
}
-type NWSForecastPeriod struct {
+type ForecastPeriod struct {
Number int
Name string
StartTime string
@@ -50,29 +49,29 @@ type NWSForecastPeriod struct {
DetailedForecast string
}
-type NWSForecastProperties struct {
- Periods []NWSForecastPeriod
+type ForecastProperties struct {
+ Periods []ForecastPeriod
}
-type NWSForecast struct {
- Properties NWSForecastProperties
+type Forecast struct {
+ Properties ForecastProperties
}
-type NWSError struct {
+type Error struct {
Title string
Type string
Status int
Detail string
}
-func (e NWSError) Error() string {
+func (e Error) Error() string {
return fmt.Sprintf("%d: %s: %s", e.Status, e.Type, e.Detail)
}
// NWS `/points` endpoint.
//
-// TODO: return NWSError instead of error
-func Points(lat, lng float32) (*NWSPoint, error) {
+// 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 {
@@ -87,7 +86,7 @@ func Points(lat, lng float32) (*NWSPoint, error) {
// Check if the request failed.
if resp.StatusCode != 200 {
- perr := new(NWSError)
+ perr := new(Error)
err := json.Unmarshal(body, perr)
if err != nil {
return nil, fmt.Errorf("points: json: %v", err)
@@ -96,7 +95,7 @@ func Points(lat, lng float32) (*NWSPoint, error) {
}
// Unmarshal.
- point := new(NWSPoint)
+ point := new(Point)
err = json.Unmarshal(body, point)
if err != nil {
return nil, fmt.Errorf("points: decode: %v", err)
@@ -112,8 +111,8 @@ func Points(lat, lng float32) (*NWSPoint, error) {
// NWS forecast endpoint.
//
-// TODO: return NWSError instead of error.
-func Forecast(point *NWSPoint) (*NWSForecast, error) {
+// TODO: return Error instead of error.
+func GetForecast(point *Point) (*Forecast, error) {
if point == nil {
return nil, fmt.Errorf("forecast: point nil")
}
@@ -135,7 +134,7 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) {
// Check if the request failed.
if resp.StatusCode != 200 {
- perr := new(NWSError)
+ perr := new(Error)
err := json.Unmarshal(body, perr)
if err != nil {
return nil, fmt.Errorf("forecast: json: %v", err)
@@ -144,7 +143,7 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) {
}
// Unmarshal.
- forecast := new(NWSForecast)
+ forecast := new(Forecast)
err = json.Unmarshal(body, forecast)
if err != nil {
return nil, fmt.Errorf("forecast: decode: %v", err)
@@ -157,8 +156,8 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) {
// NWS forecast hourly endpoint.
//
-// TODO: return NWSError instead of error
-func ForecastHourly(point *NWSPoint) (*NWSForecast, error) {
+// TODO: return Error instead of error
+func GetForecastHourly(point *Point) (*Forecast, error) {
if point == nil {
return nil, fmt.Errorf("forecast hourly: point nil")
}
@@ -180,7 +179,7 @@ func ForecastHourly(point *NWSPoint) (*NWSForecast, error) {
// Check if the request failed.
if resp.StatusCode != 200 {
- perr := new(NWSError)
+ perr := new(Error)
err := json.Unmarshal(body, perr)
if err != nil {
return nil, fmt.Errorf("forecast hourly: json: %v", err)
@@ -189,7 +188,7 @@ func ForecastHourly(point *NWSPoint) (*NWSForecast, error) {
}
// Unmarshal.
- forecast := new(NWSForecast)
+ forecast := new(Forecast)
err = json.Unmarshal(body, forecast)
if err != nil {
return nil, fmt.Errorf("forecast hourly: decode: %v", err)
diff --git a/nws/nws_test.go b/nws/nws_test.go
index 9dcd53e..ffa05c8 100644
--- a/nws/nws_test.go
+++ b/nws/nws_test.go
@@ -42,7 +42,7 @@ func TestPoints(t *testing.T) {
}
}
-func TestForecast(t *testing.T) {
+func TestGetForecast(t *testing.T) {
// Get point.
np, err := Points(41.115, -83.177)
if err != nil {
@@ -51,7 +51,7 @@ func TestForecast(t *testing.T) {
}
// Get forecast.
- fc, err := Forecast(np)
+ fc, err := GetForecast(np)
if err != nil {
t.Errorf("error: %v", err)
return
@@ -96,7 +96,7 @@ func TestForecast(t *testing.T) {
}
}
-func TestForecastHourly(t *testing.T) {
+func TestGetForecastHourly(t *testing.T) {
// Get point.
np, err := Points(41.115, -83.177)
if err != nil {
@@ -105,7 +105,7 @@ func TestForecastHourly(t *testing.T) {
}
// Get forecast hourly.
- fc, err := ForecastHourly(np)
+ fc, err := GetForecastHourly(np)
if err != nil {
t.Errorf("error: %v", err)
return