diff --git a/README.md b/README.md index d4a10176..7ff9bbad 100644 --- a/README.md +++ b/README.md @@ -66,15 +66,15 @@ async with AsyncClient('http://127.0.0.1:7700', 'masterKey') as client: ```py from meilisearch_python_sdk import Client -client = Client('http://127.0.0.1:7700', 'masterKey') -index = client.index("books") +with Client('http://127.0.0.1:7700', 'masterKey') as client: + index = client.index("books") -documents = [ - {"id": 1, "title": "Ready Player One"}, - {"id": 42, "title": "The Hitchhiker's Guide to the Galaxy"}, -] + documents = [ + {"id": 1, "title": "Ready Player One"}, + {"id": 42, "title": "The Hitchhiker's Guide to the Galaxy"}, + ] -index.add_documents(documents) + index.add_documents(documents) ``` The server will return an update id that can be used to diff --git a/docs/client_api.md b/docs/client_api.md index e17bc801..bde203dd 100644 --- a/docs/client_api.md +++ b/docs/client_api.md @@ -1,16 +1,17 @@ ## `client` Usage -### Create a client +### Create a client with a context manager -To create a client: +This client runs in a context manager which ensures that everything is cleaned up after the use of +the client is done. To create a client: ```py -from milisearch_python_sdk import Client +from meilisearch-python-sdk import Client -client = Client("http://localhost:7700", "masterKey") -index = client.index("movies") -... +with Client("http://localhost:7700", "masterKey") as client: + index = client.index("movies") + ... ``` ### Custom headers @@ -21,15 +22,32 @@ client. ```py from meilisearch_python_sdk import Client -client = Client( +with Client( "http://127.0.0.1:7700", "masterKey", custom_headers={"header_key_1": "header_value_1", "header_key_2": "header_value_2"} -) -index = client.index("movies") +) as client: + index = client.index("movies") ... ``` +### Create a client without a context manager + +It is also possible to call the client without using a context manager, but in doing so you will +need to make sure to do the cleanup yourself: + +```py +from meilisearch-python-sdk import Client + + +try: + client = Client("http://localhost:7700", "masterKey") + ... +finally: + await client.close() + +``` + ## `Client` API ::: meilisearch_python_sdk.Client diff --git a/docs/json_handler.md b/docs/json_handler.md index 86ab064b..8c8bb1e8 100644 --- a/docs/json_handler.md +++ b/docs/json_handler.md @@ -37,9 +37,9 @@ documents = [ {"id": uuid4(), "title": "test 1", "when": datetime.now()}, {"id": uuid4(), "title": "Test 2", "when": datetime.now()}, ] -client = Client("http://127.0.0.1:7700", json_handler=BuiltinHandler(serializer=CustomEncoder)) -index = client.index("movies", primary_key="id") -index.add_documents(documents) +with Client("http://127.0.0.1:7700", json_handler=BuiltinHandler(serializer=CustomEncoder)) as client: + index = client.index("movies", primary_key="id") + index.add_documents(documents) ``` ## orjson @@ -57,9 +57,9 @@ documents = [ {"id": uuid4(), "title": "test 1"}, {"id": uuid4(), "title": "Test 2"}, ] -client = Client("http://127.0.0.1:7700", json_handler=OrjsonHandler()) -index = client.index("movies", primary_key="id") -index.add_documents(documents) +with Client("http://127.0.0.1:7700", json_handler=OrjsonHandler()) as client: + index = client.index("movies", primary_key="id") + index.add_documents(documents) ``` ## ujson @@ -77,7 +77,7 @@ documents = [ {"id": uuid4(), "title": "test 1"}, {"id": uuid4(), "title": "Test 2"}, ] -client = Client("http://127.0.0.1:7700", json_handler=UjsonHandler()) -index = client.index("movies", primary_key="id") -index.add_documents(documents) +with Client("http://127.0.0.1:7700", json_handler=UjsonHandler()) as client: + index = client.index("movies", primary_key="id") + index.add_documents(documents) ``` diff --git a/docs/plugins.md b/docs/plugins.md index 71dedf35..18ed8c96 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -84,13 +84,13 @@ async def main() -> int: with open("datasets/small_movies.json") as f: documents = json.load(f) - client = AsyncClient("http://127.0.0.1:7700", "masterKey") - plugins = AsyncIndexPlugins(search_plugins=(SearchTrackerPlugin(),)) - index = await client.create_index("movies", primary_key="id", plugins=plugins) - task = await index.add_documents(documents) - await client.wait_for_task(task.task_uid) - result = await index.search("Cars") - print(result) # noqa: T201 + async with AsyncClient("http://127.0.0.1:7700", "masterKey") as client: + plugins = AsyncIndexPlugins(search_plugins=(SearchTrackerPlugin(),)) + index = await client.create_index("movies", primary_key="id", plugins=plugins) + task = await index.add_documents(documents) + await client.wait_for_task(task.task_uid) + result = await index.search("Cars") + print(result) # noqa: T201 return 0 @@ -142,13 +142,13 @@ def main() -> int: with open("datasets/small_movies.json") as f: documents = json.load(f) - client = Client("http://127.0.0.1:7700", "masterKey") - plugins = IndexPlugins(search_plugins=(SearchTrackerPlugin(),)) - index = client.create_index("movies", primary_key="id", plugins=plugins) - task = index.add_documents(documents) - client.wait_for_task(task.task_uid) - result = index.search("Cars") - print(result) # noqa: T201 + with Client("http://127.0.0.1:7700", "masterKey") as client: + plugins = IndexPlugins(search_plugins=(SearchTrackerPlugin(),)) + index = client.create_index("movies", primary_key="id", plugins=plugins) + task = index.add_documents(documents) + client.wait_for_task(task.task_uid) + result = index.search("Cars") + print(result) # noqa: T201 return 0 @@ -224,17 +224,17 @@ async def main() -> int: with open("datasets/small_movies.json") as f: documents = json.load(f) - client = AsyncClient("http://127.0.0.1:7700", "masterKey") - plugins = AsyncIndexPlugins( - add_documents_plugins=(ModifyDocumentPlugin(),), - update_documents_plugins=(ModifyDocumentPlugin(),), - search_plugins=(FilterSearchResultsPlugin(),), - ) - index = await client.create_index("movies", primary_key="id", plugins=plugins) - task = await index.add_documents(documents) - await client.wait_for_task(task.task_uid) - result = await index.search("cars") - print(result) # noqa: T201 + async with AsyncClient("http://127.0.0.1:7700", "masterKey") as client: + plugins = AsyncIndexPlugins( + add_documents_plugins=(ModifyDocumentPlugin(),), + update_documents_plugins=(ModifyDocumentPlugin(),), + search_plugins=(FilterSearchResultsPlugin(),), + ) + index = await client.create_index("movies", primary_key="id", plugins=plugins) + task = await index.add_documents(documents) + await client.wait_for_task(task.task_uid) + result = await index.search("cars") + print(result) # noqa: T201 return 0 @@ -295,17 +295,17 @@ def main() -> int: with open("datasets/small_movies.json") as f: documents = json.load(f) - client = Client("http://127.0.0.1:7700", "masterKey") - plugins = IndexPlugins( - add_documents_plugins=(ModifyDocumentPlugin(),), - update_documents_plugins=(ModifyDocumentPlugin(),), - search_plugins=(FilterSearchResultsPlugin(),), - ) - index = client.create_index("movies", primary_key="id", plugins=plugins) - task = index.add_documents(documents) - client.wait_for_task(task.task_uid) - result = index.search("cars") - print(result) # noqa: T201 + with Client("http://127.0.0.1:7700", "masterKey") as client: + plugins = IndexPlugins( + add_documents_plugins=(ModifyDocumentPlugin(),), + update_documents_plugins=(ModifyDocumentPlugin(),), + search_plugins=(FilterSearchResultsPlugin(),), + ) + index = client.create_index("movies", primary_key="id", plugins=plugins) + task = index.add_documents(documents) + client.wait_for_task(task.task_uid) + result = index.search("cars") + print(result) # noqa: T201 return 0 diff --git a/examples/add_documents_decorator.py b/examples/add_documents_decorator.py index 9f5fdd2e..5d3687c2 100644 --- a/examples/add_documents_decorator.py +++ b/examples/add_documents_decorator.py @@ -19,12 +19,12 @@ def load_documents() -> list[dict[str, Any]]: def main() -> int: - client = Client("http://127.0.0.1:7700", "masterKey") - index = client.create_index("movies", "id") - load_documents() - documents = index.get_documents() + with Client("http://127.0.0.1:7700", "masterKey") as client: + index = client.create_index("movies", "id") + load_documents() + documents = index.get_documents() - print(documents) # noqa: T201 + print(documents) # noqa: T201 return 0 diff --git a/examples/add_documents_in_batches.py b/examples/add_documents_in_batches.py index 93b6ee31..69df5eda 100644 --- a/examples/add_documents_in_batches.py +++ b/examples/add_documents_in_batches.py @@ -9,11 +9,11 @@ def main() -> int: with open("../datasets/small_movies.json") as f: documents = json.load(f) - client = Client("http://127.0.0.1:7700", "masterKey") - index = client.index("movies") + with Client("http://127.0.0.1:7700", "masterKey") as client: + index = client.index("movies") - # Meilisearch prefers larger batch sizes so set this as large as you can. - index.add_documents_in_batches(documents, primary_key="id", batch_size=1000) + # Meilisearch prefers larger batch sizes so set this as large as you can. + index.add_documents_in_batches(documents, primary_key="id", batch_size=1000) return 0 diff --git a/examples/documents_and_search_results.py b/examples/documents_and_search_results.py index acdfd78f..3b4f4800 100644 --- a/examples/documents_and_search_results.py +++ b/examples/documents_and_search_results.py @@ -50,17 +50,17 @@ def main() -> int: with open("../datasets/small_movies.json") as f: documents = json.load(f) - client = Client("http://127.0.0.1:7700", "masterKey") - plugins = IndexPlugins( - add_documents_plugins=(ModifyDocumentPlugin(),), - update_documents_plugins=(ModifyDocumentPlugin(),), - search_plugins=(FilterSearchResultsPlugin(),), - ) - index = client.create_index("movies", primary_key="id", plugins=plugins) - task = index.add_documents(documents) - client.wait_for_task(task.task_uid) - result = index.search("cars") - print(result) # noqa: T201 + with Client("http://127.0.0.1:7700", "masterKey") as client: + plugins = IndexPlugins( + add_documents_plugins=(ModifyDocumentPlugin(),), + update_documents_plugins=(ModifyDocumentPlugin(),), + search_plugins=(FilterSearchResultsPlugin(),), + ) + index = client.create_index("movies", primary_key="id", plugins=plugins) + task = index.add_documents(documents) + client.wait_for_task(task.task_uid) + result = index.search("cars") + print(result) # noqa: T201 return 0 diff --git a/examples/orjson_example.py b/examples/orjson_example.py index 580b2482..06dfe30e 100644 --- a/examples/orjson_example.py +++ b/examples/orjson_example.py @@ -8,9 +8,9 @@ def add_documents(file_path: Path | str = "../datasets/small_movies.json") -> TaskInfo: - client = Client("http://127.0.0.1:7700", "masterKey", json_handler=OrjsonHandler()) - index = client.create_index("movies", primary_key="id") - return index.add_documents_from_file(file_path) + with Client("http://127.0.0.1:7700", "masterKey", json_handler=OrjsonHandler()) as client: + index = client.create_index("movies", primary_key="id") + return index.add_documents_from_file(file_path) def main() -> int: diff --git a/examples/search_tracker.py b/examples/search_tracker.py index 15a1dbbe..5dc6a975 100644 --- a/examples/search_tracker.py +++ b/examples/search_tracker.py @@ -53,13 +53,13 @@ def search(index: Index, query: str) -> SearchResults[JsonDict]: def main() -> int: - client = Client("http://127.0.0.1:7700", "masterKey") - plugins = IndexPlugins(search_plugins=(SearchTrackerPlugin(),)) - index = client.create_index("movies", primary_key="id", plugins=plugins) - task = add_documents(index) - client.wait_for_task(task.task_uid) - result = search(index, "Cars") - print(result) # noqa: T201 + with Client("http://127.0.0.1:7700", "masterKey") as client: + plugins = IndexPlugins(search_plugins=(SearchTrackerPlugin(),)) + index = client.create_index("movies", primary_key="id", plugins=plugins) + task = add_documents(index) + client.wait_for_task(task.task_uid) + result = search(index, "Cars") + print(result) # noqa: T201 return 0 diff --git a/examples/ujson_example.py b/examples/ujson_example.py index 1f810d3a..a0d3f0e5 100644 --- a/examples/ujson_example.py +++ b/examples/ujson_example.py @@ -8,9 +8,9 @@ def add_documents(file_path: Path | str = "../datasets/small_movies.json") -> TaskInfo: - client = Client("http://127.0.0.1:7700", "masterKey", json_handler=UjsonHandler()) - index = client.create_index("movies", primary_key="id") - return index.add_documents_from_file(file_path) + with Client("http://127.0.0.1:7700", "masterKey", json_handler=UjsonHandler()) as client: + index = client.create_index("movies", primary_key="id") + return index.add_documents_from_file(file_path) def main() -> int: diff --git a/examples/update_settings.py b/examples/update_settings.py index 6c2dd21a..17235c11 100644 --- a/examples/update_settings.py +++ b/examples/update_settings.py @@ -25,11 +25,11 @@ def update_settings(index: Index) -> TaskInfo: def main() -> int: - client = Client("http://127.0.0.1:7700", "masterKey") - index = client.create_index("movies", primary_key="id") - task = update_settings(index) - client.wait_for_task(task.task_uid) - add_documents(index) + with Client("http://127.0.0.1:7700", "masterKey") as client: + index = client.create_index("movies", primary_key="id") + task = update_settings(index) + client.wait_for_task(task.task_uid) + add_documents(index) return 0 diff --git a/meilisearch_python_sdk/_client.py b/meilisearch_python_sdk/_client.py index 4975583e..b67bcddb 100644 --- a/meilisearch_python_sdk/_client.py +++ b/meilisearch_python_sdk/_client.py @@ -117,10 +117,10 @@ def generate_tenant_token( >>> >>> expires_at = datetime.now(tz=timezone.utc) + timedelta(days=7) >>> - >>> client = Client("http://localhost.com", "masterKey") - >>> token = client.generate_tenant_token( - >>> search_rules = ["*"], api_key=api_key, expires_at=expires_at - >>> ) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> token = client.generate_tenant_token( + >>> search_rules = ["*"], api_key=api_key, expires_at=expires_at + >>> ) """ if isinstance(search_rules, dict) and search_rules.get("indexes"): for index in search_rules["indexes"]: @@ -1293,6 +1293,24 @@ def __init__( self._http_requests = HttpRequests(self.http_client, json_handler=self.json_handler) + def __enter__(self) -> Self: + return self + + def __exit__( + self, + et: type[BaseException] | None, + ev: type[BaseException] | None, + traceback: TracebackType | None, + ) -> None: + self.close() + + def close(self) -> None: + """Closes the client. + + This only needs to be used if the client was not created with a context manager. + """ + self.http_client.close() + def add_or_update_networks(self, *, network: Network) -> Network: """Set or update remote networks. @@ -1318,8 +1336,8 @@ def add_or_update_networks(self, *, network: Network) -> Network: >>> "remote_2": {"url": "http://localhost:7720", "searchApiKey": "xxxx"}, >>> }, >>> ) - >>> client = Client("http://localhost.com", "masterKey") - >>> response = client.add_or_update_networks(network=network) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> response = client.add_or_update_networks(network=network) """ response = self._http_requests.patch( "network", network.model_dump(by_alias=True, exclude_none=True) @@ -1341,8 +1359,8 @@ def get_networks(self) -> Network: >>> from meilisearch_python_sdk import AsyncClient >>> >>> - >>> client = Client("http://localhost.com", "masterKey") - >>> response = client.get_networks() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> response = client.get_networks() """ response = self._http_requests.get("network") @@ -1360,8 +1378,8 @@ def get_webhooks(self) -> Webhooks: Examples: >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> webhooks = client.get_webhooks() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> webhooks = client.get_webhooks() """ response = self._http_requests.get("webhooks") @@ -1382,8 +1400,8 @@ def get_webhook(self, uuid: str) -> Webhook: Examples: >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> webhook = client.get_webhook("abc-123") + >>> with Client("http://localhost.com", "masterKey"): + >>> webhook = client.get_webhook("abc-123") """ response = self._http_requests.get(f"webhooks/{uuid}") @@ -1405,12 +1423,12 @@ def create_webhook(self, webhook: WebhookCreate) -> Webhook: Examples: >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.models.webhook import WebhookCreate - >>> client = Client("http://localhost.com", "masterKey") - >>> webhook_config = WebhookCreate( - >>> url="https://example.com/webhook", - >>> headers={"Authorization": "Bearer token"} - >>> ) - >>> webhook = client.create_webhook(webhook_config) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> webhook_config = WebhookCreate( + >>> url="https://example.com/webhook", + >>> headers={"Authorization": "Bearer token"} + >>> ) + >>> webhook = client.create_webhook(webhook_config) """ response = self._http_requests.post( "webhooks", webhook.model_dump(by_alias=True, exclude_none=True) @@ -1435,9 +1453,9 @@ def update_webhook(self, *, uuid: str, webhook: WebhookUpdate) -> Webhook: Examples: >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.models.webhook import WebhookUpdate - >>> client = Client("http://localhost.com", "masterKey") - >>> webhook_update = WebhookUpdate(url="https://example.com/new-webhook") - >>> webhook = client.update_webhook("abc-123", webhook_update) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> webhook_update = WebhookUpdate(url="https://example.com/new-webhook") + >>> webhook = client.update_webhook("abc-123", webhook_update) """ response = self._http_requests.patch( f"webhooks/{uuid}", webhook.model_dump(by_alias=True, exclude_none=True) @@ -1460,8 +1478,8 @@ def delete_webhook(self, uuid: str) -> int: Examples: >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> client.delete_webhook("abc-123") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.delete_webhook("abc-123") """ response = self._http_requests.delete(f"webhooks/{uuid}") return response.status_code @@ -1478,8 +1496,8 @@ def create_dump(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> client.create_dump() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.create_dump() """ response = self._http_requests.post("dumps") @@ -1525,8 +1543,8 @@ def create_index( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.create_index("movies") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.create_index("movies") """ return Index.create( self.http_client, @@ -1552,8 +1570,8 @@ def create_snapshot(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> client.create_snapshot() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.create_snapshot() """ response = self._http_requests.post("snapshots") @@ -1574,8 +1592,8 @@ def delete_index_if_exists(self, uid: str) -> bool: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> client.delete_index_if_exists() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.delete_index_if_exists() """ response = self._http_requests.delete(f"indexes/{uid}") status = self.wait_for_task(response.json()["taskUid"], timeout_in_ms=100000) @@ -1602,8 +1620,8 @@ def get_indexes( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") as client: - >>> indexes = client.get_indexes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> indexes = client.get_indexes() """ url = _build_offset_limit_url("indexes", offset, limit) response = self._http_requests.get(url) @@ -1638,8 +1656,8 @@ def get_index(self, uid: str) -> Index: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.get_index() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.get_index() """ return Index(self.http_client, uid, json_handler=self.json_handler).fetch_info() @@ -1659,8 +1677,8 @@ def index(self, uid: str, *, plugins: IndexPlugins | None = None) -> Index: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") """ return Index(self.http_client, uid=uid, plugins=plugins, json_handler=self.json_handler) @@ -1677,8 +1695,8 @@ def get_all_stats(self) -> ClientStats: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> stats = client.get_all_stats() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> stats = client.get_all_stats() """ response = self._http_requests.get("stats") @@ -1711,8 +1729,8 @@ def get_or_create_index( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.get_or_create_index("movies") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.get_or_create_index("movies") """ try: index_instance = self.get_index(uid) @@ -1741,13 +1759,13 @@ def create_key(self, key: KeyCreate) -> Key: Examples >>> from meilisearch_python_sdk import Client >>> from meilissearch_async_client.models.client import KeyCreate - >>> client = Client("http://localhost.com", "masterKey") - >>> key_info = KeyCreate( - >>> description="My new key", - >>> actions=["search"], - >>> indexes=["movies"], - >>> ) - >>> keys = client.create_key(key_info) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> key_info = KeyCreate( + >>> description="My new key", + >>> actions=["search"], + >>> indexes=["movies"], + >>> ) + >>> keys = client.create_key(key_info) """ response = self._http_requests.post( "keys", self.json_handler.loads(key.model_dump_json(by_alias=True)) @@ -1770,8 +1788,8 @@ def delete_key(self, key: str) -> int: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> client.delete_key("abc123") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.delete_key("abc123") """ response = self._http_requests.delete(f"keys/{key}") return response.status_code @@ -1793,8 +1811,8 @@ def get_keys(self, *, offset: int | None = None, limit: int | None = None) -> Ke Examples >>> from meilisearch_python_sdk import Client - >>> client = AsyncClient("http://localhost.com", "masterKey") - >>> keys = client.get_keys() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> keys = client.get_keys() """ url = _build_offset_limit_url("keys", offset, limit) response = self._http_requests.get(url) @@ -1816,8 +1834,8 @@ def get_key(self, key: str) -> Key: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> keys = client.get_key("abc123") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> keys = client.get_key("abc123") """ response = self._http_requests.get(f"keys/{key}") @@ -1840,12 +1858,12 @@ def update_key(self, key: KeyUpdate) -> Key: Examples >>> from meilisearch_python_sdk import Client >>> from meilissearch_async_client.models.client import KeyUpdate - >>> client = Client("http://localhost.com", "masterKey") - >>> key_info = KeyUpdate( - key="abc123", - >>> indexes=["*"], - >>> ) - >>> keys = client.update_key(key_info) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> key_info = KeyUpdate( + key="abc123", + >>> indexes=["*"], + >>> ) + >>> keys = client.update_key(key_info) """ payload = _build_update_key_payload(key, self.json_handler) response = self._http_requests.patch(f"keys/{key.key}", payload) @@ -1879,12 +1897,12 @@ def multi_search( Examples >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.models.search import SearchParams - >>> client = Client("http://localhost.com", "masterKey") - >>> queries = [ - >>> SearchParams(index_uid="my_first_index", query"Some search"), - >>> SearchParams(index_uid="my_second_index", query="Another search") - >>> ] - >>> search_results = client.search(queries) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> queries = [ + >>> SearchParams(index_uid="my_first_index", query"Some search"), + >>> SearchParams(index_uid="my_second_index", query="Another search") + >>> ] + >>> search_results = client.search(queries) """ url = "multi-search" processed_queries = [] @@ -1937,8 +1955,8 @@ def get_raw_index(self, uid: str) -> IndexInfo | None: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.get_raw_index("movies") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.get_raw_index("movies") """ response = self.http_client.get(f"indexes/{uid}") @@ -1968,8 +1986,8 @@ def get_raw_indexes( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.get_raw_indexes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.get_raw_indexes() """ url = _build_offset_limit_url("indexes", offset, limit) response = self._http_requests.get(url) @@ -1991,8 +2009,8 @@ def get_version(self) -> Version: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> version = client.get_version() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> version = client.get_version() """ response = self._http_requests.get("version") @@ -2010,8 +2028,8 @@ def health(self) -> Health: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> health = client.get_health() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> health = client.get_health() """ response = self._http_requests.get("health") @@ -2034,8 +2052,8 @@ def swap_indexes(self, indexes: list[tuple[str, str]], rename: bool = False) -> Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.swap_indexes([("index_a", "index_b")]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.swap_indexes([("index_a", "index_b")]) """ if rename: processed_indexes = [{"indexes": x, "rename": True} for x in indexes] @@ -2118,8 +2136,8 @@ def cancel_tasks( >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.task import cancel_tasks >>> - >>> client = Client("http://localhost.com", "masterKey") - >>> client.cancel_tasks(uids=[1, 2]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.cancel_tasks(uids=[1, 2]) """ return _task.cancel_tasks( self.http_client, @@ -2170,8 +2188,8 @@ def delete_tasks( Examples >>> from meilisearch_python_sdk import Client >>> - >>> client = Client("http://localhost.com", "masterKey") - >>> client.delete_tasks(client, uids=[1, 2]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.delete_tasks(client, uids=[1, 2]) """ return _task.delete_tasks( self.http_client, @@ -2202,8 +2220,8 @@ def get_task(self, task_id: int) -> TaskResult: Examples >>> from meilisearch_python_sdk import Client >>> - >>> client = AsyncClient("http://localhost.com", "masterKey") - >>> get_task(client, 1244) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.get_task(client, 1244) """ return _task.get_task(self.http_client, task_id=task_id) @@ -2233,8 +2251,8 @@ def get_tasks( Examples >>> from meilisearch_python_sdk import Client >>> - >>> client = Client("http://localhost.com", "masterKey") - >>> client.get_tasks(client) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> client.get_tasks(client) """ return _task.get_tasks(self.http_client, index_ids=index_ids, types=types, reverse=reverse) @@ -2272,10 +2290,10 @@ def wait_for_task( >>> {"id": 1, "title": "Movie 1", "genre": "comedy"}, >>> {"id": 2, "title": "Movie 2", "genre": "drama"}, >>> ] - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> response = await index.add_documents(documents) - >>> client.wait_for_task(response.update_id) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> response = await index.add_documents(documents) + >>> client.wait_for_task(response.update_id) """ return _task.wait_for_task( self.http_client, @@ -2315,8 +2333,8 @@ def transfer_documents( # pragma: no cover Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index.transfer_documents("https://another-instance.com", api_key="otherMasterKey") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index.transfer_documents("https://another-instance.com", api_key="otherMasterKey") """ payload: JsonDict = {"url": url} diff --git a/meilisearch_python_sdk/index.py b/meilisearch_python_sdk/index.py index e6cab4a3..73f58d98 100644 --- a/meilisearch_python_sdk/index.py +++ b/meilisearch_python_sdk/index.py @@ -4952,9 +4952,9 @@ def compact(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.compact() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.compact() """ response = self._http_requests.post(f"{self._base_url_with_uid}/compact") return TaskInfo(**response.json()) @@ -4971,9 +4971,9 @@ def delete(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete() """ response = self._http_requests.delete(self._base_url_with_uid) return TaskInfo(**response.json()) @@ -4990,9 +4990,9 @@ def delete_if_exists(self) -> bool: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete_if_exists() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete_if_exists() """ response = self.delete() status = wait_for_task(self.http_client, response.task_uid, timeout_in_ms=100000) @@ -5016,9 +5016,9 @@ def update(self, primary_key: str) -> Self: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> updated_index = index.update() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> updated_index = index.update() """ payload = {"primaryKey": primary_key} response = self._http_requests.patch(self._base_url_with_uid, payload) @@ -5039,9 +5039,9 @@ def fetch_info(self) -> Self: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index_info = index.fetch_info() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index_info = index.fetch_info() """ response = self._http_requests.get(self._base_url_with_uid) index_dict = response.json() @@ -5062,9 +5062,9 @@ def get_primary_key(self) -> str | None: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> primary_key = index.get_primary_key() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> primary_key = index.get_primary_key() """ info = self.fetch_info() return info.primary_key @@ -5121,8 +5121,8 @@ def create( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = index.create(client, "movies") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = index.create(client, "movies") """ if not primary_key: payload = {"uid": uid} @@ -5166,9 +5166,9 @@ def get_stats(self) -> IndexStats: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> stats = index.get_stats() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> stats = index.get_stats() """ response = self._http_requests.get(self._stats_url) @@ -5282,9 +5282,9 @@ def search( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> search_results = index.search("Tron") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> search_results = index.search("Tron") """ if ranking_score_threshold: _validate_ranking_score_threshold(ranking_score_threshold) @@ -5453,13 +5453,13 @@ def facet_search( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> search_results = index.search( - >>> "Tron", - >>> facet_name="genre", - >>> facet_query="Sci-fi" - >>> ) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> search_results = index.search( + >>> "Tron", + >>> facet_name="genre", + >>> facet_query="Sci-fi" + >>> ) """ if ranking_score_threshold: _validate_ranking_score_threshold(ranking_score_threshold) @@ -5572,9 +5572,9 @@ def search_similar_documents( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> search_results = index.search_similar_documents("123") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> search_results = index.search_similar_documents("123") """ payload = { "id": id, @@ -5621,9 +5621,9 @@ def get_document( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> document = index.get_document("1234") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> document = index.get_document("1234") """ parameters: JsonDict = {} @@ -5671,9 +5671,9 @@ def get_documents( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> documents = index.get_documents() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> documents = index.get_documents() """ parameters: JsonDict = { "offset": offset, @@ -5732,9 +5732,9 @@ def add_documents( >>> {"id": 1, "title": "Movie 1", "genre": "comedy"}, >>> {"id": 2, "title": "Movie 2", "genre": "drama"}, >>> ] - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents(documents) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents(documents) """ if primary_key: url = _build_encoded_url(self._documents_url, {"primaryKey": primary_key}) @@ -5791,9 +5791,9 @@ def add_documents_in_batches( >>> {"id": 1, "title": "Movie 1", "genre": "comedy"}, >>> {"id": 2, "title": "Movie 2", "genre": "drama"}, >>> ] - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents_in_batches(documents) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents_in_batches(documents) """ return [ self.add_documents(x, primary_key, compress=compress) @@ -5838,9 +5838,9 @@ def add_documents_from_directory( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> directory_path = Path("/path/to/directory/containing/files") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents_from_directory(directory_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents_from_directory(directory_path) """ directory = Path(directory_path) if isinstance(directory_path, str) else directory_path @@ -5914,9 +5914,9 @@ def add_documents_from_directory_in_batches( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> directory_path = Path("/path/to/directory/containing/files") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents_from_directory_in_batches(directory_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents_from_directory_in_batches(directory_path) """ directory = Path(directory_path) if isinstance(directory_path, str) else directory_path @@ -5987,9 +5987,9 @@ def add_documents_from_file( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> file_path = Path("/path/to/file.json") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents_from_file(file_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents_from_file(file_path) """ documents = _load_documents_from_file(file_path, json_handler=self._json_handler) @@ -6029,9 +6029,9 @@ def add_documents_from_file_in_batches( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> file_path = Path("/path/to/file.json") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents_from_file_in_batches(file_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents_from_file_in_batches(file_path) """ documents = _load_documents_from_file( file_path, csv_delimiter, json_handler=self._json_handler @@ -6080,9 +6080,9 @@ def add_documents_from_raw_file( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> file_path = Path("/path/to/file.csv") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.add_documents_from_raw_file(file_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.add_documents_from_raw_file(file_path) """ upload_path = Path(file_path) if isinstance(file_path, str) else file_path if not upload_path.exists(): @@ -6148,9 +6148,9 @@ def edit_documents( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.edit_documents("doc.title = `${doc.title.to_upper()}`") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.edit_documents("doc.title = `${doc.title.to_upper()}`") """ url = f"{self._documents_url}/edit" payload: JsonDict = {"function": function} @@ -6193,9 +6193,9 @@ def update_documents( >>> {"id": 1, "title": "Movie 1", "genre": "comedy"}, >>> {"id": 2, "title": "Movie 2", "genre": "drama"}, >>> ] - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_documents(documents) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_documents(documents) """ if primary_key: url = _build_encoded_url(self._documents_url, {"primaryKey": primary_key}) @@ -6256,9 +6256,9 @@ def update_documents_in_batches( >>> {"id": 1, "title": "Movie 1", "genre": "comedy"}, >>> {"id": 2, "title": "Movie 2", "genre": "drama"}, >>> ] - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_documents_in_batches(documents) + >>> with Client("http://localhost.com", "masterKey") client: + >>> index = client.index("movies") + >>> index.update_documents_in_batches(documents) """ return [ self.update_documents(x, primary_key, compress=compress) @@ -6303,9 +6303,9 @@ def update_documents_from_directory( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> directory_path = Path("/path/to/directory/containing/files") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_documents_from_directory(directory_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_documents_from_directory(directory_path) """ directory = Path(directory_path) if isinstance(directory_path, str) else directory_path @@ -6378,9 +6378,9 @@ def update_documents_from_directory_in_batches( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> directory_path = Path("/path/to/directory/containing/files") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_documents_from_directory_in_batches(directory_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_documents_from_directory_in_batches(directory_path) """ directory = Path(directory_path) if isinstance(directory_path, str) else directory_path @@ -6453,9 +6453,9 @@ def update_documents_from_file( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> file_path = Path("/path/to/file.json") - >>> client = Client("http://localhost.com", "masterKey") as client: - >>> index = client.index("movies") - >>> index.update_documents_from_file(file_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_documents_from_file(file_path) """ documents = _load_documents_from_file( file_path, csv_delimiter, json_handler=self._json_handler @@ -6492,9 +6492,9 @@ def update_documents_from_file_in_batches( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> file_path = Path("/path/to/file.json") - >>> client = Client("http://localhost.com", "masterKey") as client: - >>> index = client.index("movies") - >>> index.update_documents_from_file_in_batches(file_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_documents_from_file_in_batches(file_path) """ documents = _load_documents_from_file(file_path, json_handler=self._json_handler) @@ -6541,9 +6541,9 @@ def update_documents_from_raw_file( >>> from pathlib import Path >>> from meilisearch_python_sdk import Client >>> file_path = Path("/path/to/file.csv") - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_documents_from_raw_file(file_path) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_documents_from_raw_file(file_path) """ upload_path = Path(file_path) if isinstance(file_path, str) else file_path if not upload_path.exists(): @@ -6600,9 +6600,9 @@ def delete_document(self, document_id: str) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete_document("1234") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete_document("1234") """ if self._pre_delete_document_plugins: Index._run_plugins( @@ -6633,9 +6633,9 @@ def delete_documents(self, ids: list[str]) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete_documents(["1234", "5678"]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete_documents(["1234", "5678"]) """ if self._pre_delete_documents_plugins: Index._run_plugins(self._pre_delete_documents_plugins, Event.PRE, ids=ids) @@ -6666,9 +6666,9 @@ def delete_documents_by_filter(self, filter: Filter) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete_documents_by_filter("genre=horor")) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete_documents_by_filter("genre=horor")) """ if self._pre_delete_documents_by_filter_plugins: Index._run_plugins( @@ -6705,14 +6705,14 @@ def delete_documents_in_batches_by_filter( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete_documents_in_batches_by_filter( - >>> [ - >>> "genre=horor"), - >>> "release_date=1520035200"), - >>> ] - >>> ) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete_documents_in_batches_by_filter( + >>> [ + >>> "genre=horor"), + >>> "release_date=1520035200"), + >>> ] + >>> ) """ return [self.delete_documents_by_filter(filter) for filter in filters] @@ -6728,9 +6728,9 @@ def delete_all_documents(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.delete_all_document() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.delete_all_document() """ if self._pre_delete_all_documents_plugins: Index._run_plugins(self._pre_delete_all_documents_plugins, Event.PRE) @@ -6758,9 +6758,9 @@ def get_settings(self) -> MeilisearchSettings: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> settings = index.get_settings() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> settings = index.get_settings() """ response = self._http_requests.get(self._settings_url) response_json = response.json() @@ -6810,9 +6810,9 @@ def update_settings(self, body: MeilisearchSettings, *, compress: bool = False) >>> displayed_attributes=["title", "description", "genre", "release_date"], >>> sortable_attributes=["title", "release_date"], >>> ) - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_settings(new_settings) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_settings(new_settings) """ body_dict = { k: v @@ -6835,9 +6835,9 @@ def reset_settings(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_settings() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_settings() """ response = self._http_requests.delete(self._settings_url) @@ -6855,9 +6855,9 @@ def get_ranking_rules(self) -> list[str]: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> ranking_rules = index.get_ranking_rules() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> ranking_rules = index.get_ranking_rules() """ response = self._http_requests.get(f"{self._settings_url}/ranking-rules") @@ -6889,9 +6889,9 @@ def update_ranking_rules(self, ranking_rules: list[str], *, compress: bool = Fal >>> "release_date:desc", >>> "rank:desc", >>> ], - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_ranking_rules(ranking_rules) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_ranking_rules(ranking_rules) """ response = self._http_requests.put( f"{self._settings_url}/ranking-rules", ranking_rules, compress=compress @@ -6911,9 +6911,9 @@ def reset_ranking_rules(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_ranking_rules() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_ranking_rules() """ response = self._http_requests.delete(f"{self._settings_url}/ranking-rules") @@ -6932,9 +6932,9 @@ def get_distinct_attribute(self) -> str | None: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> distinct_attribute = index.get_distinct_attribute() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> distinct_attribute = index.get_distinct_attribute() """ response = self._http_requests.get(f"{self._settings_url}/distinct-attribute") @@ -6959,9 +6959,9 @@ def update_distinct_attribute(self, body: str, *, compress: bool = False) -> Tas Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_distinct_attribute("url") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_distinct_attribute("url") """ response = self._http_requests.put( f"{self._settings_url}/distinct-attribute", body, compress=compress @@ -6981,9 +6981,9 @@ def reset_distinct_attribute(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_distinct_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_distinct_attributes() """ response = self._http_requests.delete(f"{self._settings_url}/distinct-attribute") @@ -7001,9 +7001,9 @@ def get_searchable_attributes(self) -> list[str]: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> searchable_attributes = index.get_searchable_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> searchable_attributes = index.get_searchable_attributes() """ response = self._http_requests.get(f"{self._settings_url}/searchable-attributes") @@ -7025,9 +7025,9 @@ def update_searchable_attributes(self, body: list[str], *, compress: bool = Fals Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_searchable_attributes(["title", "description", "genre"]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_searchable_attributes(["title", "description", "genre"]) """ response = self._http_requests.put( f"{self._settings_url}/searchable-attributes", body, compress=compress @@ -7047,9 +7047,9 @@ def reset_searchable_attributes(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_searchable_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_searchable_attributes() """ response = self._http_requests.delete(f"{self._settings_url}/searchable-attributes") @@ -7067,9 +7067,9 @@ def get_displayed_attributes(self) -> list[str]: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> displayed_attributes = index.get_displayed_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> displayed_attributes = index.get_displayed_attributes() """ response = self._http_requests.get(f"{self._settings_url}/displayed-attributes") @@ -7091,11 +7091,11 @@ def update_displayed_attributes(self, body: list[str], *, compress: bool = False Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_displayed_attributes( - >>> ["title", "description", "genre", "release_date"] - >>> ) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_displayed_attributes( + >>> ["title", "description", "genre", "release_date"] + >>> ) """ response = self._http_requests.put( f"{self._settings_url}/displayed-attributes", body, compress=compress @@ -7115,9 +7115,9 @@ def reset_displayed_attributes(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_displayed_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_displayed_attributes() """ response = self._http_requests.delete(f"{self._settings_url}/displayed-attributes") @@ -7135,9 +7135,9 @@ def get_stop_words(self) -> list[str] | None: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> stop_words = index.get_stop_words() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> stop_words = index.get_stop_words() """ response = self._http_requests.get(f"{self._settings_url}/stop-words") @@ -7162,9 +7162,9 @@ def update_stop_words(self, body: list[str], *, compress: bool = False) -> TaskI Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_stop_words(["the", "a", "an"]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_stop_words(["the", "a", "an"]) """ response = self._http_requests.put( f"{self._settings_url}/stop-words", body, compress=compress @@ -7184,9 +7184,9 @@ def reset_stop_words(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_stop_words() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_stop_words() """ response = self._http_requests.delete(f"{self._settings_url}/stop-words") @@ -7204,9 +7204,9 @@ def get_synonyms(self) -> dict[str, list[str]] | None: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> synonyms = index.get_synonyms() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> synonyms = index.get_synonyms() """ response = self._http_requests.get(f"{self._settings_url}/synonyms") @@ -7230,11 +7230,11 @@ def update_synonyms(self, body: dict[str, list[str]], *, compress: bool = False) Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") as client: - >>> index = client.index("movies") - >>> index.update_synonyms( - >>> {"wolverine": ["xmen", "logan"], "logan": ["wolverine"]} - >>> ) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_synonyms( + >>> {"wolverine": ["xmen", "logan"], "logan": ["wolverine"]} + >>> ) """ response = self._http_requests.put( f"{self._settings_url}/synonyms", body, compress=compress @@ -7254,9 +7254,9 @@ def reset_synonyms(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_synonyms() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_synonyms() """ response = self._http_requests.delete(f"{self._settings_url}/synonyms") @@ -7274,9 +7274,9 @@ def get_filterable_attributes(self) -> list[str | FilterableAttributes] | None: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> filterable_attributes = index.get_filterable_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> filterable_attributes = index.get_filterable_attributes() """ response = self._http_requests.get(f"{self._settings_url}/filterable-attributes") @@ -7317,9 +7317,9 @@ def update_filterable_attributes( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_filterable_attributes(["genre", "director"]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_filterable_attributes(["genre", "director"]) """ payload: list[str | JsonDict] = [] @@ -7347,9 +7347,9 @@ def reset_filterable_attributes(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_filterable_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_filterable_attributes() """ response = self._http_requests.delete(f"{self._settings_url}/filterable-attributes") @@ -7367,9 +7367,9 @@ def get_sortable_attributes(self) -> list[str]: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> sortable_attributes = index.get_sortable_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> sortable_attributes = index.get_sortable_attributes() """ response = self._http_requests.get(f"{self._settings_url}/sortable-attributes") @@ -7393,9 +7393,9 @@ def update_sortable_attributes( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_sortable_attributes(["title", "release_date"]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_sortable_attributes(["title", "release_date"]) """ response = self._http_requests.put( f"{self._settings_url}/sortable-attributes", sortable_attributes, compress=compress @@ -7415,9 +7415,9 @@ def reset_sortable_attributes(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_sortable_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_sortable_attributes() """ response = self._http_requests.delete(f"{self._settings_url}/sortable-attributes") @@ -7435,9 +7435,9 @@ def get_typo_tolerance(self) -> TypoTolerance: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> sortable_attributes = index.get_typo_tolerance() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> sortable_attributes = index.get_typo_tolerance() """ response = self._http_requests.get(f"{self._settings_url}/typo-tolerance") @@ -7461,10 +7461,10 @@ def update_typo_tolerance( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> TypoTolerance(enabled=False) - >>> index.update_typo_tolerance() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> TypoTolerance(enabled=False) + >>> index.update_typo_tolerance() """ response = self._http_requests.patch( f"{self._settings_url}/typo-tolerance", @@ -7486,9 +7486,9 @@ def reset_typo_tolerance(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_typo_tolerance() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_typo_tolerance() """ response = self._http_requests.delete(f"{self._settings_url}/typo-tolerance") @@ -7506,9 +7506,9 @@ def get_faceting(self) -> Faceting: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> faceting = index.get_faceting() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> faceting = index.get_faceting() """ response = self._http_requests.get(f"{self._settings_url}/faceting") @@ -7530,9 +7530,9 @@ def update_faceting(self, faceting: Faceting, *, compress: bool = False) -> Task Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_faceting(faceting=Faceting(max_values_per_facet=100)) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_faceting(faceting=Faceting(max_values_per_facet=100)) """ response = self._http_requests.patch( f"{self._settings_url}/faceting", @@ -7554,9 +7554,9 @@ def reset_faceting(self) -> TaskInfo: Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_faceting() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_faceting() """ response = self._http_requests.delete(f"{self._settings_url}/faceting") @@ -7574,9 +7574,9 @@ def get_pagination(self) -> Pagination: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> pagination_settings = index.get_pagination() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> pagination_settings = index.get_pagination() """ response = self._http_requests.get(f"{self._settings_url}/pagination") @@ -7599,9 +7599,9 @@ def update_pagination(self, settings: Pagination, *, compress: bool = False) -> Examples >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.models.settings import Pagination - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_pagination(settings=Pagination(max_total_hits=123)) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_pagination(settings=Pagination(max_total_hits=123)) """ response = self._http_requests.patch( f"{self._settings_url}/pagination", @@ -7623,9 +7623,9 @@ def reset_pagination(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_pagination() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_pagination() """ response = self._http_requests.delete(f"{self._settings_url}/pagination") @@ -7643,9 +7643,9 @@ def get_separator_tokens(self) -> list[str]: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> separator_token_settings = index.get_separator_tokens() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> separator_token_settings = index.get_separator_tokens() """ response = self._http_requests.get(f"{self._settings_url}/separator-tokens") @@ -7669,9 +7669,9 @@ def update_separator_tokens( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_separator_tokens(separator_tokenes=["|", "/") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_separator_tokens(separator_tokenes=["|", "/") """ response = self._http_requests.put( f"{self._settings_url}/separator-tokens", separator_tokens, compress=compress @@ -7691,9 +7691,9 @@ def reset_separator_tokens(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_separator_tokens() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_separator_tokens() """ response = self._http_requests.delete(f"{self._settings_url}/separator-tokens") @@ -7711,9 +7711,9 @@ def get_non_separator_tokens(self) -> list[str]: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> non_separator_token_settings = index.get_non_separator_tokens() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> non_separator_token_settings = index.get_non_separator_tokens() """ response = self._http_requests.get(f"{self._settings_url}/non-separator-tokens") @@ -7737,9 +7737,9 @@ def update_non_separator_tokens( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_non_separator_tokens(non_separator_tokens=["@", "#") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_non_separator_tokens(non_separator_tokens=["@", "#") """ response = self._http_requests.put( f"{self._settings_url}/non-separator-tokens", non_separator_tokens, compress=compress @@ -7759,9 +7759,9 @@ def reset_non_separator_tokens(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_non_separator_tokens() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_non_separator_tokens() """ response = self._http_requests.delete(f"{self._settings_url}/non-separator-tokens") @@ -7779,9 +7779,9 @@ def get_search_cutoff_ms(self) -> int | None: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> search_cutoff_ms_settings = index.get_search_cutoff_ms() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> search_cutoff_ms_settings = index.get_search_cutoff_ms() """ response = self._http_requests.get(f"{self._settings_url}/search-cutoff-ms") @@ -7803,9 +7803,9 @@ def update_search_cutoff_ms(self, search_cutoff_ms: int, *, compress: bool = Fal Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_search_cutoff_ms(100) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_search_cutoff_ms(100) """ response = self._http_requests.put( f"{self._settings_url}/search-cutoff-ms", search_cutoff_ms, compress=compress @@ -7825,9 +7825,9 @@ def reset_search_cutoff_ms(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_search_cutoff_ms() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_search_cutoff_ms() """ response = self._http_requests.delete(f"{self._settings_url}/search-cutoff-ms") @@ -7845,9 +7845,9 @@ def get_word_dictionary(self) -> list[str]: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> word_dictionary = index.get_word_dictionary() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> word_dictionary = index.get_word_dictionary() """ response = self._http_requests.get(f"{self._settings_url}/dictionary") @@ -7869,9 +7869,9 @@ def update_word_dictionary(self, dictionary: list[str], *, compress: bool = Fals Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_word_dictionary(dictionary=["S.O.S", "S.O") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_word_dictionary(dictionary=["S.O.S", "S.O") """ response = self._http_requests.put( f"{self._settings_url}/dictionary", dictionary, compress=compress @@ -7891,9 +7891,9 @@ def reset_word_dictionary(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_word_dictionary() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_word_dictionary() """ response = self._http_requests.delete(f"{self._settings_url}/dictionary") @@ -7911,9 +7911,9 @@ def get_proximity_precision(self) -> ProximityPrecision: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> proximity_precision = index.get_proximity_precision() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> proximity_precision = index.get_proximity_precision() """ response = self._http_requests.get(f"{self._settings_url}/proximity-precision") @@ -7938,9 +7938,9 @@ def update_proximity_precision( Examples >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.models.settings import ProximityPrecision - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_proximity_precision(ProximityPrecision.BY_ATTRIBUTE) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_proximity_precision(ProximityPrecision.BY_ATTRIBUTE) """ response = self._http_requests.put( f"{self._settings_url}/proximity-precision", @@ -7962,9 +7962,9 @@ def reset_proximity_precision(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_proximity_precision() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_proximity_precision() """ response = self._http_requests.delete(f"{self._settings_url}/proximity-precision") @@ -7982,9 +7982,9 @@ def get_embedders(self) -> Embedders | None: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> embedders = await index.get_embedders() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> embedders = await index.get_embedders() """ response = self._http_requests.get(f"{self._settings_url}/embedders") @@ -8007,11 +8007,11 @@ def update_embedders(self, embedders: Embedders, *, compress: bool = False) -> T Examples >>> from meilisearch_python_sdk import Client >>> from meilisearch_python_sdk.models.settings import Embedders, UserProvidedEmbedder - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_embedders( - >>> Embedders(embedders={dimensions=512)}) - >>> ) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_embedders( + >>> Embedders(embedders={dimensions=512)}) + >>> ) """ payload = {} for key, embedder in embedders.embedders.items(): @@ -8040,9 +8040,9 @@ def reset_embedders(self) -> TaskInfo: # pragma: no cover Examples >>> from meilisearch_async_client import Client - >>> client = AsyncClient("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_embedders() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_embedders() """ response = self._http_requests.delete(f"{self._settings_url}/embedders") @@ -8060,9 +8060,9 @@ def get_localized_attributes(self) -> list[LocalizedAttributes] | None: Examples >>> from meilisearch_async_client import AsyncClient - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> localized_attributes = await index.get_localized_attributes() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> localized_attributes = await index.get_localized_attributes() """ response = self._http_requests.get(f"{self._settings_url}/localized-attributes") @@ -8092,12 +8092,12 @@ def update_localized_attributes( >>> from meilisearch_python_sdk.models.settings import LocalizedAttributes >>> >>> - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_localized_attributes([ - >>> LocalizedAttributes(locales=["eng", "spa"], attribute_patterns=["*"]), - >>> LocalizedAttributes(locales=["ita"], attribute_patterns=["*_it"]), - >>> ]) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_localized_attributes([ + >>> LocalizedAttributes(locales=["eng", "spa"], attribute_patterns=["*"]), + >>> LocalizedAttributes(locales=["ita"], attribute_patterns=["*_it"]), + >>> ]) """ payload = [x.model_dump(by_alias=True) for x in localized_attributes] response = self._http_requests.put( @@ -8138,9 +8138,9 @@ def get_facet_search(self) -> bool: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> facet_search = await index.get_facet_search() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> facet_search = await index.get_facet_search() """ response = self._http_requests.get(f"{self._settings_url}/facet-search") @@ -8162,9 +8162,9 @@ def update_facet_search(self, facet_search: bool, *, compress: bool = False) -> Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_facet_search(True) + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_facet_search(True) """ response = self._http_requests.put( f"{self._settings_url}/facet-search", @@ -8186,9 +8186,9 @@ def reset_facet_search(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> await index.reset_facet_search() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> await index.reset_facet_search() """ response = self._http_requests.delete(f"{self._settings_url}/facet-search") @@ -8206,9 +8206,9 @@ def get_prefix_search(self) -> bool: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> prefix_search = index.get_prefix_search() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> prefix_search = index.get_prefix_search() """ response = self._http_requests.get(f"{self._settings_url}/prefix-search") @@ -8235,9 +8235,9 @@ def update_prefix_search( Examples >>> from meilisearch_python_sdk import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.update_prefix_search("disabled") + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.update_prefix_search("disabled") """ response = self._http_requests.put( f"{self._settings_url}/prefix-search", @@ -8259,9 +8259,9 @@ def reset_prefix_search(self) -> TaskInfo: Examples >>> from meilisearch_async_client import Client - >>> client = Client("http://localhost.com", "masterKey") - >>> index = client.index("movies") - >>> index.reset_prefix_search() + >>> with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> index.reset_prefix_search() """ response = self._http_requests.delete(f"{self._settings_url}/prefix-search") diff --git a/tests/conftest.py b/tests/conftest.py index b014e130..0f043cfd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -87,7 +87,8 @@ async def async_client_with_plugins(base_url, ssl_verify): @pytest.fixture(scope="session") def client(base_url, ssl_verify): - yield Client(base_url, MASTER_KEY, verify=ssl_verify) + with Client(base_url, MASTER_KEY, verify=ssl_verify) as client: + yield client @pytest.fixture(scope="session")