diff --git a/assemblyai/__version__.py b/assemblyai/__version__.py index e0d0ed6..98bb08f 100644 --- a/assemblyai/__version__.py +++ b/assemblyai/__version__.py @@ -1 +1 @@ -__version__ = "0.35.0" +__version__ = "0.35.1" diff --git a/assemblyai/types.py b/assemblyai/types.py index 1c6f3de..412d248 100644 --- a/assemblyai/types.py +++ b/assemblyai/types.py @@ -9,12 +9,15 @@ try: # pydantic v2 import - from pydantic import UUID4, BaseModel, Field + from pydantic import UUID4, BaseModel, ConfigDict, Field from pydantic_settings import BaseSettings, SettingsConfigDict + + pydantic_v2 = True except ImportError: # pydantic v1 import - from pydantic.v1 import UUID4, BaseModel, BaseSettings, Field - from pydantic.v1 import ConfigDict as SettingsConfigDict + from pydantic.v1 import UUID4, BaseModel, BaseSettings, ConfigDict, Field + + pydantic_v2 = False from typing_extensions import Self @@ -80,9 +83,15 @@ class Settings(BaseSettings): base_url: str = "https://api.assemblyai.com" "The base URL for the AssemblyAI API" - polling_interval: float = Field(default=3.0, gte=0.1) + polling_interval: float = Field(default=3.0, gt=0.0) "The default polling interval for long-running requests (e.g. polling the `Transcript`'s status)" - model_config = SettingsConfigDict(env_prefix="assemblyai_") + + if pydantic_v2: + model_config = SettingsConfigDict(env_prefix="assemblyai_") + else: + + class Config: + env_prefix = "assemblyai_" class TranscriptStatus(str, Enum): @@ -571,7 +580,7 @@ class RawTranscriptionConfig(BaseModel): """ The speech model to use for the transcription. """ - model_config = SettingsConfigDict(extra="allow") + model_config = ConfigDict(extra="allow") class TranscriptionConfig: @@ -1796,7 +1805,7 @@ class ListTranscriptParameters(BaseModel): throttled_only: Optional[bool] = None "Get only throttled transcripts, overrides the status filter" - model_config = SettingsConfigDict(use_enum_values=True) + model_config = ConfigDict(use_enum_values=True) class PageDetails(BaseModel): diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py new file mode 100644 index 0000000..25b6df4 --- /dev/null +++ b/tests/unit/test_settings.py @@ -0,0 +1,75 @@ +import os +from importlib import reload + +import assemblyai as aai + + +def test_api_key_settings(): + """ + Tests that `ASSEMBLYAI_API_KEY` works correctly + """ + tmp1 = os.environ.pop("ASSEMBLYAI_API_KEY", None) + tmp2 = os.environ.pop("API_KEY", None) + + aai.settings.api_key = None + reload(aai) + assert aai.settings.api_key is None + + # this should not change the api key + os.environ["API_KEY"] = "test" + reload(aai) + assert aai.settings.api_key is None + + # this should change the api key + os.environ["ASSEMBLYAI_API_KEY"] = "test" + reload(aai) + assert aai.settings.api_key == "test" + + # reset + if tmp1: + os.environ["ASSEMBLYAI_API_KEY"] = tmp1 + else: + os.environ.pop("ASSEMBLYAI_API_KEY", None) + + if tmp2: + os.environ["API_KEY"] = tmp2 + else: + os.environ.pop("API_KEY", None) + + reload(aai) + aai.settings.api_key = "test" + + +def test_base_url_settings(): + """ + Tests that `ASSEMBLY_BASE_URL` works correctly + """ + tmp1 = os.environ.pop("ASSEMBLYAI_BASE_URL", None) + tmp2 = os.environ.pop("BASE_URL", None) + + aai.settings.base_url = "https://api.assemblyai.com" + reload(aai) + assert aai.settings.base_url == "https://api.assemblyai.com" + + # this should not change the base url + os.environ["BASE_URL"] = "https://test.com" + reload(aai) + assert aai.settings.base_url == "https://api.assemblyai.com" + + # this should change the base url + os.environ["ASSEMBLYAI_BASE_URL"] = "https://test.com" + reload(aai) + assert aai.settings.base_url == "https://test.com" + + # reset + if tmp1: + os.environ["ASSEMBLYAI_BASE_URL"] = tmp1 + else: + os.environ.pop("ASSEMBLYAI_BASE_URL", None) + if tmp2: + os.environ["BASE_URL"] = tmp2 + else: + os.environ.pop("BASE_URL", None) + + reload(aai) + aai.settings.api_key = "test"