Skip to content

Commit 3cc4fac

Browse files
authored
Merge pull request #1300 from sanders41/network
Add methods to get and set networks
2 parents 8b817e0 + efcf134 commit 3cc4fac

File tree

5 files changed

+177
-3
lines changed

5 files changed

+177
-3
lines changed

meilisearch_python_sdk/_client.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
KeyCreate,
2323
KeySearch,
2424
KeyUpdate,
25+
Network,
2526
)
2627
from meilisearch_python_sdk.models.health import Health
2728
from meilisearch_python_sdk.models.index import IndexInfo
@@ -196,12 +197,66 @@ async def aclose(self) -> None:
196197
"""
197198
await self.http_client.aclose()
198199

200+
async def add_or_update_networks(self, *, network: Network) -> Network:
201+
"""Set or update remote networks.
202+
203+
Args:
204+
network: Information to use for the networks.
205+
206+
Returns:
207+
An instance of Network containing the network information.
208+
209+
Raises:
210+
MeilisearchCommunicationError: If there was an error communicating with the server.
211+
MeilisearchApiError: If the Meilisearch API returned an error.
212+
213+
Examples:
214+
>>> from meilisearch_python_sdk import AsyncClient
215+
>>> from meilisearch_python_sdk.models.client import Network, Remote
216+
>>>
217+
>>>
218+
>>> network = Network(
219+
>>> self_="remote_1",
220+
>>> remotes={
221+
>>> "remote_1": {"url": "http://localhost:7700", "searchApiKey": "xxxx"},
222+
>>> "remote_2": {"url": "http://localhost:7720", "searchApiKey": "xxxx"},
223+
>>> },
224+
>>> )
225+
>>> async with AsyncClient("http://localhost.com", "masterKey") as client:
226+
>>> response = await client.add_or_update_networks(network=network)
227+
"""
228+
response = await self._http_requests.patch(
229+
"network", network.model_dump(by_alias=True, exclude_none=True)
230+
)
231+
232+
return Network(**response.json())
233+
234+
async def get_networks(self) -> Network:
235+
"""Fetches the remote-networks
236+
237+
Returns:
238+
An instance of Network containing information about each remote.
239+
240+
Raises:
241+
MeilisearchCommunicationError: If there was an error communicating with the server.
242+
MeilisearchApiError: If the Meilisearch API returned an error.
243+
244+
Examples:
245+
>>> from meilisearch_python_sdk import AsyncClient
246+
>>>
247+
>>>
248+
>>> async with AsyncClient("http://localhost.com", "masterKey") as client:
249+
>>> response = await client.get_networks()
250+
"""
251+
response = await self._http_requests.get("network")
252+
253+
return Network(**response.json())
254+
199255
async def create_dump(self) -> TaskInfo:
200256
"""Trigger the creation of a Meilisearch dump.
201257
202258
Returns:
203259
The details of the task.
204-
205260
Raises:
206261
MeilisearchCommunicationError: If there was an error communicating with the server.
207262
MeilisearchApiError: If the Meilisearch API returned an error.
@@ -1061,6 +1116,61 @@ def __init__(
10611116

10621117
self._http_requests = HttpRequests(self.http_client, json_handler=self.json_handler)
10631118

1119+
def add_or_update_networks(self, *, network: Network) -> Network:
1120+
"""Set or update remote networks.
1121+
1122+
Args:
1123+
network: Information to use for the networks.
1124+
1125+
Returns:
1126+
An instance of Network containing the network information.
1127+
1128+
Raises:
1129+
MeilisearchCommunicationError: If there was an error communicating with the server.
1130+
MeilisearchApiError: If the Meilisearch API returned an error.
1131+
1132+
Examples:
1133+
>>> from meilisearch_python_sdk import Client
1134+
>>> from meilisearch_python_sdk.models.client import Network, Remote
1135+
>>>
1136+
>>>
1137+
>>> network = Network(
1138+
>>> self_="remote_1",
1139+
>>> remotes={
1140+
>>> "remote_1": {"url": "http://localhost:7700", "searchApiKey": "xxxx"},
1141+
>>> "remote_2": {"url": "http://localhost:7720", "searchApiKey": "xxxx"},
1142+
>>> },
1143+
>>> )
1144+
>>> client = Client("http://localhost.com", "masterKey")
1145+
>>> response = client.add_or_update_networks(network=network)
1146+
"""
1147+
response = self._http_requests.patch(
1148+
"network", network.model_dump(by_alias=True, exclude_none=True)
1149+
)
1150+
1151+
return Network(**response.json())
1152+
1153+
def get_networks(self) -> Network:
1154+
"""Fetches the remote-networks
1155+
1156+
Returns:
1157+
An instance of Network containing information about each remote.
1158+
1159+
Raises:
1160+
MeilisearchCommunicationError: If there was an error communicating with the server.
1161+
MeilisearchApiError: If the Meilisearch API returned an error.
1162+
1163+
Examples:
1164+
>>> from meilisearch_python_sdk import AsyncClient
1165+
>>>
1166+
>>>
1167+
>>> client = Client("http://localhost.com", "masterKey")
1168+
>>> response = client.get_networks()
1169+
"""
1170+
response = self._http_requests.get("network")
1171+
1172+
return Network(**response.json())
1173+
10641174
def create_dump(self) -> TaskInfo:
10651175
"""Trigger the creation of a Meilisearch dump.
10661176

meilisearch_python_sdk/models/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Mapping
34
from datetime import datetime
45

56
import pydantic
@@ -84,3 +85,13 @@ class KeySearch(CamelBase):
8485
offset: int
8586
limit: int
8687
total: int
88+
89+
90+
class Remote(CamelBase):
91+
url: str | None = None
92+
search_api_key: str | None = None
93+
94+
95+
class Network(CamelBase):
96+
self_: str | None = pydantic.Field(None, alias="self")
97+
remotes: Mapping[str, Remote] | None = None

tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ async def enable_edit_by_function(base_url, ssl_verify):
304304
yield
305305

306306

307+
@pytest.fixture(scope="session", autouse=True)
308+
async def enable_network(base_url, ssl_verify):
309+
async with HttpxAsyncClient(
310+
base_url=base_url, headers={"Authorization": f"Bearer {MASTER_KEY}"}, verify=ssl_verify
311+
) as client:
312+
await client.patch("/experimental-features", json={"network": True})
313+
yield
314+
315+
307316
@pytest.fixture
308317
async def create_tasks(async_empty_index, small_movies):
309318
"""Ensures there are some tasks present for testing."""

tests/test_async_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
MeilisearchTaskFailedError,
2323
MeilisearchTimeoutError,
2424
)
25-
from meilisearch_python_sdk.models.client import KeyCreate, KeyUpdate
25+
from meilisearch_python_sdk.models.client import KeyCreate, KeyUpdate, Network
2626
from meilisearch_python_sdk.models.index import IndexInfo
2727
from meilisearch_python_sdk.models.version import Version
2828
from meilisearch_python_sdk.types import JsonDict
@@ -1048,3 +1048,25 @@ async def test_get_batch(async_client, async_empty_index, small_movies):
10481048
async def test_get_batch_not_found(async_client):
10491049
with pytest.raises(BatchNotFoundError):
10501050
await async_client.get_batch(999999999)
1051+
1052+
1053+
async def test_get_networks(async_client):
1054+
response = await async_client.get_networks()
1055+
1056+
assert isinstance(response, Network)
1057+
1058+
1059+
async def test_add_or_update_networks(async_client):
1060+
network = Network(
1061+
self_="remote_1",
1062+
remotes={
1063+
"remote_1": {"url": "http://localhost:7700", "searchApiKey": "xxxxxxxxxxxxxx"},
1064+
"remote_2": {"url": "http://localhost:7720", "searchApiKey": "xxxxxxxxxxxxxxx"},
1065+
},
1066+
)
1067+
response = await async_client.add_or_update_networks(network=network)
1068+
1069+
assert response.self_ == "remote_1"
1070+
assert len(response.remotes) >= 2
1071+
assert "remote_1" in response.remotes
1072+
assert "remote_2" in response.remotes

tests/test_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
MeilisearchTaskFailedError,
2020
MeilisearchTimeoutError,
2121
)
22-
from meilisearch_python_sdk.models.client import KeyCreate, KeyUpdate
22+
from meilisearch_python_sdk.models.client import KeyCreate, KeyUpdate, Network
2323
from meilisearch_python_sdk.models.index import IndexInfo
2424
from meilisearch_python_sdk.models.version import Version
2525
from meilisearch_python_sdk.types import JsonDict
@@ -1029,3 +1029,25 @@ def test_get_batch(client, empty_index, small_movies):
10291029
def test_get_batch_not_found(client):
10301030
with pytest.raises(BatchNotFoundError):
10311031
client.get_batch(999999999)
1032+
1033+
1034+
def test_get_networks(client):
1035+
response = client.get_networks()
1036+
1037+
assert isinstance(response, Network)
1038+
1039+
1040+
def test_add_or_update_networks(client):
1041+
network = Network(
1042+
self_="remote_1",
1043+
remotes={
1044+
"remote_1": {"url": "http://localhost:7700", "searchApiKey": "xxxxxxxxxxxxxx"},
1045+
"remote_2": {"url": "http://localhost:7720", "searchApiKey": "xxxxxxxxxxxxxxx"},
1046+
},
1047+
)
1048+
response = client.add_or_update_networks(network=network)
1049+
1050+
assert response.self_ == "remote_1"
1051+
assert len(response.remotes) >= 2
1052+
assert "remote_1" in response.remotes
1053+
assert "remote_2" in response.remotes

0 commit comments

Comments
 (0)