Skip to content

Commit ded425b

Browse files
authored
Merge pull request #37 from praw-dev/preprocessing
Cast non-string objects to string when preprocessing ``data`` and ``params``
2 parents 8181cd0 + be1f362 commit ded425b

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Change Log
44
Unreleased
55
----------
66

7+
**Changed**
8+
9+
- Cast non-string objects to string when preprocessing ``data`` and ``params``.
10+
711
2.2.0 (2021/06/15)
812
------------------
913

asyncprawcore/sessions.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -115,56 +115,14 @@ def _log_request(data, method, params, url):
115115
log.debug(f"Params: {params}")
116116

117117
@staticmethod
118-
def _preprocess_data(data, files):
119-
"""Preprocess data and files before request.
120-
121-
This is to convert requests that are formatted for the ``requests`` package to
122-
be compatible with the ``aiohttp`` package. The motivation for this is so that
123-
``praw`` and ``asyncpraw`` can remain as similar as possible and thus making
124-
contributions to ``asyncpraw`` simpler.
125-
126-
This method does the following:
127-
128-
- Removes keys that have a value of ``None`` from ``data``.
129-
- Moves ``files`` into ``data``.
130-
131-
:param data: Dictionary, bytes, or file-like object to send in the body of the
132-
request.
133-
:param files: Dictionary, mapping ``filename`` to file-like object to add to
134-
``data``.
135-
136-
"""
137-
if isinstance(data, dict):
138-
data = {key: value for key, value in data.items() if value is not None}
139-
if files is not None:
140-
data.update(files)
141-
return data
142-
143-
@staticmethod
144-
def _preprocess_params(params):
145-
"""Preprocess params before request.
146-
147-
This is to convert requests that are formatted for the ``requests`` package to
148-
be compatible with ``aiohttp`` package. The motivation for this is so that
149-
``praw`` and ``asyncpraw`` can remain as similar as possible and thus making
150-
contributions to ``asyncpraw`` simpler.
151-
152-
This method does the following:
153-
154-
- Removes keys that have a value of ``None`` from ``params``.
155-
- Casts bool values in ``params`` to str.
156-
157-
:param params: The query parameters to send with the request.
158-
159-
"""
160-
new_params = {}
161-
for key, value in params.items():
118+
def _preprocess_dict(data):
119+
new_data = {}
120+
for key, value in data.items():
162121
if isinstance(value, bool):
163-
new_params[key] = str(value).lower()
122+
new_data[key] = str(value).lower()
164123
elif value is not None:
165-
new_params[key] = value
166-
params = new_params
167-
return params
124+
new_data[key] = str(value) if not isinstance(value, str) else value
125+
return new_data
168126

169127
def __init__(self, authorizer):
170128
"""Prepare the connection to reddit's API.
@@ -249,6 +207,49 @@ async def _make_request(
249207
raise
250208
return None, exception.original_exception
251209

210+
def _preprocess_data(self, data, files):
211+
"""Preprocess data and files before request.
212+
213+
This is to convert requests that are formatted for the ``requests`` package to
214+
be compatible with the ``aiohttp`` package. The motivation for this is so that
215+
``praw`` and ``asyncpraw`` can remain as similar as possible and thus making
216+
contributions to ``asyncpraw`` simpler.
217+
218+
This method does the following:
219+
220+
- Removes keys that have a value of ``None`` from ``data``.
221+
- Moves ``files`` into ``data``.
222+
223+
:param data: Dictionary, bytes, or file-like object to send in the body of the
224+
request.
225+
:param files: Dictionary, mapping ``filename`` to file-like object to add to
226+
``data``.
227+
228+
"""
229+
if isinstance(data, dict):
230+
data = self._preprocess_dict(data)
231+
if files is not None:
232+
data.update(files)
233+
return data
234+
235+
def _preprocess_params(self, params):
236+
"""Preprocess params before request.
237+
238+
This is to convert requests that are formatted for the ``requests`` package to
239+
be compatible with ``aiohttp`` package. The motivation for this is so that
240+
``praw`` and ``asyncpraw`` can remain as similar as possible and thus making
241+
contributions to ``asyncpraw`` simpler.
242+
243+
This method does the following:
244+
245+
- Removes keys that have a value of ``None`` from ``params``.
246+
- Casts bool values in ``params`` to str.
247+
248+
:param params: The query parameters to send with the request.
249+
250+
"""
251+
return self._preprocess_dict(params)
252+
252253
async def _request_with_retries(
253254
self,
254255
data,

0 commit comments

Comments
 (0)