feat(owm): add API key environment variable

resolves #4488
This commit is contained in:
Jan De Dobbeleer 2023-11-29 11:10:09 +01:00 committed by Jan De Dobbeleer
parent 230f8f4973
commit 74da88f99c
2 changed files with 19 additions and 9 deletions

View file

@ -36,6 +36,8 @@ 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"
)
type weather struct {
@ -90,6 +92,14 @@ func (d *Owm) getResult() (*owmDataResponse, error) {
}
apikey := properties.OneOf[string](d.props, ".", APIKey, "apiKey")
if len(apikey) == 0 {
apikey = d.env.Getenv(PoshOWMAPIKey)
}
if len(apikey) == 0 {
return nil, errors.New("no api key found")
}
location := d.props.GetString(Location, "De Bilt,NL")
latitude := d.props.GetFloat64(Latitude, 91) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value
longitude := d.props.GetFloat64(Longitude, 181) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value

View file

@ -37,16 +37,16 @@ import Config from "@site/src/components/Config.js";
## Properties
| Name | Type | Description |
| --------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `api_key` | `string` | Your API key from [Open Weather Map][owm] |
| `latitude` | `float64` | The latitude of the requested location. |
| `longitude` | `float64` | The longitude of the requested location. |
| Name | Type | Description |
| --------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `api_key` | `string` | Your API key from [Open Weather Map][owm]. Can also be set using the `POSH_OWM_API_KEY` environment variable. |
| `latitude` | `float64` | The latitude of the requested location. |
| `longitude` | `float64` | The longitude of the requested location. |
| `location` | `string` | The requested location. 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 - defaults to `DE BILT,NL` |
| `units` | `string` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) - defaults to `standard` |
| `http_timeout` | `int` | The default timeout for http request is 20ms. |
| `cache_timeout` | `int` | The default timeout for request caching is 10m. A value of 0 disables the cache. |
| `template` | `string` | A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the properties below - defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})` |
| `units` | `string` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) - defaults to `standard` |
| `http_timeout` | `int` | The default timeout for http request is 20ms. |
| `cache_timeout` | `int` | The default timeout for request caching is 10m. A value of 0 disables the cache. |
| `template` | `string` | A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the properties below - defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})` |
### Specifying Location