Skip to content

Commit

Permalink
Merge pull request #334 from sanders41/tasks-client
Browse files Browse the repository at this point in the history
Add option to use Client in task functions
  • Loading branch information
sanders41 committed Nov 22, 2022
2 parents 4e4c08f + 52c9363 commit 1c03458
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
16 changes: 10 additions & 6 deletions meilisearch_python_async/task.py
Expand Up @@ -16,7 +16,7 @@


async def cancel_tasks(
http_client: AsyncClient,
client: AsyncClient | Client,
*,
uids: list[str] | None = None,
index_uids: list[str] | None = None,
Expand All @@ -33,6 +33,7 @@ async def cancel_tasks(
Args:
client: An httpx AsyncClient or meilisearch_python_async Client instance.
uids: A list of task UIDs to cancel.
index_uids: A list of index UIDs for which to cancel tasks.
statuses: A list of statuses to cancel.
Expand All @@ -58,7 +59,7 @@ async def cancel_tasks(
>>> from meilisearch_python_async.task import cancel_tasks
>>>
>>> async with Client("http://localhost.com", "masterKey") as client:
>>> await cancel_tasks(client.http_client, uids=[1, 2])
>>> await cancel_tasks(client, uids=[1, 2])
"""
parameters = {}
if uids:
Expand All @@ -83,13 +84,14 @@ async def cancel_tasks(
parameters["statuses"] = "enqueued,processing"

url = f"tasks/cancel?{urlencode(parameters)}"
response = await http_client.post(url)
client_ = _get_client(client)
response = await client_.post(url)

return TaskInfo(**response.json())


async def delete_tasks(
http_client: AsyncClient,
client: AsyncClient | Client,
*,
uids: list[str] | None = None,
index_uids: list[str] | None = None,
Expand All @@ -106,6 +108,7 @@ async def delete_tasks(
Args:
client: An httpx AsyncClient or meilisearch_python_async Client instance.
uids: A list of task UIDs to cancel.
index_uids: A list of index UIDs for which to cancel tasks.
statuses: A list of statuses to cancel.
Expand All @@ -131,7 +134,7 @@ async def delete_tasks(
>>> from meilisearch_python_async.task import delete_tasks
>>>
>>> async with Client("http://localhost.com", "masterKey") as client:
>>> await delete_tasks(client.http_client, uids=[1, 2])
>>> await delete_tasks(client, uids=[1, 2])
"""
parameters = {}
if uids:
Expand All @@ -156,7 +159,8 @@ async def delete_tasks(
parameters["statuses"] = "canceled,enqueued,failed,processing,succeeded"

url = f"tasks?{urlencode(parameters)}"
response = await http_client.delete(url)
client_ = _get_client(client)
response = await client_.delete(url)

return TaskInfo(**response.json())

Expand Down
48 changes: 24 additions & 24 deletions tests/test_task.py
Expand Up @@ -22,10 +22,10 @@ async def create_tasks(empty_index, small_movies):

@pytest.mark.usefixtures("create_tasks")
async def test_cancel_statuses(test_client):
task = await cancel_tasks(test_client.http_client, statuses=["enqueued", "processing"])
await wait_for_task(test_client.http_client, task.task_uid)
completed_task = await get_task(test_client.http_client, task.task_uid)
tasks = await get_tasks(test_client.http_client, types="taskCancelation")
task = await cancel_tasks(test_client, statuses=["enqueued", "processing"])
await wait_for_task(test_client, task.task_uid)
completed_task = await get_task(test_client, task.task_uid)
tasks = await get_tasks(test_client, types="taskCancelation")

assert completed_task.index_uids is None
assert completed_task.status == "succeeded"
Expand All @@ -36,10 +36,10 @@ async def test_cancel_statuses(test_client):

@pytest.mark.usefixtures("create_tasks")
async def test_cancel_tasks(test_client):
task = await cancel_tasks(test_client.http_client, uids=["1", "2"])
await wait_for_task(test_client.http_client, task.task_uid)
completed_task = await get_task(test_client.http_client, task.task_uid)
tasks = await get_tasks(test_client.http_client, types="taskCancelation")
task = await cancel_tasks(test_client, uids=["1", "2"])
await wait_for_task(test_client, task.task_uid)
completed_task = await get_task(test_client, task.task_uid)
tasks = await get_tasks(test_client, types="taskCancelation")

assert completed_task.status == "succeeded"
assert completed_task.task_type == "taskCancelation"
Expand All @@ -49,10 +49,10 @@ async def test_cancel_tasks(test_client):

@pytest.mark.usefixtures("create_tasks")
async def test_cancel_task_no_params(test_client):
task = await cancel_tasks(test_client.http_client)
await wait_for_task(test_client.http_client, task.task_uid)
completed_task = await get_task(test_client.http_client, task.task_uid)
tasks = await get_tasks(test_client.http_client, types="taskCancelation")
task = await cancel_tasks(test_client)
await wait_for_task(test_client, task.task_uid)
completed_task = await get_task(test_client, task.task_uid)
tasks = await get_tasks(test_client, types="taskCancelation")

assert completed_task.status == "succeeded"
assert completed_task.task_type == "taskCancelation"
Expand All @@ -62,10 +62,10 @@ async def test_cancel_task_no_params(test_client):

@pytest.mark.usefixtures("create_tasks")
async def test_delete_statuses(test_client):
task = await delete_tasks(test_client.http_client, statuses=["enqueued", "processing"])
await wait_for_task(test_client.http_client, task.task_uid)
deleted_tasks = await get_task(test_client.http_client, task.task_uid)
tasks = await get_tasks(test_client.http_client, types="taskDeletion")
task = await delete_tasks(test_client, statuses=["enqueued", "processing"])
await wait_for_task(test_client, task.task_uid)
deleted_tasks = await get_task(test_client, task.task_uid)
tasks = await get_tasks(test_client, types="taskDeletion")

assert deleted_tasks.status == "succeeded"
assert deleted_tasks.task_type == "taskDeletion"
Expand All @@ -75,10 +75,10 @@ async def test_delete_statuses(test_client):

@pytest.mark.usefixtures("create_tasks")
async def test_delete_tasks(test_client):
task = await delete_tasks(test_client.http_client, uids=["1", "2"])
await wait_for_task(test_client.http_client, task.task_uid)
completed_task = await get_task(test_client.http_client, task.task_uid)
tasks = await get_tasks(test_client.http_client, types="taskDeletion")
task = await delete_tasks(test_client, uids=["1", "2"])
await wait_for_task(test_client, task.task_uid)
completed_task = await get_task(test_client, task.task_uid)
tasks = await get_tasks(test_client, types="taskDeletion")

assert completed_task.status == "succeeded"
assert completed_task.task_type == "taskDeletion"
Expand All @@ -88,10 +88,10 @@ async def test_delete_tasks(test_client):

@pytest.mark.usefixtures("create_tasks")
async def test_delete_no_params(test_client):
task = await delete_tasks(test_client.http_client)
await wait_for_task(test_client.http_client, task.task_uid)
deleted_tasks = await get_task(test_client.http_client, task.task_uid)
tasks = await get_tasks(test_client.http_client, types="taskDeletion")
task = await delete_tasks(test_client)
await wait_for_task(test_client, task.task_uid)
deleted_tasks = await get_task(test_client, task.task_uid)
tasks = await get_tasks(test_client, types="taskDeletion")

assert deleted_tasks.status == "succeeded"
assert deleted_tasks.task_type == "taskDeletion"
Expand Down

0 comments on commit 1c03458

Please sign in to comment.