implement openweather

This commit is contained in:
2025-12-04 18:37:46 +09:00
parent c59834e92b
commit b4e423c1bf
6 changed files with 1615 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package openweather
import (
"context"
"errors"
"fmt"
"log"
"resty.dev/v3"
)
var (
ErrForecast = errors.New("error in get forecast")
)
func Forecast(ctx context.Context, key string, lat float64, lon float64) (*ForecastResponse, error) {
client := resty.New()
defer func() {
err := client.Close()
if err != nil {
log.Println("error in close client", err)
}
}()
resp, err := client.R().
SetContext(ctx).
SetQueryParam("lat", fmt.Sprintf("%f", lat)).
SetQueryParam("lon", fmt.Sprintf("%f", lon)).
SetQueryParam("appid", key).
SetQueryParam("units", "metric").
SetResult(&ForecastResponse{}). //nolint:exhaustruct
Get("https://api.openweathermap.org/data/2.5/forecast")
if err != nil {
return nil, fmt.Errorf("error in get forecast: %w", err)
}
if resp.IsError() {
return nil, fmt.Errorf("%w: %s, %s", ErrForecast, resp.Status(), resp.String())
}
return resp.Result().(*ForecastResponse), nil //nolint:forcetypeassert
}

View File

@@ -0,0 +1,24 @@
package openweather_test
import (
_ "embed"
"encoding/json"
"testing"
"github.com/neatflowcv/seven-skies/internal/pkg/openweather"
"github.com/stretchr/testify/require"
)
//go:embed testdata/forecast.json
var forecast []byte
func TestForecast(t *testing.T) {
t.Parallel()
var resp openweather.ForecastResponse
err := json.Unmarshal(forecast, &resp)
require.NoError(t, err)
require.NotNil(t, forecast)
}

View File

@@ -0,0 +1,70 @@
package openweather
type ForecastResponse struct {
Cod string `json:"cod,omitempty"`
Message int `json:"message,omitempty"`
Cnt int `json:"cnt,omitempty"`
List []List `json:"list,omitempty"`
City City `json:"city,omitzero"`
}
type Main struct {
Temp float64 `json:"temp,omitempty"`
FeelsLike float64 `json:"feels_like,omitempty"`
TempMin float64 `json:"temp_min,omitempty"`
TempMax float64 `json:"temp_max,omitempty"`
Pressure int `json:"pressure,omitempty"`
SeaLevel int `json:"sea_level,omitempty"`
GrndLevel int `json:"grnd_level,omitempty"`
Humidity int `json:"humidity,omitempty"`
TempKf float64 `json:"temp_kf,omitempty"`
}
type Weather struct {
ID int `json:"id,omitempty"`
Main string `json:"main,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
}
type Clouds struct {
All int `json:"all,omitempty"`
}
type Wind struct {
Speed float64 `json:"speed,omitempty"`
Deg int `json:"deg,omitempty"`
Gust float64 `json:"gust,omitempty"`
}
type Sys struct {
Pod string `json:"pod,omitempty"`
}
type List struct {
Dt int `json:"dt,omitempty"`
Main Main `json:"main,omitzero"`
Weather []Weather `json:"weather,omitempty"`
Clouds Clouds `json:"clouds,omitzero"`
Wind Wind `json:"wind,omitzero"`
Visibility int `json:"visibility,omitempty"`
Pop float64 `json:"pop,omitempty"`
Sys Sys `json:"sys,omitzero"`
DtTxt string `json:"dt_txt,omitempty"`
}
type Coord struct {
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
}
type City struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Coord Coord `json:"coord,omitzero"`
Country string `json:"country,omitempty"`
Population int `json:"population,omitempty"`
Timezone int `json:"timezone,omitempty"`
Sunrise int `json:"sunrise,omitempty"`
Sunset int `json:"sunset,omitempty"`
}

File diff suppressed because it is too large Load Diff