Implement from_env method for InfluxDBv3Session

Add class method to create InfluxDBv3Session from environment variables.
This commit is contained in:
Carmelo Mordini
2026-04-09 11:47:42 +02:00
committed by GitHub
parent 63ad631b83
commit 8e15d29fcb
@@ -120,3 +120,32 @@ class InfluxDBv3Session:
token=_config.token.get_secret_value(),
verify_ssl=_config.verify_ssl,
)
@classmethod
def from_env(cls, path: str | PathLike | None = None) -> "InfluxDBv3Session":
"""Create InfluxDBv3Session from environment variables.
The variables should be prefixed by 'INFLUXDBV3_' and should reflect the
attribute names of the Influxdbv3Config class.
Examples:
INFLUXDBV3_URL=<influx server url>
INFLUXDBV3_BUCKET=bucket
etc...
Args:
path (str | PathLike | None): Path to the configuration file.
If None, variables are read from the environment. Otherwise,
variables are read from the file but environment takes precedence.
See https://confz.readthedocs.io/en/latest/reference/sources.html#confz.EnvSource
Returns:
InfluxDBv3Session: An instance of InfluxDBv3Session.
"""
config = InfluxDBv3Config(config_sources=EnvSource(file=path, allow_all=True, prefix="INFLUXDBV3_"))
return cls(
host=config.url,
org=config.org,
bucket=config.bucket,
token=config.token.get_secret_value(),
verify_ssl=config.verify_ssl,
)