diff --git a/astrapy/utils.py b/astrapy/utils.py index 23c2d062..80ca3e5a 100644 --- a/astrapy/utils.py +++ b/astrapy/utils.py @@ -38,6 +38,25 @@ class http_methods: package_name = __name__.split(".")[0] +user_agent_astrapy = f"{package_name}/{__version__}" + + +def detect_ragstack_user_agent() -> Optional[str]: + from importlib import metadata + from importlib.metadata import PackageNotFoundError + + try: + ragstack_meta = metadata.metadata("ragstack-ai") + if ragstack_meta: + ragstack_version = ragstack_meta["version"] + return f"ragstack-ai/{ragstack_version}" + except PackageNotFoundError: + pass + return None + + +user_agent_rs = detect_ragstack_user_agent() + def log_request( method: str, @@ -82,13 +101,22 @@ def log_response(r: httpx.Response) -> None: def compose_user_agent( caller_name: Optional[str], caller_version: Optional[str] ) -> str: + user_agent_caller: Optional[str] = None if caller_name: if caller_version: - return f"{caller_name}/{caller_version} {package_name}/{__version__}" + user_agent_caller = f"{caller_name}/{caller_version}" else: - return f"{caller_name} {package_name}/{__version__}" - else: - return f"{package_name}/{__version__}" + user_agent_caller = f"{caller_name}" + all_user_agents = [ + ua_block + for ua_block in [ + user_agent_rs, + user_agent_caller, + user_agent_astrapy, + ] + if ua_block + ] + return " ".join(all_user_agents) def make_request( diff --git a/tests/astrapy/conftest.py b/tests/astrapy/conftest.py index fc897992..2d68900b 100644 --- a/tests/astrapy/conftest.py +++ b/tests/astrapy/conftest.py @@ -24,6 +24,11 @@ T = TypeVar("T") +TEST_SKIP_COLLECTION_DELETE: bool +if os.getenv("TEST_SKIP_COLLECTION_DELETE"): + TEST_SKIP_COLLECTION_DELETE = int(os.environ["TEST_SKIP_COLLECTION_DELETE"]) != 0 +else: + TEST_SKIP_COLLECTION_DELETE = False # fixed TEST_WRITABLE_VECTOR_COLLECTION = "writable_v_col" @@ -139,7 +144,7 @@ def readonly_v_collection(db: AstraDB) -> Iterable[AstraDBCollection]: yield collection - if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0: + if not TEST_SKIP_COLLECTION_DELETE: db.delete_collection(TEST_READONLY_VECTOR_COLLECTION) @@ -156,7 +161,7 @@ def writable_v_collection(db: AstraDB) -> Iterable[AstraDBCollection]: yield collection - if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0: + if not TEST_SKIP_COLLECTION_DELETE: db.delete_collection(TEST_WRITABLE_VECTOR_COLLECTION) @@ -189,7 +194,7 @@ def writable_nonv_collection(db: AstraDB) -> Iterable[AstraDBCollection]: yield collection - if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0: + if not TEST_SKIP_COLLECTION_DELETE: db.delete_collection(TEST_WRITABLE_NONVECTOR_COLLECTION) @@ -214,7 +219,7 @@ def allowindex_nonv_collection(db: AstraDB) -> Iterable[AstraDBCollection]: yield collection - if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0: + if not TEST_SKIP_COLLECTION_DELETE: db.delete_collection(TEST_WRITABLE_ALLOWINDEX_NONVECTOR_COLLECTION) @@ -243,7 +248,7 @@ def denyindex_nonv_collection(db: AstraDB) -> Iterable[AstraDBCollection]: yield collection - if int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 0: + if not TEST_SKIP_COLLECTION_DELETE: db.delete_collection(TEST_WRITABLE_DENYINDEX_NONVECTOR_COLLECTION) diff --git a/tests/astrapy/test_async_db_ddl.py b/tests/astrapy/test_async_db_ddl.py index 6fd0f033..026cb518 100644 --- a/tests/astrapy/test_async_db_ddl.py +++ b/tests/astrapy/test_async_db_ddl.py @@ -16,12 +16,12 @@ Tests for the `db.py` parts related to DML & client creation """ -import os import logging import pytest from ..conftest import AstraDBCredentials +from .conftest import TEST_SKIP_COLLECTION_DELETE from astrapy.db import AsyncAstraDB, AsyncAstraDBCollection from astrapy.defaults import DEFAULT_KEYSPACE_NAME @@ -74,7 +74,7 @@ async def test_path_handling( @pytest.mark.skipif( - int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 1, + TEST_SKIP_COLLECTION_DELETE, reason="collection-deletion tests are suppressed", ) @pytest.mark.describe("should create, use and destroy a non-vector collection (async)") @@ -104,7 +104,7 @@ async def test_create_use_destroy_nonvector_collection(async_db: AsyncAstraDB) - @pytest.mark.skipif( - int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 1, + TEST_SKIP_COLLECTION_DELETE, reason="collection-deletion tests are suppressed", ) @pytest.mark.describe("should create and destroy a vector collection (async)") diff --git a/tests/astrapy/test_db_ddl.py b/tests/astrapy/test_db_ddl.py index 75b4a690..df25d37a 100644 --- a/tests/astrapy/test_db_ddl.py +++ b/tests/astrapy/test_db_ddl.py @@ -16,12 +16,12 @@ Tests for the `db.py` parts related to DML & client creation """ -import os import logging import pytest from ..conftest import AstraDBCredentials +from .conftest import TEST_SKIP_COLLECTION_DELETE from astrapy.db import AstraDB, AstraDBCollection from astrapy.defaults import DEFAULT_KEYSPACE_NAME @@ -70,7 +70,7 @@ def test_path_handling(astra_db_credentials_kwargs: AstraDBCredentials) -> None: @pytest.mark.skipif( - int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 1, + TEST_SKIP_COLLECTION_DELETE, reason="collection-deletion tests are suppressed", ) @pytest.mark.describe("should create, use and destroy a non-vector collection") @@ -98,7 +98,7 @@ def test_create_use_destroy_nonvector_collection(db: AstraDB) -> None: @pytest.mark.skipif( - int(os.getenv("TEST_SKIP_COLLECTION_DELETE", "0")) == 1, + TEST_SKIP_COLLECTION_DELETE, reason="collection-deletion tests are suppressed", ) @pytest.mark.describe("should create and destroy a vector collection")