Skip to content

Commit 7d92087

Browse files
committed
introduce return_raw_response engine attribute
1 parent 67fe322 commit 7d92087

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Changelog
4747
registry, to which, all resources that inherit from any Python-Redmine resource are being automatically added
4848
- Removed ``container_many`` in favor of ``container_filter``, ``container_create`` and ``container_update``
4949
attributes on ``Resource`` object to allow more fine-grained resource setup
50+
- ``return_raw`` parameter on ``engine.request()`` and ``engine.process_response()`` methods has been removed
51+
in favor of ``return_raw_response`` attribute on engine object
5052

5153
**Bugfixes**:
5254

redminelib/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, url, **kwargs):
3030
:param raise_attr_exception: (optional). Control over resource attribute access exception raising.
3131
:type raise_attr_exception: bool or tuple
3232
:param cls engine: (optional). Engine that will be used to make requests to Redmine.
33+
:param bool return_raw_response (optional). Whether engine should return raw or json encoded responses.
3334
"""
3435
self.url = url.rstrip('/')
3536
self.ver = kwargs.get('version', None)
@@ -123,7 +124,8 @@ def download(self, url, savepath=None, filename=None, params=None):
123124
:param string filename: (optional). Name that will be used for the file.
124125
:param dict params: (optional). Params to send in the query string.
125126
"""
126-
response = self.engine.request('get', url, params=dict(params or {}, **{'stream': True}), return_raw=True)
127+
with self.session(return_raw_response=True):
128+
response = self.engine.request('get', url, params=dict(params or {}, **{'stream': True}))
127129

128130
# If a savepath wasn't provided we return a response directly
129131
# so a user can have maximum control over response data

redminelib/engines/base.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def __init__(self, **options):
1717
:param string password: (optional). Password used for authentication.
1818
:param dict requests: (optional). Connection options.
1919
:param string impersonate: (optional). Username to impersonate.
20+
:param bool return_raw_response (optional). Whether to return raw or json encoded responses.
2021
"""
22+
self.return_raw_response = options.pop('return_raw_response', False)
2123
self.requests = dict(dict(headers={}, params={}, data={}), **options.get('requests', {}))
2224

2325
if options.get('impersonate') is not None:
@@ -59,7 +61,7 @@ def construct_request_kwargs(method, headers, params, data):
5961

6062
return kwargs
6163

62-
def request(self, method, url, headers=None, params=None, data=None, return_raw=False):
64+
def request(self, method, url, headers=None, params=None, data=None):
6365
"""
6466
Makes a single request to Redmine and returns processed response.
6567
@@ -69,10 +71,9 @@ def request(self, method, url, headers=None, params=None, data=None, return_raw=
6971
:param dict params: (optional). Params to send in the query string.
7072
:param data: (optional). Data to send in the body of the request.
7173
:type data: dict, bytes or file-like object
72-
:param bool return_raw: (optional). Whether to return raw or json encoded response.
7374
"""
7475
kwargs = self.construct_request_kwargs(method, headers, params, data)
75-
return self.process_response(self.session.request(method, url, **kwargs), return_raw)
76+
return self.process_response(self.session.request(method, url, **kwargs))
7677

7778
def bulk_request(self, method, url, container, **params):
7879
"""
@@ -125,13 +126,11 @@ def process_bulk_request(self, method, url, container, bulk_params):
125126
"""
126127
raise NotImplementedError
127128

128-
@staticmethod
129-
def process_response(response, return_raw=False):
129+
def process_response(self, response):
130130
"""
131131
Processes response received from Redmine.
132132
133133
:param obj response: (required). Response object with response details.
134-
:param bool return_raw: (optional). Whether to return raw or json encoded response.
135134
"""
136135
if response.history:
137136
r = response.history[0]
@@ -141,7 +140,7 @@ def process_response(response, return_raw=False):
141140
status_code = response.status_code
142141

143142
if status_code in (200, 201, 204):
144-
if return_raw:
143+
if self.return_raw_response:
145144
return response
146145
elif not response.content.strip():
147146
return True

0 commit comments

Comments
 (0)