From 199b0d9e65831d7688e3cbfa5dbed97093720b78 Mon Sep 17 00:00:00 2001 From: David Karchmer Date: Mon, 16 May 2022 17:09:53 -0700 Subject: [PATCH] Allow delete() to accept payload (#9) * Allow delete() to accept payload And fix broken tests. Fixes #1 * Ensure change is backwards compatible --- CHANGELOG.md | 6 +++++- README.md | 3 ++- drf_client/connection.py | 18 +++++++++++++----- setup.py | 2 +- tests/api.py | 10 +++++++++- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6f4e24..e0b5fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ +### v0.5.0 (2022-05-16) + + * Allow `delete()` method to accept optional `payload` + ### v0.4.1 (2022-03-13) * Fix BaseMain Login method @@ -18,7 +22,7 @@ ### v0.1.2 (2020-06-01) * Remove dependency on unitest2 - + ### v0.1.0 (2017-05-06) * First release diff --git a/README.md b/README.md index 2732087..6d23af1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Django Rest Framework Python API Package [![Build Status](https://travis-ci.org/dkarchmer/django-rest-framework-client.svg?branch=master)](https://travis-ci.org/dkarchmer/django-rest-framework-client) -[![PyPI version](https://img.shields.io/pypi/v/django-rest-framework-client.svg)](https://pypi.python.org/pypi/django-rest-framework-client) +[![PyPI version](https://img.shields.io/pypi/v/django-rest-framework-client.svg)](https://pypi.python.org/pypi/django-rest-framework-client) A python library for interacting with any Django web server base on django-rest-framework @@ -187,6 +187,7 @@ py.test ## CI Deployment 1. Update `setup.py` with new version +2. Update `CHANGELOG.md` with description of new version 2. Create new tag with same version ``` diff --git a/drf_client/connection.py b/drf_client/connection.py index b02aff1..7c09519 100644 --- a/drf_client/connection.py +++ b/drf_client/connection.py @@ -191,8 +191,13 @@ def put(self, data=None, **kwargs): resp = requests.put(self.url(), data=payload, headers=self._get_header()) return self._process_response(resp) - def delete(self, **kwargs): - resp = requests.delete(self.url(), headers=self._get_header()) + def delete(self, data=None, **kwargs): + if data: + payload = json.dumps(data) + else: + payload = None + + resp = requests.delete(self.url(), data=payload, headers=self._get_header()) if 200 <= resp.status_code <= 299: if resp.status_code == 204: return True @@ -235,14 +240,17 @@ def login(self, password, username=None): # Default to 'username' which is the default DRF behavior username_key = self.options.get('USERNAME_KEY', 'username') data = {'password': password} - data[username_key] = username + data[username_key] = username url = '{0}/{1}'.format(self.base_url, self.options['LOGIN']) payload = json.dumps(data) r = requests.post(url, data=payload, headers=DEFAULT_HEADERS) if r.status_code in [200, 201]: content = json.loads(r.content.decode()) - self.token = content['token'] + self.token = content.get(self.token_type) + if self.token is None: + # Default to "token" if token_type is not used by server + self.token = content.get('token') self.username = username return True else: @@ -285,4 +293,4 @@ def __getattr__(self, item): return self._get_resource(**kwargs) def _get_resource(self, **kwargs): - return self.resource_class(**kwargs) \ No newline at end of file + return self.resource_class(**kwargs) diff --git a/setup.py b/setup.py index 43ae96e..d58f5b6 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ README = (HERE / "README.md").read_text() setup(name='django-rest-framework-client', - version='0.4.1', + version='0.5.0', description='Python client for a DjangoRestFramework based web site', long_description=README, long_description_content_type="text/markdown", diff --git a/tests/api.py b/tests/api.py index d582160..9bfe5eb 100644 --- a/tests/api.py +++ b/tests/api.py @@ -49,7 +49,7 @@ def test_login(self, m): ok = self.api.login(username='user1@test.com', password='pass') self.assertTrue(ok) - self.assertEqual(self.api.username, 'user1') + self.assertEqual(self.api.username, 'user1@test.com') self.assertEqual(self.api.token, 'big-token') @requests_mock.Mocker() @@ -163,6 +163,14 @@ def test_delete(self, m): deleted = self.api.test('my-detail').delete() self.assertTrue(deleted) + result = { + "id": 2 + } + m.delete('https://example.com/api/v1/test/my-detail2/', text=json.dumps(result)) + + deleted = self.api.test('my-detail2').delete(data={'foo': 'bar'}) + self.assertTrue(deleted) + @requests_mock.Mocker() def test_post_with_error(self, m): payload = {