From 3e7cfc077f7122e0d417645f92aecf56cbd8cef0 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Wed, 31 Jul 2024 16:20:23 -0700 Subject: [PATCH] Add support for deployments.delete endpoint Signed-off-by: Mattt Zmuda --- replicate/deployment.py | 26 ++++++++++++++++++++++++++ tests/test_deployment.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/replicate/deployment.py b/replicate/deployment.py index 6b2aa2fb..d8359a33 100644 --- a/replicate/deployment.py +++ b/replicate/deployment.py @@ -356,6 +356,32 @@ async def async_update( return _json_to_deployment(self._client, resp.json()) + def delete(self, deployment_owner: str, deployment_name: str) -> None: + """ + Delete an existing deployment. + + Args: + deployment_owner: The owner of the deployment. + deployment_name: The name of the deployment. + """ + self._client._request( + "DELETE", + f"/v1/deployments/{deployment_owner}/{deployment_name}", + ) + + async def async_delete(self, deployment_owner: str, deployment_name: str) -> None: + """ + Delete an existing deployment asynchronously. + + Args: + deployment_owner: The owner of the deployment. + deployment_name: The name of the deployment. + """ + await self._client._async_request( + "DELETE", + f"/v1/deployments/{deployment_owner}/{deployment_name}", + ) + @property def predictions(self) -> "DeploymentsPredictions": """ diff --git a/tests/test_deployment.py b/tests/test_deployment.py index d711d740..3270cff4 100644 --- a/tests/test_deployment.py +++ b/tests/test_deployment.py @@ -178,6 +178,11 @@ ) ) +router.route( + method="DELETE", + path="/deployments/acme/image-upscaler", + name="deployments.delete", +).mock(return_value=httpx.Response(204)) router.route(host="api.replicate.com").pass_through() @@ -399,3 +404,28 @@ async def test_update_deployment(async_flag): assert updated_deployment.current_release.configuration.hardware == "gpu-v100" assert updated_deployment.current_release.configuration.min_instances == 2 assert updated_deployment.current_release.configuration.max_instances == 10 + + +@respx.mock +@pytest.mark.asyncio +@pytest.mark.parametrize("async_flag", [True, False]) +async def test_delete_deployment(async_flag): + client = Client( + api_token="test-token", transport=httpx.MockTransport(router.handler) + ) + + if async_flag: + await client.deployments.async_delete( + deployment_owner="acme", deployment_name="image-upscaler" + ) + else: + client.deployments.delete( + deployment_owner="acme", deployment_name="image-upscaler" + ) + + assert router["deployments.delete"].called + assert router["deployments.delete"].calls[0].request.method == "DELETE" + assert ( + router["deployments.delete"].calls[0].request.url + == "https://api.replicate.com/v1/deployments/acme/image-upscaler" + )