22
33import asyncio
44from functools import wraps
5- from typing import Any , Callable
5+ from typing import Any , Callable , NamedTuple
66
77from meilisearch_python_sdk import AsyncClient , Client
88from meilisearch_python_sdk ._utils import use_task_groups
99
1010
11+ class ConnectionInfo (NamedTuple ):
12+ """Infomation on how to connect to Meilisearch.
13+
14+ url: URL for the Meilisearch server.
15+ api_key: The API key for the server.
16+ """
17+
18+ url : str
19+ api_key : str
20+
21+
1122def async_add_documments (
1223 * ,
1324 index_name : str ,
14- async_client : AsyncClient | None = None ,
15- url : str | None = None ,
16- api_token : str | None = None ,
25+ connection_info : AsyncClient | ConnectionInfo ,
1726 batch_size : int | None = None ,
1827 primary_key : str | None = None ,
1928 wait_for_task : bool = False ,
@@ -25,10 +34,8 @@ def async_add_documments(
2534 Args:
2635
2736 index_name: The name of the index to which the documents should be added.
28- async_client: An AsyncClient instance. Default = None.
29- url: URL for the Meilisearch server. Default = None.
30- api_token: The API key for the server. This key needs to have permission to add documents
31- to the index, and create the index if it does not already exist. Default = None.
37+ connection_info: Either an AsyncClient instance ConnectionInfo with informtaion on how to
38+ connect to Meilisearch.
3239 batch_size: If provided the documents will be sent in batches of the specified size.
3340 Otherwise all documents are sent at once. Default = None.
3441 primary_key: The primary key of the documents. This will be ignored if already set.
@@ -48,9 +55,21 @@ def async_add_documments(
4855
4956 Examples:
5057
51- >>> from meilisearch_python_sdk.decorators import async_add_documents
58+ >>> from meilisearch_python_sdk import AsyncClient
59+ >>> from meilisearch_python_sdk.decorators import async_add_documents, ConnectionInfo
60+ >>>
61+ >>>
62+ >>> # with `AsyncClient`
63+ >>> client = AsyncClient(url="http://localhost:7700", api_key="masterKey")
64+ >>> @async_add_documents(index_name="movies", connection_info=client)
65+ >>> async def my_function() -> list[dict[str, Any]]:
66+ >>> return [{"id": 1, "title": "Test 1"}, {"id": 2, "title": "Test 2"}]
5267 >>>
53- >>> @async_add_documents(index_name="movies", url="http://localhost:7700", api_token="masterKey")
68+ >>> # with `ConnectionInfo`
69+ >>> @async_add_documents(
70+ index_name="movies",
71+ connection_info=ConnectionInfo(url="http://localhost:7700", api_key="masterKey",
72+ )
5473 >>> async def my_function() -> list[dict[str, Any]]:
5574 >>> return [{"id": 1, "title": "Test 1"}, {"id": 2, "title": "Test 2"}]
5675 """
@@ -59,16 +78,18 @@ def decorator(func: Callable) -> Callable:
5978 @wraps (func )
6079 async def wrapper (* args : Any , ** kwargs : Any ) -> Any :
6180 result = await func (* args , ** kwargs )
62- if async_client :
81+ if isinstance ( connection_info , AsyncClient ) :
6382 await _async_add_documents (
64- async_client , index_name , result , batch_size , primary_key , wait_for_task
83+ connection_info ,
84+ index_name ,
85+ result ,
86+ batch_size ,
87+ primary_key ,
88+ wait_for_task ,
6589 )
6690 return result
6791
68- if not url :
69- raise ValueError ("Either an async_client or url is required" )
70-
71- async with AsyncClient (url , api_token ) as client :
92+ async with AsyncClient (connection_info .url , connection_info .api_key ) as client :
7293 await _async_add_documents (
7394 client , index_name , result , batch_size , primary_key , wait_for_task
7495 )
@@ -83,9 +104,7 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
83104def add_documments (
84105 * ,
85106 index_name : str ,
86- client : Client | None = None ,
87- url : str | None = None ,
88- api_token : str | None = None ,
107+ connection_info : Client | ConnectionInfo ,
89108 batch_size : int | None = None ,
90109 primary_key : str | None = None ,
91110 wait_for_task : bool = False ,
@@ -97,10 +116,8 @@ def add_documments(
97116 Args:
98117
99118 index_name: The name of the index to which the documents should be added.
100- client: An Client instance. Default = None.
101- url: URL for the Meilisearch server. Default = None.
102- api_token: The API key for the server. This key needs to have permission to add documents
103- to the index, and create the index if it does not already exist. Default = None.
119+ connection_info: Either an Client instance ConnectionInfo with informtaion on how to
120+ connect to Meilisearch.
104121 batch_size: If provided the documents will be sent in batches of the specified size.
105122 Otherwise all documents are sent at once. Default = None.
106123 primary_key: The primary key of the documents. This will be ignored if already set.
@@ -120,9 +137,21 @@ def add_documments(
120137
121138 Examples:
122139
123- >>> from meilisearch_python_sdk.decorators import add_documents
140+ >>> from meilisearch_python_sdk import Client
141+ >>> from meilisearch_python_sdk.decorators import add_documents, ConnectionInfo
142+ >>>
124143 >>>
125- >>> @add_documents(index_name="movies", url="http://localhost:7700", api_token="masterKey")
144+ >>> # With `Client`
145+ >>> client = Client(url="http://localhost:7700", api_key="masterKey")
146+ >>> @add_documents(index_name="movies", connection_info=client)
147+ >>> def my_function() -> list[dict[str, Any]]:
148+ >>> return [{"id": 1, "title": "Test 1"}, {"id": 2, "title": "Test 2"}]
149+ >>>
150+ >>> # With `ConnectionInfo`
151+ >>> @add_documents(
152+ index_name="movies",
153+ connection_info=ConnectionInfo(url="http://localhost:7700", api_key="masterKey"),
154+ )
126155 >>> def my_function() -> list[dict[str, Any]]:
127156 >>> return [{"id": 1, "title": "Test 1"}, {"id": 2, "title": "Test 2"}]
128157 """
@@ -131,16 +160,25 @@ def decorator(func: Callable) -> Callable:
131160 @wraps (func )
132161 def wrapper (* args : Any , ** kwargs : Any ) -> Any :
133162 result = func (* args , ** kwargs )
134- if client :
135- _add_documents (client , index_name , result , batch_size , primary_key , wait_for_task )
163+ if isinstance (connection_info , Client ):
164+ _add_documents (
165+ connection_info ,
166+ index_name ,
167+ result ,
168+ batch_size ,
169+ primary_key ,
170+ wait_for_task ,
171+ )
136172 return result
137173
138- if not url :
139- raise ValueError ("Either an async_client or url is required" )
140-
141- decorator_client = Client (url , api_token )
174+ decorator_client = Client (url = connection_info .url , api_key = connection_info .api_key )
142175 _add_documents (
143- decorator_client , index_name , result , batch_size , primary_key , wait_for_task
176+ decorator_client ,
177+ index_name ,
178+ result ,
179+ batch_size ,
180+ primary_key ,
181+ wait_for_task ,
144182 )
145183
146184 return result
0 commit comments