From adb2e233e39979b21467b9feb1bf672b14c3e462 Mon Sep 17 00:00:00 2001 From: Katistix <33645731+Katistic@users.noreply.github.com> Date: Mon, 5 Oct 2020 04:02:10 +1100 Subject: [PATCH] Retry logic (specificly when vrc disconnects request) --- vrcpy/request.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/vrcpy/request.py b/vrcpy/request.py index 4c439ab..9064fa0 100644 --- a/vrcpy/request.py +++ b/vrcpy/request.py @@ -6,6 +6,9 @@ from vrcpy.errors import * +call_retries = 1 + + def raise_for_status(resp): if type(resp["data"]) == bytes: resp["data"] = json.loads(resp["data"].decode()) @@ -80,7 +83,19 @@ async def closeSession(self): await self.session.close() self.session = None - async def call(self, path, method="GET", headers={}, params={}, json={}, no_auth=False, verify=True): + async def call(self, path, method="GET", headers={}, params={}, json={}, no_auth=False, verify=True, retries=None): + for tri in range(0, (retries or call_retries) + 1): + try: + resp = await self._call_wrap(path, method, headers, params, json, no_auth, verify) + break + except requests.exceptions.ConnectionError as e: # Gosh darnit VRC team, why've you done this! + if tri == (retries or call_retries): + raise requests.exceptions.ConnectionError( + str(e) + " ({} retries)".format(retries)) + + return resp + + async def _call_wrap(self, path, method="GET", headers={}, params={}, json={}, no_auth=False, verify=True): if no_auth: return await self._call(path, method, headers, params, json, verify) @@ -188,7 +203,19 @@ def new_session(self): self.session = requests.Session() self.b64_auth = None - def call(self, path, method="GET", headers={}, params={}, json={}, no_auth=False, verify=True): + def call(self, path, method="GET", headers={}, params={}, json={}, no_auth=False, verify=True, retries=None): + for tri in range(0, (retries or call_retries) + 1): + try: + resp = self._call_wrap(path, method, headers, params, json, no_auth, verify) + break + except requests.exceptions.ConnectionError as e: # Gosh darnit VRC team, why've you done this! + if tri == (retries or call_retries): + raise requests.exceptions.ConnectionError( + str(e) + " ({} retries)".format(retries)) + + return resp + + def _call_wrap(self, path, method="GET", headers={}, params={}, json={}, no_auth=False, verify=True): headers["user-agent"] = "" if no_auth: