Skip to main content

Add Consul as source of env variable to settings

Project description

Pydantic Consul settings

PyPI version

Add Consul as source of env variable to settings

This package provides a way to use pydantic settings with consul as source of environment variables.

Installation

pip install pydantic-consul-settings

Usage

from pydantic_consul_settings import BaseSettingsWithConsul, ConsulClientSettings, SettingsConfigDict


class ConsulSettings(ConsulClientSettings):
    """Add additional settings for key generation"""

    stage: str = 'dev'
    service: str = 'my-service'


    @property
    def key_prefix(self) -> str:
        """Get the key prefix."""
        return f"{self.stage}/{self.service}"


class BaseSettings(BaseSettingsWithConsul):
  model_config = SettingsConfigDict(
    env_prefix='APP_',
    consul_model=ConsulSettings(),
  )


class Settings(BaseSettings):
    """App settings"""

    model_config = SettingsConfigDict(
      env_prefix='APP_',
    )

    # Get from env APP_NAME
    # and `dev/my-service/APP_NAME` from consul
    name: str = 'my-app'
    # Get from env APP_VERSION
    # and `dev/my-service/APP_VERSION` from consul
    version: str = '0.1.0'
    # Get from env APP_DESCRIPTION
    # and `dev/my-service/APP_DESCRIPTION` from consul
    description: str = 'My app description'


settings = Settings()

print(settings.dict())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_consul_settings-1.1.1.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

pydantic_consul_settings-1.1.1-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page