From 5b1546b1bd7bf0243124eaef8c5962d02ae8dcbc Mon Sep 17 00:00:00 2001 From: Sebastian Roth Date: Thu, 18 Jul 2024 14:57:11 +0100 Subject: [PATCH] add version param support for REST delete calls --- CONTRIBUTING.md | 16 ++++++++++++++++ snyk/client.py | 22 +++++++++++++++++++--- snyk/test_client.py | 12 ++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c92a94d --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,16 @@ +# Getting started + +This project uses Poetry. Install the dependencies using: + +``` +poetry install +``` + +## Running the tests + +To run the test suite, use the following command: + + +``` +poetry run pytest +``` \ No newline at end of file diff --git a/snyk/client.py b/snyk/client.py index 3b3a3d3..84211b2 100644 --- a/snyk/client.py +++ b/snyk/client.py @@ -196,14 +196,30 @@ def get( return resp - def delete(self, path: str) -> requests.Response: - url = f"{self.api_url}/{path}" + def delete( + self, + path: str, + version: str = None, + ) -> requests.Response: + if version: + url = f"{self.rest_api_url}/{path}?version={version}" + else: + url = f"{self.api_url}/{path}" + logger.debug(f"DELETE: {url}") + if version or self.version: + params = {} + params["version"] = version or self.version + + fkwargs = {"headers": self.api_headers, "params": params} + else: + fkwargs = {"headers": self.api_headers} + resp = retry_call( self.request, fargs=[requests.delete, url], - fkwargs={"headers": self.api_headers}, + fkwargs=fkwargs, tries=self.tries, delay=self.delay, backoff=self.backoff, diff --git a/snyk/test_client.py b/snyk/test_client.py index ec0bb93..0a93bf9 100644 --- a/snyk/test_client.py +++ b/snyk/test_client.py @@ -12,6 +12,7 @@ TEST_DATA = os.path.join(os.path.dirname(__file__), "test_data") REST_ORG = "39ddc762-b1b9-41ce-ab42-defbe4575bd6" +REST_PROJECT = "1ae27847-83d0-40f8-be5c-93438336cc3e" REST_URL = "https://api.snyk.io/rest" REST_VERSION = "2022-02-16~experimental" @@ -297,6 +298,17 @@ def test_rest_get(self, requests_mock, rest_client, rest_targets_page1): assert len(targets["data"]) == 10 + def test_rest_delete(self, requests_mock, rest_client, rest_targets_page1): + requests_mock.delete( + f"{REST_URL}/orgs/{REST_ORG}/projects/{REST_PROJECT}?version={REST_VERSION}", + status_code=204, + json={}, + ) + + rest_client.delete(f"orgs/{REST_ORG}/projects/{REST_PROJECT}") + + assert requests_mock.call_count == 1 + def test_get_rest_pages( self, requests_mock,