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

2
go.mod
View File

@@ -5,6 +5,7 @@ go 1.25.5
require (
github.com/nats-io/nats.go v1.47.0
github.com/stretchr/testify v1.11.1
resty.dev/v3 v3.0.0-beta.4
)
require (
@@ -14,6 +15,7 @@ require (
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.45.0 // indirect
golang.org/x/net v0.47.0 // indirect
golang.org/x/sys v0.38.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

4
go.sum
View File

@@ -14,9 +14,13 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
resty.dev/v3 v3.0.0-beta.4 h1:2O77oFymtA4NT8AY87wAaSgSGUBk2yvvM1qno9VRXZU=
resty.dev/v3 v3.0.0-beta.4/go.mod h1:NTOerrC/4T7/FE6tXIZGIysXXBdgNqwMZuKtxpea9NM=

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