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: Allow listing collection by name #62

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
70 changes: 56 additions & 14 deletions dewy-client/dewy_client/api/default/list_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,37 @@
from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.collection import Collection
from ...types import Response
from ...models.http_validation_error import HTTPValidationError
from ...types import UNSET, Response, Unset


def _get_kwargs() -> Dict[str, Any]:
def _get_kwargs(
*,
name: Union[None, Unset, str] = UNSET,
) -> Dict[str, Any]:
params: Dict[str, Any] = {}

json_name: Union[None, Unset, str]
if isinstance(name, Unset):
json_name = UNSET
else:
json_name = name
params["name"] = json_name

params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

_kwargs: Dict[str, Any] = {
"method": "get",
"url": "/api/collections/",
"params": params,
}

return _kwargs


def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[List["Collection"]]:
) -> Optional[Union[HTTPValidationError, List["Collection"]]]:
if response.status_code == HTTPStatus.OK:
response_200 = []
_response_200 = response.json()
Expand All @@ -30,6 +46,10 @@ def _parse_response(
response_200.append(response_200_item)

return response_200
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
response_422 = HTTPValidationError.from_dict(response.json())

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
Expand All @@ -38,7 +58,7 @@ def _parse_response(

def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[List["Collection"]]:
) -> Response[Union[HTTPValidationError, List["Collection"]]]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
Expand All @@ -50,20 +70,26 @@ def _build_response(
def sync_detailed(
*,
client: Union[AuthenticatedClient, Client],
) -> Response[List["Collection"]]:
name: Union[None, Unset, str] = UNSET,
) -> Response[Union[HTTPValidationError, List["Collection"]]]:
"""List Collections

List collections.

Args:
name (Union[None, Unset, str]): Find collections by name.

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[List['Collection']]
Response[Union[HTTPValidationError, List['Collection']]]
"""

kwargs = _get_kwargs()
kwargs = _get_kwargs(
name=name,
)

response = client.get_httpx_client().request(
**kwargs,
Expand All @@ -75,41 +101,52 @@ def sync_detailed(
def sync(
*,
client: Union[AuthenticatedClient, Client],
) -> Optional[List["Collection"]]:
name: Union[None, Unset, str] = UNSET,
) -> Optional[Union[HTTPValidationError, List["Collection"]]]:
"""List Collections

List collections.

Args:
name (Union[None, Unset, str]): Find collections by name.

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
List['Collection']
Union[HTTPValidationError, List['Collection']]
"""

return sync_detailed(
client=client,
name=name,
).parsed


async def asyncio_detailed(
*,
client: Union[AuthenticatedClient, Client],
) -> Response[List["Collection"]]:
name: Union[None, Unset, str] = UNSET,
) -> Response[Union[HTTPValidationError, List["Collection"]]]:
"""List Collections

List collections.

Args:
name (Union[None, Unset, str]): Find collections by name.

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[List['Collection']]
Response[Union[HTTPValidationError, List['Collection']]]
"""

kwargs = _get_kwargs()
kwargs = _get_kwargs(
name=name,
)

response = await client.get_async_httpx_client().request(**kwargs)

Expand All @@ -119,21 +156,26 @@ async def asyncio_detailed(
async def asyncio(
*,
client: Union[AuthenticatedClient, Client],
) -> Optional[List["Collection"]]:
name: Union[None, Unset, str] = UNSET,
) -> Optional[Union[HTTPValidationError, List["Collection"]]]:
"""List Collections

List collections.

Args:
name (Union[None, Unset, str]): Find collections by name.

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
List['Collection']
Union[HTTPValidationError, List['Collection']]
"""

return (
await asyncio_detailed(
client=client,
name=name,
)
).parsed
186 changes: 186 additions & 0 deletions dewy-client/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dewy-client/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dewy-client"
version = "0.1.1"
version = "0.1.2"
bjchambers marked this conversation as resolved.
Show resolved Hide resolved
description = "A client library for accessing Dewy Knowledge Base API"
authors = []
readme = "README.md"
Expand Down
13 changes: 10 additions & 3 deletions dewy/collection/router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Annotated, List

from fastapi import APIRouter, Path
from fastapi import APIRouter, Path, Query
from loguru import logger

from dewy.collection.models import Collection, CollectionCreate
Expand Down Expand Up @@ -51,9 +51,16 @@ async def add_collection(


@router.get("/")
async def list_collections(conn: PgConnectionDep) -> List[Collection]:
async def list_collections(conn: PgConnectionDep,
name: Annotated[str | None, Query(description="Find collections by name.")] = None) -> List[Collection]:
"""List collections."""
results = await conn.fetch("SELECT id, name, text_embedding_model FROM collection")
results = await conn.fetch(
"""
SELECT id, name, text_embedding_model
FROM collection
WHERE name = coalesce($1, name)
""",
name)
return [Collection.model_validate(dict(result)) for result in results]


Expand Down
Loading
Loading