-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide methods to delete documents and collections (#94)
* Provide methods to delete documents and collections Chunks and extracts are managed by the system, so they don't need explicit deletion endpoints - they're deleted whenever the document that produced them is deleted. * Comments & refactor * Use cascade delete rather than doing it explicitly * Comments and formatting
- Loading branch information
Showing
10 changed files
with
596 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, Optional, Union | ||
|
||
import httpx | ||
|
||
from ... import errors | ||
from ...client import AuthenticatedClient, Client | ||
from ...models.collection import Collection | ||
from ...models.http_validation_error import HTTPValidationError | ||
from ...types import Response | ||
|
||
|
||
def _get_kwargs( | ||
name: str, | ||
) -> Dict[str, Any]: | ||
_kwargs: Dict[str, Any] = { | ||
"method": "delete", | ||
"url": f"/api/collections/{name}", | ||
} | ||
|
||
return _kwargs | ||
|
||
|
||
def _parse_response( | ||
*, client: Union[AuthenticatedClient, Client], response: httpx.Response | ||
) -> Optional[Union[Collection, HTTPValidationError]]: | ||
if response.status_code == HTTPStatus.OK: | ||
response_200 = Collection.from_dict(response.json()) | ||
|
||
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: | ||
return None | ||
|
||
|
||
def _build_response( | ||
*, client: Union[AuthenticatedClient, Client], response: httpx.Response | ||
) -> Response[Union[Collection, HTTPValidationError]]: | ||
return Response( | ||
status_code=HTTPStatus(response.status_code), | ||
content=response.content, | ||
headers=response.headers, | ||
parsed=_parse_response(client=client, response=response), | ||
) | ||
|
||
|
||
def sync_detailed( | ||
name: str, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Response[Union[Collection, HTTPValidationError]]: | ||
"""Delete Collection | ||
Delete a collection and all documents contained within it. | ||
Args: | ||
name (str): The collection 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[Union[Collection, HTTPValidationError]] | ||
""" | ||
|
||
kwargs = _get_kwargs( | ||
name=name, | ||
) | ||
|
||
response = client.get_httpx_client().request( | ||
**kwargs, | ||
) | ||
|
||
return _build_response(client=client, response=response) | ||
|
||
|
||
def sync( | ||
name: str, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Optional[Union[Collection, HTTPValidationError]]: | ||
"""Delete Collection | ||
Delete a collection and all documents contained within it. | ||
Args: | ||
name (str): The collection 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: | ||
Union[Collection, HTTPValidationError] | ||
""" | ||
|
||
return sync_detailed( | ||
name=name, | ||
client=client, | ||
).parsed | ||
|
||
|
||
async def asyncio_detailed( | ||
name: str, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Response[Union[Collection, HTTPValidationError]]: | ||
"""Delete Collection | ||
Delete a collection and all documents contained within it. | ||
Args: | ||
name (str): The collection 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[Union[Collection, HTTPValidationError]] | ||
""" | ||
|
||
kwargs = _get_kwargs( | ||
name=name, | ||
) | ||
|
||
response = await client.get_async_httpx_client().request(**kwargs) | ||
|
||
return _build_response(client=client, response=response) | ||
|
||
|
||
async def asyncio( | ||
name: str, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Optional[Union[Collection, HTTPValidationError]]: | ||
"""Delete Collection | ||
Delete a collection and all documents contained within it. | ||
Args: | ||
name (str): The collection 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: | ||
Union[Collection, HTTPValidationError] | ||
""" | ||
|
||
return ( | ||
await asyncio_detailed( | ||
name=name, | ||
client=client, | ||
) | ||
).parsed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
from http import HTTPStatus | ||
from typing import Any, Dict, Optional, Union | ||
|
||
import httpx | ||
|
||
from ... import errors | ||
from ...client import AuthenticatedClient, Client | ||
from ...models.document import Document | ||
from ...models.http_validation_error import HTTPValidationError | ||
from ...types import Response | ||
|
||
|
||
def _get_kwargs( | ||
id: int, | ||
) -> Dict[str, Any]: | ||
_kwargs: Dict[str, Any] = { | ||
"method": "delete", | ||
"url": f"/api/documents/{id}", | ||
} | ||
|
||
return _kwargs | ||
|
||
|
||
def _parse_response( | ||
*, client: Union[AuthenticatedClient, Client], response: httpx.Response | ||
) -> Optional[Union[Document, HTTPValidationError]]: | ||
if response.status_code == HTTPStatus.OK: | ||
response_200 = Document.from_dict(response.json()) | ||
|
||
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: | ||
return None | ||
|
||
|
||
def _build_response( | ||
*, client: Union[AuthenticatedClient, Client], response: httpx.Response | ||
) -> Response[Union[Document, HTTPValidationError]]: | ||
return Response( | ||
status_code=HTTPStatus(response.status_code), | ||
content=response.content, | ||
headers=response.headers, | ||
parsed=_parse_response(client=client, response=response), | ||
) | ||
|
||
|
||
def sync_detailed( | ||
id: int, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Response[Union[Document, HTTPValidationError]]: | ||
"""Delete Document | ||
Delete a document. | ||
Args: | ||
id (int): The document ID. | ||
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[Union[Document, HTTPValidationError]] | ||
""" | ||
|
||
kwargs = _get_kwargs( | ||
id=id, | ||
) | ||
|
||
response = client.get_httpx_client().request( | ||
**kwargs, | ||
) | ||
|
||
return _build_response(client=client, response=response) | ||
|
||
|
||
def sync( | ||
id: int, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Optional[Union[Document, HTTPValidationError]]: | ||
"""Delete Document | ||
Delete a document. | ||
Args: | ||
id (int): The document ID. | ||
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: | ||
Union[Document, HTTPValidationError] | ||
""" | ||
|
||
return sync_detailed( | ||
id=id, | ||
client=client, | ||
).parsed | ||
|
||
|
||
async def asyncio_detailed( | ||
id: int, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Response[Union[Document, HTTPValidationError]]: | ||
"""Delete Document | ||
Delete a document. | ||
Args: | ||
id (int): The document ID. | ||
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[Union[Document, HTTPValidationError]] | ||
""" | ||
|
||
kwargs = _get_kwargs( | ||
id=id, | ||
) | ||
|
||
response = await client.get_async_httpx_client().request(**kwargs) | ||
|
||
return _build_response(client=client, response=response) | ||
|
||
|
||
async def asyncio( | ||
id: int, | ||
*, | ||
client: Union[AuthenticatedClient, Client], | ||
) -> Optional[Union[Document, HTTPValidationError]]: | ||
"""Delete Document | ||
Delete a document. | ||
Args: | ||
id (int): The document ID. | ||
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: | ||
Union[Document, HTTPValidationError] | ||
""" | ||
|
||
return ( | ||
await asyncio_detailed( | ||
id=id, | ||
client=client, | ||
) | ||
).parsed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.