Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature (OWM) - Add ability to set the location parameter by an Environmental Variable #5478

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
21 changes: 15 additions & 6 deletions src/segments/owm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,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
PoshOWMAPIKey string = "POSH_OWM_API_KEY"
// Environmental variable to dynamically set the location string
PoshOWMLocationKey string = "POSH_OWM_LOCATION"
)

type weather struct {
Expand Down Expand Up @@ -88,14 +90,21 @@ func (d *Owm) getResult() (*owmDataResponse, error) {
apikey = d.env.Getenv(PoshOWMAPIKey)
}

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

location = url.QueryEscape(location)
location := d.props.GetString(Location, "De Bilt,NL")
if len(location) == 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never be true, right? We default to a value when fetching the location from the properties which implies location will always have a value.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamesAndrewJackson13 any update on this remark?

location = d.env.Getenv(PoshOWMLocationKey)
}

if len(apikey) == 0 || len(location) == 0 {
return nil, errors.New("no api key or location found")
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
14 changes: 7 additions & 7 deletions website/docs/segments/web/owm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ import Config from "@site/src/components/Config.js";

## 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 |
| `cache_timeout` | `int` | `10` | in minutes, the timeout for request caching. A value of 0 disables the cache. |
| 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 if the property is set to an empty string (`""`) |
| `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 if the property is set to an empty string (`""`) |
| `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 |
| `cache_timeout` | `int` | `10` | in minutes, the timeout for request caching. A value of 0 disables the cache. |

## Template ([info][templates])

Expand Down
Loading