Skip to content

Commit

Permalink
Allow delete() to accept payload (#9)
Browse files Browse the repository at this point in the history
* Allow delete() to accept payload

And fix broken tests.

Fixes #1

* Ensure change is backwards compatible
  • Loading branch information
dkarchmer authored May 17, 2022
1 parent bf2a593 commit 199b0d9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,7 +22,7 @@
### v0.1.2 (2020-06-01)

* Remove dependency on unitest2

### v0.1.0 (2017-05-06)

* First release
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

```
Expand Down
18 changes: 13 additions & 5 deletions drf_client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -285,4 +293,4 @@ def __getattr__(self, item):
return self._get_resource(**kwargs)

def _get_resource(self, **kwargs):
return self.resource_class(**kwargs)
return self.resource_class(**kwargs)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion tests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_login(self, m):

ok = self.api.login(username='[email protected]', 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()
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 199b0d9

Please sign in to comment.