Skip to content

Commit

Permalink
feat(owm): add ability to set location by env var
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesAndrewJackson13 authored and JanDeDobbeleer committed Mar 3, 2025
1 parent 82f8a71 commit 58670c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
22 changes: 14 additions & 8 deletions src/segments/owm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ const (
CacheKeyResponse string = "owm_response"
// CacheKeyURL key used when caching the url responsible for the response
CacheKeyURL string = "owm_url"

PoshOWMAPIKey = "POSH_OWM_API_KEY"
// Environmental variable to dynamically set the Open Map API key
OWMAPIKey string = "POSH_OWM_API_KEY"
// Environmental variable to dynamically set the location string
OWMLocationKey string = "POSH_OWM_LOCATION"
)

type weather struct {
Expand Down Expand Up @@ -68,18 +70,22 @@ func (d *Owm) Template() string {
func (d *Owm) getResult() (*owmDataResponse, error) {
response := new(owmDataResponse)

apikey := properties.OneOf(d.props, ".", APIKey, "apiKey")
apikey := properties.OneOf(d.props, d.env.Getenv(OWMAPIKey), APIKey, "apiKey")
if len(apikey) == 0 {
apikey = d.env.Getenv(PoshOWMAPIKey)
apikey = "."
}

location := d.props.GetString(Location, "De Bilt,NL")
location = url.QueryEscape(location)
if len(apikey) == 0 {
return nil, errors.New("no api key found")
}

if len(apikey) == 0 || len(location) == 0 {
return nil, errors.New("no api key or location found")
location := d.props.GetString(Location, d.env.Getenv(OWMLocationKey))
if len(location) == 0 {
return nil, errors.New("no location found")
}

location = url.QueryEscape(location)

units := d.props.GetString(Units, "standard")
httpTimeout := d.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)

Expand Down
26 changes: 4 additions & 22 deletions src/segments/owm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func TestOWMSegmentSingle(t *testing.T) {
location := url.QueryEscape(tc.Location)
testURL := fmt.Sprintf(OWMWEATHERAPIURL, location)
env.On("HTTPRequest", testURL).Return([]byte(tc.WeatherJSONResponse), tc.Error)
env.On("Getenv", OWMLocationKey).Return("")
env.On("Getenv", OWMAPIKey).Return("")

o := &Owm{}
o.Init(props, env)
Expand Down Expand Up @@ -207,6 +209,8 @@ func TestOWMSegmentIcons(t *testing.T) {
expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString)

env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil)
env.On("Getenv", OWMLocationKey).Return("")
env.On("Getenv", OWMAPIKey).Return("")

props := properties.Map{
APIKey: "key",
Expand All @@ -220,26 +224,4 @@ func TestOWMSegmentIcons(t *testing.T) {
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, renderTemplate(env, o.Template(), o), tc.Case)
}

// test with hyperlink enabled
for _, tc := range cases {
env := &mock.Environment{}

weatherResponse := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20.3}}`, tc.IconID)
expectedString := fmt.Sprintf("«%s (20°C)»(%s)", tc.ExpectedIconString, testURL)

env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil)

props := properties.Map{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
}

o := &Owm{}
o.Init(props, env)

assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, renderTemplate(env, "«{{.Weather}} ({{.Temperature}}{{.UnitIcon}})»({{.URL}})", o), tc.Case)
}
}
14 changes: 7 additions & 7 deletions website/docs/segments/web/owm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ import Config from "@site/src/components/Config.js";
units: "metric",
http_timeout: 20,
},
}}u
}}
/>

## Properties

| Name | Type | Default | Description |
| -------------- | :------: | :----------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `api_key` | `string` | `.` | Your API key from [Open Weather Map][owm]. Can also be set using the `POSH_OWM_API_KEY` environment variable. |
| `location` | `string` | `De Bilt,NL` | The requested location interpreted only if valid coordinates aren't given. Formatted as \<City,STATE,COUNTRY_CODE\>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes |
| `units` | `string` | `standard` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) |
| `http_timeout` | `int` | `20` | in milliseconds, the timeout for http request |
| Name | Type | Default | Description |
| -------------- | :------: | :----------: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `api_key` | `string` | `.` | Your API key from [Open Weather Map][owm]. Can also be set using the `POSH_OWM_API_KEY` environment variable |
| `location` | `string` | `De Bilt,NL` | The requested location interpreted only if valid coordinates aren't given. Formatted as \<City,STATE,COUNTRY_CODE\>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes . Can also be set using the `POSH_OWM_LOCATION` environment variable |
| `units` | `string` | `standard` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) |
| `http_timeout` | `int` | `20` | in milliseconds, the timeout for http request |

## Template ([info][templates])

Expand Down

0 comments on commit 58670c5

Please sign in to comment.