|
1 | | -from __future__ import annotations |
| 1 | +""" |
| 2 | +This module provides functionality for managing individual collections in the Typesense API. |
| 3 | +
|
| 4 | +It contains the Collection class, which allows for retrieving, updating, and deleting |
| 5 | +collections, as well as managing documents, overrides, and synonyms within a collection. |
| 6 | +
|
| 7 | +Classes: |
| 8 | + Collection: Manages operations on a single collection in the Typesense API. |
| 9 | +
|
| 10 | +Dependencies: |
| 11 | + - typesense.api_call: Provides the ApiCall class for making API requests. |
| 12 | + - typesense.documents: Provides the Documents class for managing documents. |
| 13 | + - typesense.overrides: Provides the Overrides class for managing overrides. |
| 14 | + - typesense.synonyms: Provides the Synonyms class for managing synonyms. |
| 15 | + - typesense.types.collection: Provides CollectionSchema and CollectionUpdateSchema types. |
| 16 | + - typesense.types.document: Provides DocumentSchema type. |
| 17 | +
|
| 18 | +Note: This module uses conditional imports to support both Python 3.11+ and earlier versions. |
| 19 | +""" |
2 | 20 |
|
3 | 21 | import sys |
4 | 22 |
|
|
10 | 28 | import typing_extensions as typing |
11 | 29 |
|
12 | 30 | from typesense.api_call import ApiCall |
| 31 | +from typesense.documents import Documents |
| 32 | +from typesense.overrides import Overrides |
| 33 | +from typesense.synonyms import Synonyms |
13 | 34 | from typesense.types.document import DocumentSchema |
14 | 35 |
|
15 | | -from .documents import Documents |
16 | | -from .overrides import Overrides |
17 | | -from .synonyms import Synonyms |
18 | | - |
19 | 36 | TDoc = typing.TypeVar("TDoc", bound=DocumentSchema) |
20 | 37 |
|
21 | 38 |
|
22 | 39 | class Collection(typing.Generic[TDoc]): |
| 40 | + """ |
| 41 | + Manages operations on a single collection in the Typesense API. |
| 42 | +
|
| 43 | + This class provides methods to retrieve, update, and delete a collection, |
| 44 | + as well as access to documents, overrides, and synonyms within the collection. |
| 45 | + It is generic over the document type TDoc, which should be a subtype of DocumentSchema. |
| 46 | +
|
| 47 | + Attributes: |
| 48 | + name (str): The name of the collection. |
| 49 | + api_call (ApiCall): The ApiCall instance for making API requests. |
| 50 | + documents (Documents[TDoc]): Instance for managing documents in this collection. |
| 51 | + overrides (Overrides): Instance for managing overrides in this collection. |
| 52 | + synonyms (Synonyms): Instance for managing synonyms in this collection. |
| 53 | + """ |
| 54 | + |
23 | 55 | def __init__(self, api_call: ApiCall, name: str): |
| 56 | + """ |
| 57 | + Initialize the Collection instance. |
| 58 | +
|
| 59 | + Args: |
| 60 | + api_call (ApiCall): The ApiCall instance for making API requests. |
| 61 | + name (str): The name of the collection. |
| 62 | + """ |
24 | 63 | self.name = name |
25 | 64 | self.api_call = api_call |
26 | | - self.documents = Documents[TDoc](api_call, name) |
| 65 | + self.documents: Documents[TDoc] = Documents(api_call, name) |
27 | 66 | self.overrides = Overrides(api_call, name) |
28 | 67 | self.synonyms = Synonyms(api_call, name) |
29 | 68 |
|
30 | | - @property |
31 | | - def _endpoint_path(self) -> str: |
32 | | - from typesense.collections import Collections |
33 | | - |
34 | | - return f"{Collections.RESOURCE_PATH}/{self.name}" |
35 | | - |
36 | 69 | def retrieve(self) -> CollectionSchema: |
| 70 | + """ |
| 71 | + Retrieve the schema of this collection from Typesense. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + CollectionSchema: The schema of the collection. |
| 75 | + """ |
37 | 76 | response: CollectionSchema = self.api_call.get( |
38 | | - endpoint=self._endpoint_path, entity_type=CollectionSchema, as_json=True |
| 77 | + endpoint=self._endpoint_path, |
| 78 | + entity_type=CollectionSchema, |
| 79 | + as_json=True, |
39 | 80 | ) |
40 | 81 | return response |
41 | 82 |
|
42 | 83 | def update(self, schema_change: CollectionUpdateSchema) -> CollectionUpdateSchema: |
| 84 | + """ |
| 85 | + Update the schema of this collection in Typesense. |
| 86 | +
|
| 87 | + Args: |
| 88 | + schema_change (CollectionUpdateSchema): |
| 89 | + The changes to apply to the collection schema. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + CollectionUpdateSchema: The updated schema of the collection. |
| 93 | + """ |
43 | 94 | response: CollectionUpdateSchema = self.api_call.patch( |
44 | 95 | endpoint=self._endpoint_path, |
45 | 96 | body=schema_change, |
46 | 97 | entity_type=CollectionUpdateSchema, |
47 | 98 | ) |
48 | 99 | return response |
49 | 100 |
|
50 | | - # There's currently no parameters passed to Collection deletions, but ensuring future compatibility |
51 | 101 | def delete( |
52 | 102 | self, |
53 | | - params: typing.Union[typing.Dict[str, typing.Union[str, bool]], None] = None, |
| 103 | + delete_parameters: typing.Union[ |
| 104 | + typing.Dict[str, typing.Union[str, bool]], |
| 105 | + None, |
| 106 | + ] = None, |
54 | 107 | ) -> CollectionSchema: |
55 | | - return self.api_call.delete( |
56 | | - self._endpoint_path, entity_type=CollectionSchema, params=params |
| 108 | + """ |
| 109 | + Delete this collection from Typesense. |
| 110 | +
|
| 111 | + Args: |
| 112 | + delete_parameters (Union[Dict[str, Union[str, bool]], None], optional): |
| 113 | + Additional parameters for the delete operation. Defaults to None. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + CollectionSchema: The schema of the deleted collection. |
| 117 | + """ |
| 118 | + response: CollectionSchema = self.api_call.delete( |
| 119 | + self._endpoint_path, |
| 120 | + entity_type=CollectionSchema, |
| 121 | + params=delete_parameters, |
57 | 122 | ) |
| 123 | + return response |
| 124 | + |
| 125 | + @property |
| 126 | + def _endpoint_path(self) -> str: |
| 127 | + """ |
| 128 | + Get the API endpoint path for this collection. |
| 129 | +
|
| 130 | + Returns: |
| 131 | + str: The full endpoint path for the collection. |
| 132 | + """ |
| 133 | + from typesense.collections import Collections |
| 134 | + |
| 135 | + return "/".join([Collections.resource_path, self.name]) |
0 commit comments