From 60be1b9446c70277714c2ee24b9fe2be76e9c979 Mon Sep 17 00:00:00 2001 From: "Noah D. Brenowitz" Date: Wed, 28 Apr 2021 06:17:56 -0700 Subject: [PATCH] fix 400 errors on retries in _call (#380) * fix 400 errors on retries in _call The kwargs was being overwritten by ._get_args. If any retries are needed, then on the second iteration datain will always be 'None'. This will cause uploading steps to return with exit code 400. This commit fixes this bug by refactoring the _call method to minimize the number of variabes in the scope. Resolves #290 * rename to params for clarity --- gcsfs/core.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/gcsfs/core.py b/gcsfs/core.py index 38e51b9a..b4e73b06 100644 --- a/gcsfs/core.py +++ b/gcsfs/core.py @@ -498,6 +498,27 @@ def _get_args(self, path, *args, **kwargs): kwargs["userProject"] = user_project return path, jsonin, datain, headers, kwargs + async def _request(self, method, path, *args, **kwargs): + await self._set_session() + self.maybe_refresh() + path, jsonin, datain, headers, params = self._get_args(path, *args, **kwargs) + async with self.session.request( + method=method, + url=path, + params=params, + json=jsonin, + headers=headers, + data=datain, + timeout=self.requests_timeout, + ) as r: + + status = r.status + headers = r.headers + info = r.request_info # for debug only + contents = await r.read() + + return status, headers, info, contents + async def _call( self, method, path, *args, json_out=False, info_out=False, **kwargs ): @@ -507,26 +528,9 @@ async def _call( try: if retry > 0: await asyncio.sleep(min(random.random() + 2 ** (retry - 1), 32)) - self.maybe_refresh() - path, jsonin, datain, headers, kwargs = self._get_args( - path, *args, **kwargs + status, headers, info, contents = await self._request( + method, path, *args, **kwargs ) - await self._set_session() - async with self.session.request( - method=method, - url=path, - params=kwargs, - json=jsonin, - headers=headers, - data=datain, - timeout=self.requests_timeout, - ) as r: - - status = r.status - headers = r.headers - info = r.request_info # for debug only - contents = await r.read() - self.validate_response(status, contents, path, headers) break except (HttpError, RequestException, GoogleAuthError, ChecksumError) as e: