|
| 1 | +""" |
| 2 | +This module provides the main async client interface for interacting with the Typesense API. |
| 3 | +
|
| 4 | +It contains the AsyncClient class, which serves as the entry point for all Typesense operations, |
| 5 | +integrating various components like collections, multi-search, keys, aliases, analytics, etc. |
| 6 | +
|
| 7 | +Classes: |
| 8 | + Client: The main client class for interacting with Typesense. |
| 9 | +
|
| 10 | +Dependencies: |
| 11 | + - typesense.aliases: Provides the AsyncAliases class. |
| 12 | + - typesense.analytics: Provides the AsyncAnalytics class. |
| 13 | + - typesense.api_call: Provides the AsyncApiCall class for making API requests. |
| 14 | + - typesense.collection: Provides the AsyncCollection class. |
| 15 | + - typesense.collections: Provides the AsyncCollections class. |
| 16 | + - typesense.configuration: Provides AsyncConfiguration and ConfigDict types. |
| 17 | + - typesense.conversations_models: Provides the AsyncConversationsModels class. |
| 18 | + - typesense.debug: Provides the AsyncDebug class. |
| 19 | + - typesense.keys: Provides the AsyncKeys class. |
| 20 | + - typesense.metrics: Provides the AsyncMetrics class. |
| 21 | + - typesense.multi_search: Provides the AsyncMultiSearch class. |
| 22 | + - typesense.operations: Provides the AsyncOperations class. |
| 23 | + - typesense.stopwords: Provides the AsyncStopwords class. |
| 24 | + - typesense.types.document: Provides the AsyncDocumentSchema type. |
| 25 | +
|
| 26 | +Note: This module uses conditional imports to support both Python 3.11+ and earlier versions. |
| 27 | +""" |
| 28 | + |
| 29 | +import sys |
| 30 | + |
| 31 | +from typing_extensions import deprecated |
| 32 | + |
| 33 | +from typesense.types.document import DocumentSchema |
| 34 | + |
| 35 | +if sys.version_info >= (3, 11): |
| 36 | + import typing |
| 37 | +else: |
| 38 | + import typing_extensions as typing |
| 39 | + |
| 40 | +from typesense.async_aliases import AsyncAliases |
| 41 | +from typesense.async_analytics import AsyncAnalytics |
| 42 | +from typesense.async_analytics_v1 import AsyncAnalyticsV1 |
| 43 | +from typesense.async_api_call import AsyncApiCall |
| 44 | +from typesense.async_collection import AsyncCollection |
| 45 | +from typesense.async_collections import AsyncCollections |
| 46 | +from typesense.async_conversations_models import AsyncConversationsModels |
| 47 | +from typesense.async_curation_sets import AsyncCurationSets |
| 48 | +from typesense.async_debug import AsyncDebug |
| 49 | +from typesense.async_keys import AsyncKeys |
| 50 | +from typesense.async_metrics import AsyncMetrics |
| 51 | +from typesense.async_multi_search import AsyncMultiSearch |
| 52 | +from typesense.async_nl_search_models import AsyncNLSearchModels |
| 53 | +from typesense.async_operations import AsyncOperations |
| 54 | +from typesense.async_stemming import AsyncStemming |
| 55 | +from typesense.async_stopwords import AsyncStopwords |
| 56 | +from typesense.async_synonym_sets import AsyncSynonymSets |
| 57 | +from typesense.configuration import ConfigDict, Configuration |
| 58 | + |
| 59 | +TDoc = typing.TypeVar("TDoc", bound=DocumentSchema) |
| 60 | + |
| 61 | + |
| 62 | +class AsyncClient: |
| 63 | + """ |
| 64 | + The main client class for interacting with Typesense. |
| 65 | +
|
| 66 | + This class serves as the entry point for all Typesense operations. It initializes |
| 67 | + and provides access to various components of the Typesense SDK, such as collections, |
| 68 | + multi-search, keys, aliases, analytics, stemming, operations, debug, stopwords, |
| 69 | + and conversation models. |
| 70 | +
|
| 71 | + Attributes: |
| 72 | + config (Configuration): The configuration object for the Typesense client. |
| 73 | + api_call (ApiCall): The ApiCall instance for making API requests. |
| 74 | + collections (Collections[DocumentSchema]): Instance for managing collections. |
| 75 | + multi_search (MultiSearch): Instance for performing multi-search operations. |
| 76 | + keys (Keys): Instance for managing API keys. |
| 77 | + aliases (Aliases): Instance for managing collection aliases. |
| 78 | + analyticsV1 (AnalyticsV1): Instance for analytics operations (V1). |
| 79 | + analytics (Analytics): Instance for analytics operations (v30). |
| 80 | + curation_sets (CurationSets): Instance for Curation Sets (v30+) |
| 81 | + stemming (Stemming): Instance for stemming dictionary operations. |
| 82 | + operations (Operations): Instance for various Typesense operations. |
| 83 | + debug (Debug): Instance for debug operations. |
| 84 | + stopwords (Stopwords): Instance for managing stopwords. |
| 85 | + metrics (Metrics): Instance for retrieving system and Typesense metrics. |
| 86 | + conversations_models (ConversationsModels): Instance for managing conversation models. |
| 87 | + """ |
| 88 | + |
| 89 | + def __init__(self, config_dict: ConfigDict) -> None: |
| 90 | + """ |
| 91 | + Initialize the Client instance. |
| 92 | +
|
| 93 | + Args: |
| 94 | + config_dict (ConfigDict): |
| 95 | + A dictionary containing the configuration for the Typesense client. |
| 96 | +
|
| 97 | + Example: |
| 98 | + >>> config = { |
| 99 | + ... "api_key": "your_api_key", |
| 100 | + ... "nodes": [ |
| 101 | + ... {"host": "localhost", "port": "8108", "protocol": "http"} |
| 102 | + ... ], |
| 103 | + ... "connection_timeout_seconds": 2, |
| 104 | + ... } |
| 105 | + >>> client = Client(config) |
| 106 | + """ |
| 107 | + self.config = Configuration(config_dict) |
| 108 | + self.api_call = AsyncApiCall(self.config) |
| 109 | + self.collections: AsyncCollections[DocumentSchema] = AsyncCollections( |
| 110 | + self.api_call |
| 111 | + ) |
| 112 | + self.multi_search = AsyncMultiSearch(self.api_call) |
| 113 | + self.keys = AsyncKeys(self.api_call) |
| 114 | + self.aliases = AsyncAliases(self.api_call) |
| 115 | + self._analyticsV1 = AsyncAnalyticsV1(self.api_call) |
| 116 | + self.analytics = AsyncAnalytics(self.api_call) |
| 117 | + self.stemming = AsyncStemming(self.api_call) |
| 118 | + self.curation_sets = AsyncCurationSets(self.api_call) |
| 119 | + self.operations = AsyncOperations(self.api_call) |
| 120 | + self.debug = AsyncDebug(self.api_call) |
| 121 | + self.stopwords = AsyncStopwords(self.api_call) |
| 122 | + self.synonym_sets = AsyncSynonymSets(self.api_call) |
| 123 | + self.metrics = AsyncMetrics(self.api_call) |
| 124 | + self.conversations_models = AsyncConversationsModels(self.api_call) |
| 125 | + self.nl_search_models = AsyncNLSearchModels(self.api_call) |
| 126 | + |
| 127 | + @property |
| 128 | + @deprecated( |
| 129 | + "AnalyticsV1 is deprecated on v30+. Use client.analytics instead.", |
| 130 | + category=None, |
| 131 | + ) |
| 132 | + def analyticsV1(self) -> AsyncAnalyticsV1: |
| 133 | + return self._analyticsV1 |
| 134 | + |
| 135 | + def typed_collection( |
| 136 | + self, |
| 137 | + *, |
| 138 | + model: typing.Type[TDoc], |
| 139 | + name: typing.Union[str, None] = None, |
| 140 | + ) -> AsyncCollection[TDoc]: |
| 141 | + """ |
| 142 | + Get a AsyncCollection instance for a specific document model. |
| 143 | +
|
| 144 | + This method allows retrieving a AsyncCollection instance typed to a specific document model. |
| 145 | + If no name is provided, it uses the lowercase name of the model class as |
| 146 | + the collection name. |
| 147 | +
|
| 148 | + Args: |
| 149 | + model (Type[TDoc]): The document model class. |
| 150 | + name (Union[str, None], optional): |
| 151 | + The name of the collection. If None, uses the lowercase model class name. |
| 152 | +
|
| 153 | + Returns: |
| 154 | + AsyncCollection[TDoc]: An AsyncCollection instance typed to the specified document model. |
| 155 | +
|
| 156 | + Example: |
| 157 | + >>> class Company(DocumentSchema): |
| 158 | + ... name: str |
| 159 | + ... num_employees: int |
| 160 | + >>> client = Client(config) |
| 161 | + >>> companies_collection = client.typed_collection(model=Company) |
| 162 | + # This is equivalent to: |
| 163 | + # companies_collection = client.typed_collection(model=Company, name="company") |
| 164 | + """ |
| 165 | + if name is None: |
| 166 | + name = model.__name__.lower() |
| 167 | + collection: AsyncCollection[TDoc] = self.collections[name] |
| 168 | + return collection |
0 commit comments