From c470b6a74f9ef8d7f71a1e798000cc4b3c3cc3d0 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 28 Jul 2023 14:01:02 +0300 Subject: [PATCH] Add retry on GET requests to QFieldCloud API --- qfieldcloud_sdk/sdk.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/qfieldcloud_sdk/sdk.py b/qfieldcloud_sdk/sdk.py index 3d415a0..0ca4a95 100644 --- a/qfieldcloud_sdk/sdk.py +++ b/qfieldcloud_sdk/sdk.py @@ -11,6 +11,7 @@ from qfieldcloud_sdk.interfaces import QfcException, QfcRequest, QfcRequestException from qfieldcloud_sdk.utils import get_numeric_params, log +from requests.adapters import HTTPAdapter, Retry logger = logging.getLogger(__file__) @@ -54,6 +55,16 @@ def __init__( `session` will be reused between requests if the SDK is run as a library. """ self.session = requests.Session() + # retries should be only on GET and only if error 5xx + retries = Retry( + total=5, + backoff_factor=0.1, + allowed_methods=["GET"], + # skip 501, as it is "Not Implemented", no point to retry + status_forcelist=[500, 502, 503, 504], + ) + self.session.mount("https://", HTTPAdapter(max_retries=retries)) + self.url = url or os.environ.get("QFIELDCLOUD_URL", None) self.token = token or os.environ.get("QFIELDCLOUD_TOKEN", None) self.verify_ssl = verify_ssl