Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/geo_basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
)

func main() {
client, err := geo.NewClient(nil)
if err != nil {
log.Fatalf("new client: %v", err)
}
client := geo.NewClient(nil)

resp, err := client.Direct("Stockholm,SE", nil)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions examples/onecall_advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
Transport: newRateLimitedTransport(rate.Every(time.Second), 1, nil),
}

client, err := onecall.NewClient(&onecall.ClientOptions{
client := onecall.NewClient(&onecall.ClientOptions{
HttpClient: httpClient,

// Either pass AppID like below,
Expand All @@ -37,9 +37,7 @@ func main() {
// which is not very common for everyday applications.
Units: onecall.Units.METRIC,
})
if err != nil {
panic(err)
}

resp, err := client.OneCall(59.3327, 18.0656, &onecall.OneCallOptions{
// If we only want CURRENT and DAILY for our location we can exclude the other forecasts.
Exclude: []onecall.Exclude{onecall.Excludes.HOURLY, onecall.Excludes.MINUTELY, onecall.Excludes.ALERTS},
Expand Down
5 changes: 1 addition & 4 deletions examples/onecall_basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import (
)

func main() {
client, err := onecall.NewClient(&onecall.ClientOptions{
client := onecall.NewClient(&onecall.ClientOptions{
// By default, OpenWeatherMap API returns Kelvin for temperature,
// which is not very common for everyday applications.
Units: onecall.Units.METRIC,
})
if err != nil {
panic(err)
}

resp, err := client.OneCall(59.3327, 18.0656, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions geo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ClientOptions struct {
Logger *slog.Logger
}

func NewClient(opts *ClientOptions) (*Client, error) {
func NewClient(opts *ClientOptions) *Client {

// Defaults if opts are not provided
if opts == nil {
Expand All @@ -60,7 +60,7 @@ func NewClient(opts *ClientOptions) (*Client, error) {
appID: opts.AppID,
httpClient: opts.HttpClient,
logger: opts.Logger,
}, nil
}
}

type GeoOptions struct {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.24.3

require (
github.com/joho/godotenv v1.5.1
github.com/martinlindhe/unit v0.0.0-20230420213220-4adfd7d0a0d6
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792
golang.org/x/time v0.12.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/martinlindhe/unit v0.0.0-20230420213220-4adfd7d0a0d6 h1:muzoir7BEy+lDPqdROr57IjJBP7OydzCg0VDhZtdG+w=
github.com/martinlindhe/unit v0.0.0-20230420213220-4adfd7d0a0d6/go.mod h1:8QbxAolnDKw/JhUJMU80MRjHjEs0tLwkjZAPrTn+xLA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792 h1:R9PFI6EUdfVKgwKjZef7QIwGcBKu86OEFpJ9nUEP2l4=
Expand All @@ -12,5 +17,6 @@ golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 18 additions & 15 deletions onecall/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ClientOptions struct {
Units Unit // Units to use for the client. Overruled by unit option explicitly passed to client calls.
}

func NewClient(opts *ClientOptions) (*Client, error) {
func NewClient(opts *ClientOptions) *Client {
if opts == nil {
opts = &ClientOptions{}
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewClient(opts *ClientOptions) (*Client, error) {
if opts.Units.IsValid() {
client.unit = opts.Units
}
return client, nil
return client
}

type OneCallOptions struct {
Expand All @@ -75,17 +75,17 @@ type OneCallOptions struct {
Lang Lang
}

func (c *Client) OneCallRaw(lat, lon float64, opts *OneCallOptions) (*OneCallResponseRaw, error) {
func (c *Client) OneCallRaw(lat, lon float64, opts *OneCallOptions) (*OneCallResponseRaw, *Unit, error) {
if lat < -90 || lat > 90 {
return nil, fmt.Errorf("lat argument must be in range (-90; 90), is %v", lat)
return nil, nil, fmt.Errorf("lat argument must be in range (-90; 90), is %v", lat)
}
if lon < -180 || lon > 180 {
return nil, fmt.Errorf("lon argument must be in range (-180; 180), is %v", lon)
return nil, nil, fmt.Errorf("lon argument must be in range (-180; 180), is %v", lon)
}

u, err := url.Parse(c.baseURL)
if err != nil {
return nil, fmt.Errorf("parse url: %w", err)
return nil, nil, fmt.Errorf("parse url: %w", err)
}

q := u.Query()
Expand All @@ -97,9 +97,12 @@ func (c *Client) OneCallRaw(lat, lon float64, opts *OneCallOptions) (*OneCallRes
q.Set(excludeParam, ExcludeList(opts.Exclude).String())
}

var units *Unit
if opts != nil && opts.Units.IsValid() {
units = &opts.Units
q.Set(unitsParam, opts.Units.String())
} else if c.unit.IsValid() {
units = &c.unit
q.Set(unitsParam, c.unit.String())
}

Expand All @@ -111,44 +114,44 @@ func (c *Client) OneCallRaw(lat, lon float64, opts *OneCallOptions) (*OneCallRes

resp, err := c.httpClient.Get(u.String())
if err != nil {
return nil, fmt.Errorf("get: %w", err)
return nil, nil, fmt.Errorf("get: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status: %s", resp.Status)
return nil, nil, fmt.Errorf("unexpected status: %s", resp.Status)
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
return nil, nil, fmt.Errorf("failed to read response body: %w", err)
}

// Save response body to a file
f, err := os.Create("response.json")
if err != nil {
return nil, fmt.Errorf("failed to create file: %w", err)
return nil, nil, fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()

_, err = f.Write(bodyBytes)
if err != nil {
return nil, fmt.Errorf("failed to write to file: %w", err)
return nil, nil, fmt.Errorf("failed to write to file: %w", err)
}

var oneCallResp OneCallResponseRaw
if err := json.Unmarshal(bodyBytes, &oneCallResp); err != nil {
return nil, fmt.Errorf("failed to decode one call response JSON: %w", err)
return nil, nil, fmt.Errorf("failed to decode one call response JSON: %w", err)
}

return &oneCallResp, nil
return &oneCallResp, units, nil
}

func (c *Client) OneCall(lat, lon float64, opts *OneCallOptions) (*OneCallResponse, error) {
raw, err := c.OneCallRaw(lat, lon, opts)
raw, units, err := c.OneCallRaw(lat, lon, opts)
if err != nil {
return nil, err
}

return raw.Parse(), nil
return raw.Parse(units), nil
}
5 changes: 2 additions & 3 deletions onecall/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
)

func TestNewClient(t *testing.T) {
client, err := NewClient(&ClientOptions{
client := NewClient(&ClientOptions{
AppID: "TEST",
Units: Units.METRIC,
})
require.NoError(t, err)
_, err = client.OneCall(0, 0, nil)
_, err := client.OneCall(0, 0, nil)
require.Error(t, err) // 401 Unauthorized
}
6 changes: 3 additions & 3 deletions onecall/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (w weathersRaw) convert() []WeatherCondition {
return out
}

func (r OneCallResponseRaw) Parse() *OneCallResponse {
func (r OneCallResponseRaw) Parse(units *Unit) *OneCallResponse {
return &OneCallResponse{
oneCallResponseCommon: r.oneCallResponseCommon,
Current: *r.Current.Parse(),
Minutely: minuteResponsesRaw(r.Minutely).Parse(),
Daily: dailyResponsesRaw(r.Daily).Parse(),
Daily: dailyResponsesRaw(r.Daily).Parse(units),
}
}

Expand All @@ -54,6 +54,6 @@ func (p OneCallResponse) convert() *OneCallResponseRaw {
oneCallResponseCommon: p.oneCallResponseCommon,
Current: p.Current.Parse(),
Minutely: minuteResponses(p.Minutely).convert(),
Daily: dailyResponses(p.Daily).parse(),
Daily: dailyResponses(p.Daily).convert(),
}
}
Loading