From 6babddfb146b325ee831bf5567499b1adb45ab99 Mon Sep 17 00:00:00 2001 From: LilSpazJoekp <15524072+LilSpazJoekp@users.noreply.github.com> Date: Mon, 28 Sep 2020 17:09:10 -0500 Subject: [PATCH] Fix data and params handling to be inline with prawcore --- CHANGES.rst | 10 ++++++++++ asyncprawcore/sessions.py | 30 +++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a9c6019..82803c8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,16 @@ Change Log asyncprawcore follows `semantic versioning `_ 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) ------------------------ diff --git a/asyncprawcore/sessions.py b/asyncprawcore/sessions.py index 71b6b40..ee5f6f7 100644 --- a/asyncprawcore/sessions.py +++ b/asyncprawcore/sessions.py @@ -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, @@ -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