# pydantic-settings-azure-app-configuration
[](https://pypi.python.org/pypi/pydantic-settings-azure-app-configuration)
[](https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration/blob/main/LICENSE)
[](https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration)
## Introduction
https://learn.microsoft.com/en-us/azure/azure-app-configuration/overview
## Installation
```bash
pip install pydantic-settings-azure-app-configuration
```
## Usage
By default, it loads all the values from Azure App Configuration, but you can assign a prefix to your application (e.g. `my_api__`) and load only the values for it using the `select_key` method. You can also use the `trim_key_prefix` method to remove the prefix from the value names.
Furthermore, if some value references a secret stored in Azure Key Vault, you can use the `configure_key_vault` method to retrieve it.
If you use Entra ID authentication, you can use the role `App Configuration Data Reader` to access the configurations and `Key Vault Secrets User` to access the secrets.
To nest models you have to define a `env_nested_delimiter` (e.g. `__`), either in the source constructor or in the `model_config` class.
The configuration of this settings source is almost idental to the the provided by ASP.NET Core, in case you want to read the official documentation to inform you about more complex uses, best practices, etc.
```python
import os
from azure.identity import DefaultAzureCredential
from pydantic import BaseModel
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource
from src.pydantic_settings_azure_app_configuration.source import (
AzureAppConfigurationSettingsSource,
)
import os
from azure.identity import DefaultAzureCredential
from pydantic import BaseModel
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource
from src.pydantic_settings_azure_app_configuration.source import (
AzureAppConfigurationSettingsSource,
)
class SqlServer(BaseModel):
password: str
host: str
class AzureKeyVaultSettings(BaseSettings):
logging_level: str
sql_server: SqlServer
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
azure_credential = DefaultAzureCredential()
azure_app_configuration = AzureAppConfigurationSettingsSource(
settings_cls,
lambda app_configuration_options: app_configuration_options.connect_with_url(
os.environ["AZURE_APP_CONFIGURATION_URL"], azure_credential
)
.select_key("my_api__*")
.trim_key_prefix("my_api__")
.configure_key_vault(
lambda key_vault_options: key_vault_options.set_credential(
azure_credential
)
),
env_nested_delimiter="__",
)
return (
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
azure_app_configuration,
)
```
Raw data
{
"_id": null,
"home_page": null,
"name": "pydantic-settings-azure-app-configuration",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/f9/b6/1f812d135a7b836ec3003a5fc4b3b7cba8351cfa69460a50867375e83a9f/pydantic_settings_azure_app_configuration-0.0.2.tar.gz",
"platform": null,
"description": "# pydantic-settings-azure-app-configuration\n\n[](https://pypi.python.org/pypi/pydantic-settings-azure-app-configuration)\n\n[](https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration/blob/main/LICENSE)\n\n[](https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration)\n\n## Introduction\n\nhttps://learn.microsoft.com/en-us/azure/azure-app-configuration/overview\n\n## Installation\n\n```bash\npip install pydantic-settings-azure-app-configuration\n```\n\n## Usage\n\nBy default, it loads all the values from Azure App Configuration, but you can assign a prefix to your application (e.g. `my_api__`) and load only the values for it using the `select_key` method. You can also use the `trim_key_prefix` method to remove the prefix from the value names.\n\nFurthermore, if some value references a secret stored in Azure Key Vault, you can use the `configure_key_vault` method to retrieve it.\n\nIf you use Entra ID authentication, you can use the role `App Configuration Data Reader` to access the configurations and `Key Vault Secrets User` to access the secrets.\n\nTo nest models you have to define a `env_nested_delimiter` (e.g. `__`), either in the source constructor or in the `model_config` class.\n\nThe configuration of this settings source is almost idental to the the provided by ASP.NET Core, in case you want to read the official documentation to inform you about more complex uses, best practices, etc.\n\n```python\nimport os\n\nfrom azure.identity import DefaultAzureCredential\nfrom pydantic import BaseModel\nfrom pydantic_settings import BaseSettings, PydanticBaseSettingsSource\nfrom src.pydantic_settings_azure_app_configuration.source import (\n AzureAppConfigurationSettingsSource,\n)\n\n\nimport os\n\nfrom azure.identity import DefaultAzureCredential\nfrom pydantic import BaseModel\nfrom pydantic_settings import BaseSettings, PydanticBaseSettingsSource\nfrom src.pydantic_settings_azure_app_configuration.source import (\n AzureAppConfigurationSettingsSource,\n)\n\n\nclass SqlServer(BaseModel):\n password: str\n host: str\n\n\nclass AzureKeyVaultSettings(BaseSettings):\n logging_level: str\n sql_server: SqlServer\n\n @classmethod\n def settings_customise_sources(\n cls,\n settings_cls: type[BaseSettings],\n init_settings: PydanticBaseSettingsSource,\n env_settings: PydanticBaseSettingsSource,\n dotenv_settings: PydanticBaseSettingsSource,\n file_secret_settings: PydanticBaseSettingsSource,\n ) -> tuple[PydanticBaseSettingsSource, ...]:\n azure_credential = DefaultAzureCredential()\n azure_app_configuration = AzureAppConfigurationSettingsSource(\n settings_cls,\n lambda app_configuration_options: app_configuration_options.connect_with_url(\n os.environ[\"AZURE_APP_CONFIGURATION_URL\"], azure_credential\n )\n .select_key(\"my_api__*\")\n .trim_key_prefix(\"my_api__\")\n .configure_key_vault(\n lambda key_vault_options: key_vault_options.set_credential(\n azure_credential\n )\n ),\n env_nested_delimiter=\"__\",\n )\n return (\n init_settings,\n env_settings,\n dotenv_settings,\n file_secret_settings,\n azure_app_configuration,\n )\n```",
"bugtrack_url": null,
"license": null,
"summary": null,
"version": "0.0.2",
"project_urls": {
"Documentation": "https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration",
"Repository": "https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0a0be1e0cd80f1c39a06b7e91abceeef64ecca77e72e2616ccae4d14aee03480",
"md5": "b7f1a0e73c812fbe52dd1b0de6a088b3",
"sha256": "4f53e72dd908f738d946cc26f4ebc1f68c307cc3b8bcbb0f43064e2c08f4c817"
},
"downloads": -1,
"filename": "pydantic_settings_azure_app_configuration-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b7f1a0e73c812fbe52dd1b0de6a088b3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 5104,
"upload_time": "2024-07-24T14:39:15",
"upload_time_iso_8601": "2024-07-24T14:39:15.505306Z",
"url": "https://files.pythonhosted.org/packages/0a/0b/e1e0cd80f1c39a06b7e91abceeef64ecca77e72e2616ccae4d14aee03480/pydantic_settings_azure_app_configuration-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f9b61f812d135a7b836ec3003a5fc4b3b7cba8351cfa69460a50867375e83a9f",
"md5": "8f68db856656c4e9d796f5bad4619101",
"sha256": "830938bd9d5ea131247ecb2d794250acd492e572ee49686a572fe86cef54729b"
},
"downloads": -1,
"filename": "pydantic_settings_azure_app_configuration-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "8f68db856656c4e9d796f5bad4619101",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 6212,
"upload_time": "2024-07-24T14:39:16",
"upload_time_iso_8601": "2024-07-24T14:39:16.506754Z",
"url": "https://files.pythonhosted.org/packages/f9/b6/1f812d135a7b836ec3003a5fc4b3b7cba8351cfa69460a50867375e83a9f/pydantic_settings_azure_app_configuration-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-24 14:39:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AndreuCodina",
"github_project": "pydantic-settings-azure-app-configuration",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pydantic-settings-azure-app-configuration"
}