Skip to content

Commit 020d792

Browse files
authored
Merge pull request #738 from sanders41/decorators
Change decorator connection parameters
2 parents 6c32f40 + 3953937 commit 020d792

File tree

2 files changed

+81
-65
lines changed

2 files changed

+81
-65
lines changed

meilisearch_python_sdk/decorators.py

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22

33
import asyncio
44
from functools import wraps
5-
from typing import Any, Callable
5+
from typing import Any, Callable, NamedTuple
66

77
from meilisearch_python_sdk import AsyncClient, Client
88
from 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+
1122
def 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:
83104
def 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

tests/test_decorators.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import pytest
44

5-
from meilisearch_python_sdk.decorators import add_documments, async_add_documments
5+
from meilisearch_python_sdk.decorators import (
6+
ConnectionInfo,
7+
add_documments,
8+
async_add_documments,
9+
)
610

711

812
@pytest.mark.parametrize("batch_size, primary_key", [(None, None), (10, "alternate")])
@@ -15,7 +19,7 @@ def test_add_documents_with_client(batch_size, primary_key, client):
1519

1620
@add_documments(
1721
index_name=index_name,
18-
client=client,
22+
connection_info=client,
1923
batch_size=batch_size,
2024
primary_key=primary_key,
2125
wait_for_task=True,
@@ -35,7 +39,7 @@ def tester():
3539

3640

3741
@pytest.mark.parametrize("batch_size, primary_key", [(None, None), (10, "alternate")])
38-
def test_add_documents_with_url(batch_size, primary_key, client, base_url, master_key):
42+
def test_add_documents_with_connection_info(batch_size, primary_key, client, base_url, master_key):
3943
index_name = str(uuid4())
4044
documents = []
4145

@@ -44,8 +48,7 @@ def test_add_documents_with_url(batch_size, primary_key, client, base_url, maste
4448

4549
@add_documments(
4650
index_name=index_name,
47-
url=base_url,
48-
api_token=master_key,
51+
connection_info=ConnectionInfo(url=base_url, api_key=master_key),
4952
batch_size=batch_size,
5053
primary_key=primary_key,
5154
wait_for_task=True,
@@ -64,18 +67,6 @@ def tester():
6467
assert result.results == documents
6568

6669

67-
def test_add_documents_no_client_or_url():
68-
index_name = str(uuid4())
69-
documents = [{"id": 1, "title": "Title 1"}, {"id": 2, "title": "Title 2"}]
70-
71-
@add_documments(index_name=index_name, wait_for_task=True)
72-
def tester():
73-
return documents
74-
75-
with pytest.raises(ValueError):
76-
tester()
77-
78-
7970
@pytest.mark.parametrize("batch_size, primary_key", [(None, None), (10, "alternate")])
8071
async def test_async_add_documents_with_client(batch_size, primary_key, async_client):
8172
index_name = str(uuid4())
@@ -86,7 +77,7 @@ async def test_async_add_documents_with_client(batch_size, primary_key, async_cl
8677

8778
@async_add_documments(
8879
index_name=index_name,
89-
async_client=async_client,
80+
connection_info=async_client,
9081
batch_size=batch_size,
9182
primary_key=primary_key,
9283
wait_for_task=True,
@@ -108,7 +99,7 @@ async def tester():
10899

109100

110101
@pytest.mark.parametrize("batch_size, primary_key", [(None, None), (10, "alternate")])
111-
async def test_async_add_documents_with_url(
102+
async def test_async_add_documents_with_connection_info(
112103
batch_size, primary_key, async_client, base_url, master_key
113104
):
114105
index_name = str(uuid4())
@@ -119,8 +110,7 @@ async def test_async_add_documents_with_url(
119110

120111
@async_add_documments(
121112
index_name=index_name,
122-
url=base_url,
123-
api_token=master_key,
113+
connection_info=ConnectionInfo(url=base_url, api_key=master_key),
124114
batch_size=batch_size,
125115
primary_key=primary_key,
126116
wait_for_task=True,
@@ -139,15 +129,3 @@ async def tester():
139129

140130
# order will be random since documents were added async so sort them first.
141131
assert sorted(result.results, key=lambda x: x["id"]) == documents
142-
143-
144-
async def test_async_add_documents_no_client():
145-
index_name = str(uuid4())
146-
documents = [{"id": 1, "title": "Title 1"}, {"id": 2, "title": "Title 2"}]
147-
148-
@async_add_documments(index_name=index_name, wait_for_task=True)
149-
async def tester():
150-
return documents
151-
152-
with pytest.raises(ValueError):
153-
await tester()

0 commit comments

Comments
 (0)