Move OpenWeatherMap code to a separate struct

This commit is contained in:
2022-08-28 09:44:35 -05:00
parent af10ef4dc5
commit 7d071ebe0b
3 changed files with 138 additions and 100 deletions

42
lib/weather/structs.go Normal file
View File

@@ -0,0 +1,42 @@
package weather
type (
Temperature float32
Weather struct {
Main struct {
Temp Temperature `json:"temp"`
FeelsLike Temperature `json:"feels_like"`
TempMin Temperature `json:"temp_min"`
TempMax Temperature `json:"temp_max"`
Pressure float32 `json:"pressure"`
Humidity float32 `json:"humidity"`
} `json:"main"`
Coord struct {
Lon float32 `json:"lon"`
Lat float32 `json:"lat"`
} `json:"coord"`
Rain struct {
H1 float32 `json:"1h"`
H3 float32 `json:"3h"`
} `json:"rain"`
}
WeatherError struct {
Message string `json:"message"`
}
)
func (t *Temperature) Kelvin() float32 {
return float32(*t)
}
func (t *Temperature) Fahrenheit() float32 {
return ((float32(*t) - 273.15) * (9.0 / 5)) + 32
}
func (t *Temperature) Celcius() float32 {
return float32(*t) - 273.15
}

81
lib/weather/weather.go Normal file
View File

@@ -0,0 +1,81 @@
package weather
import (
"encoding/json"
"fmt"
"io"
"net/http"
"git.kill0.net/chill9/beepboop/lib"
log "github.com/sirupsen/logrus"
)
type (
WeatherClient struct {
token string
}
)
const (
OpenWeatherMapURI = "https://api.openweathermap.org"
)
var (
EndpointWeather = lib.BuildURI(OpenWeatherMapURI, "/data/2.5/weather")
)
func NewClient(token string) *WeatherClient {
return &WeatherClient{token}
}
func (c *WeatherClient) Get(loc string) (w Weather, err error) {
var (
werr WeatherError
)
req, err := http.NewRequest("GET", EndpointWeather, nil)
if err != nil {
err = fmt.Errorf("failed to create new request: %s", err)
return
}
q := req.URL.Query()
q.Add("q", loc)
q.Add("appid", c.token)
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("HTTP request failed: %s", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("reading HTTP response failed: %s", err)
return
}
if resp.StatusCode != 200 {
err = json.Unmarshal(body, &werr)
if err != nil {
log.Debugf("%s", body)
err = fmt.Errorf("unmarshaling JSON failed: %s", err)
return
}
err = fmt.Errorf("error: (%s) %s", resp.Status, werr.Message)
return
}
err = json.Unmarshal(body, &w)
if err != nil {
log.Debugf("%s", body)
log.Errorf("unmarshaling JSON failed: %s", err)
return
}
return
}