Skip to content

Commit

Permalink
Fix data and params handling to be inline with prawcore
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Sep 29, 2020
1 parent f8763b4 commit 6babddf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Expand Up @@ -4,6 +4,16 @@ Change Log
asyncprawcore follows `semantic versioning <http://semver.org/>`_ with the exception
that deprecations will not be announced by a minor release.

1.5.0 (2020-09-28)
------------------

**Fixed**

* When passing a ``None`` value for a key in ``data`` it is not dropped like it is in
``prawcore`` with ``requests`` package.
* When passing a bool or ``None`` value for a key in ``params`` it is not dropped like
it is in ``prawcore`` with ``requests`` package.

1.4.0.post2 (2020-07-12)
------------------------

Expand Down
30 changes: 23 additions & 7 deletions asyncprawcore/sessions.py
Expand Up @@ -310,11 +310,21 @@ async def request(
"""
params = deepcopy(params) or {}
if params:
# this is to ensure consistency with prawcore as requests accepts bool
# params while aiohttp does not
new_params = {}
for k, v in params.items():
if isinstance(v, bool):
new_params[k] = str(v).lower()
elif v is not None:
new_params[k] = v
params = new_params
params["raw_json"] = 1
data = self.validate_data_files(data, files)
if isinstance(json, dict):
json = deepcopy(json)
json["api_type"] = "json"
data = self.validate_data_files(data, files)
url = urljoin(self._requestor.oauth_url, path)
return await self._request_with_retries(
data=data,
Expand All @@ -329,18 +339,24 @@ async def request(
def validate_data_files(data, files):
"""Transfers the files and data from the arguments into the data.
This is done to maintain consistency with prawcore :param data dictionary of
data :param files dictionary of "file" mapped to file-stream
This is done to maintain consistency with prawcore
:param data dictionary of data
:param files dictionary of "file" mapped to file-stream
"""
if isinstance(data, dict):
data = deepcopy(data)
data["api_type"] = "json"
# this is to ensure consistency with prawcore as requests drop keys who's
# values are None
new_data = {}
for k, v in data.items():
if v is not None:
new_data[k] = v
new_data["api_type"] = "json"
if files is not None:
data.update(files)
data = sorted(data.items())
if isinstance(files, dict):
data = files
data = sorted(new_data.items())
return data


Expand Down

0 comments on commit 6babddf

Please sign in to comment.