From fc23eae9c92c09beac3a2aa0704e49644a3761e7 Mon Sep 17 00:00:00 2001 From: Rustem Sayargaliev Date: Wed, 22 Aug 2018 17:17:33 +0200 Subject: [PATCH 1/2] Use format arg in resources Documentation page https://slumber.readthedocs.io/en/v0.6.0/options.html#serializer says that we can define certain format for each resource, but this was not taken into an account. PR adds this behaviour and fixes #131. --- slumber/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/slumber/__init__.py b/slumber/__init__.py index 9bb3c7a..86e6ead 100644 --- a/slumber/__init__.py +++ b/slumber/__init__.py @@ -85,14 +85,15 @@ def __call__(self, id=None, format=None, url_override=None): def _request(self, method, data=None, files=None, params=None): serializer = self._store["serializer"] + fmt = self._store['format'] url = self.url() - headers = {"accept": serializer.get_content_type()} + headers = {"accept": serializer.get_content_type(format=fmt)} if not files: if data is not None: - headers["content-type"] = serializer.get_content_type() - data = serializer.dumps(data) + headers["content-type"] = serializer.get_content_type(format=fmt) + data = serializer.dumps(data, format=fmt) resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers) From 85e90315205ff2fe72165a9ce8f0a7db436d06b3 Mon Sep 17 00:00:00 2001 From: Rustem Sayargaliev Date: Wed, 22 Aug 2018 17:39:49 +0200 Subject: [PATCH 2/2] Add test --- tests/resource.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/resource.py b/tests/resource.py index 4665411..e60f523 100644 --- a/tests/resource.py +++ b/tests/resource.py @@ -626,3 +626,25 @@ def test_unicode_decodable_response(self): resp = self.base_resource.post(data={'foo': 'bar'}) expected = b'Pr\xc3\xa9paratoire'.decode('utf8') self.assertEqual(resp['result'], expected) + + def test_resource_with_custom_format(self): + r = mock.Mock(spec=requests.Response) + r.status_code = 200 + r.content = '{"result": ["a", "b", "c"]}' + r.headers = {"content-type": "application/json"} + + self.base_resource._store.update({ + "session": mock.Mock(spec=requests.Session), + "serializer": slumber.serialize.Serializer(), + }) + self.base_resource._store["session"].request.return_value = r + + resource = self.base_resource(format='yaml') + serializer = resource._store["serializer"] + fmt = resource._store['format'] + content_type = serializer.get_content_type(format=fmt) + self.assertEqual(fmt, 'yaml') + self.assertEqual(content_type, 'text/yaml') + + resp = self.base_resource(format='yaml').post(data={'foo': 'bar'}) + self.assertEqual(resp['result'], ['a', 'b', 'c']) \ No newline at end of file