Public API¶
This section covers the main functions intended for direct use in your applications.
envresolve.api
¶
Public API for envresolve.
EnvResolver
¶
Manages provider registration and secret resolution.
This class encapsulates the provider registry and resolver instance, eliminating the need for module-level global variables.
Source code in src/envresolve/api.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
__init__
¶
register_azure_kv_provider
¶
Register Azure Key Vault provider for akv:// scheme.
This method is safe to call multiple times (idempotent).
Raises:
Type | Description |
---|---|
ProviderRegistrationError
|
If azure-identity or azure-keyvault-secrets is not installed |
Source code in src/envresolve/api.py
resolve_secret
¶
Resolve a secret URI to its value.
This function supports: - Variable expansion: ${VAR} and $VAR syntax using os.environ - Secret URI resolution: akv:// scheme - Idempotent resolution: Plain strings and non-target URIs pass through
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
str
|
Secret URI or plain string to resolve |
required |
Returns:
Type | Description |
---|---|
str
|
Resolved secret value or the original string if not a secret URI |
Raises:
Type | Description |
---|---|
URIParseError
|
If the URI format is invalid |
SecretResolutionError
|
If secret resolution fails |
VariableNotFoundError
|
If a referenced variable is not found |
CircularReferenceError
|
If a circular variable reference is detected |
Source code in src/envresolve/api.py
resolve_with_env
¶
Expand variables and resolve secret URIs with custom environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
Value to resolve (may contain variables or be a secret URI) |
required |
env
|
dict[str, str]
|
Environment dict for variable expansion |
required |
Returns:
Type | Description |
---|---|
str
|
Resolved value |
Source code in src/envresolve/api.py
load_env
¶
load_env(
path: str | Path = ".env",
*,
export: bool = True,
override: bool = False,
) -> dict[str, str]
Load environment variables from a .env file and resolve secret URIs.
This function: 1. Loads variables from the .env file 2. Expands variable references within values 3. Resolves secret URIs (akv://) to actual secret values 4. Optionally exports to os.environ
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
Path to .env file (default: ".env") |
'.env'
|
export
|
bool
|
If True, export resolved variables to os.environ |
True
|
override
|
bool
|
If True, override existing os.environ variables |
False
|
Returns:
Type | Description |
---|---|
dict[str, str]
|
Dictionary of resolved environment variables |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the .env file doesn't exist |
URIParseError
|
If a URI format is invalid |
SecretResolutionError
|
If secret resolution fails |
VariableNotFoundError
|
If a referenced variable is not found |
CircularReferenceError
|
If a circular variable reference is detected |
Source code in src/envresolve/api.py
register_azure_kv_provider
¶
Register Azure Key Vault provider for akv:// scheme.
This function should be called before attempting to resolve secrets from Azure Key Vault. It is safe to call multiple times (idempotent).
Example
import envresolve envresolve.register_azure_kv_provider()
Now you can resolve secrets (requires Azure authentication)¶
secret = envresolve.resolve_secret("akv://my-vault/db-password")¶
Source code in src/envresolve/api.py
resolve_secret
¶
Resolve a secret URI to its value.
This function supports: - Variable expansion: ${VAR} and $VAR syntax using os.environ - Secret URI resolution: akv:// scheme - Idempotent resolution: Plain strings and non-target URIs pass through unchanged
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
str
|
Secret URI or plain string to resolve |
required |
Returns:
Type | Description |
---|---|
str
|
Resolved secret value or the original string if not a secret URI |
Raises:
Type | Description |
---|---|
URIParseError
|
If the URI format is invalid |
SecretResolutionError
|
If secret resolution fails |
VariableNotFoundError
|
If a referenced variable is not found |
CircularReferenceError
|
If a circular variable reference is detected |
Examples:
>>> import envresolve
>>> # Idempotent - plain strings pass through
>>> value = envresolve.resolve_secret("just-a-string")
>>> value
'just-a-string'
>>> # Non-target URIs pass through unchanged
>>> uri = envresolve.resolve_secret("postgres://localhost/db")
>>> uri
'postgres://localhost/db'
>>> # Secret URIs require provider registration and authentication
>>> # envresolve.register_azure_kv_provider()
>>> # secret = envresolve.resolve_secret("akv://my-vault/db-password")
Source code in src/envresolve/api.py
load_env
¶
load_env(
path: str | Path = ".env",
*,
export: bool = True,
override: bool = False,
) -> dict[str, str]
Load environment variables from a .env file and resolve secret URIs.
This function: 1. Loads variables from the .env file 2. Expands variable references within values 3. Resolves secret URIs (akv://) to actual secret values 4. Optionally exports to os.environ
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
Path to .env file (default: ".env") |
'.env'
|
export
|
bool
|
If True, export resolved variables to os.environ (default: True) |
True
|
override
|
bool
|
If True, override existing os.environ variables (default: False) |
False
|
Returns:
Type | Description |
---|---|
dict[str, str]
|
Dictionary of resolved environment variables |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the .env file doesn't exist |
URIParseError
|
If a URI format is invalid |
SecretResolutionError
|
If secret resolution fails |
VariableNotFoundError
|
If a referenced variable is not found |
CircularReferenceError
|
If a circular variable reference is detected |
Examples:
>>> import envresolve
>>> envresolve.register_azure_kv_provider()
>>> # Load and export to os.environ
>>> resolved = envresolve.load_env(".env", export=True)
>>> # Load without exporting
>>> resolved = envresolve.load_env(".env", export=False)