Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ragstack-ai to user-agent if present #228

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions astrapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 10 additions & 5 deletions tests/astrapy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand Down Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions tests/astrapy/test_async_db_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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)")
Expand Down
6 changes: 3 additions & 3 deletions tests/astrapy/test_db_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Loading