From 33e1f7ffbb152df78cb2cbf73561e8a69d90c1b6 Mon Sep 17 00:00:00 2001 From: "zhen.chen" Date: Mon, 29 Nov 2021 15:48:29 +0800 Subject: [PATCH] refactor(utility): use "config.timeout" as the argument of urlopen PR Closed: https://github.com/Graviti-AI/tensorbay-python-sdk/pull/1140 --- tensorbay/client/__init__.py | 2 +- tensorbay/client/requests.py | 28 +----------------------- tensorbay/client/segment.py | 3 +-- tensorbay/utility/__init__.py | 2 ++ tensorbay/utility/file.py | 3 ++- tensorbay/utility/request_config.py | 33 +++++++++++++++++++++++++++++ 6 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 tensorbay/utility/request_config.py diff --git a/tensorbay/client/__init__.py b/tensorbay/client/__init__.py index ca3320328..77526bb69 100644 --- a/tensorbay/client/__init__.py +++ b/tensorbay/client/__init__.py @@ -7,7 +7,7 @@ from tensorbay.client.gas import GAS from tensorbay.client.profile import profile -from tensorbay.client.requests import config +from tensorbay.utility import config __all__ = [ "GAS", diff --git a/tensorbay/client/requests.py b/tensorbay/client/requests.py index 0e6efb7d2..af6ec56b7 100644 --- a/tensorbay/client/requests.py +++ b/tensorbay/client/requests.py @@ -32,6 +32,7 @@ from tensorbay.__version__ import __version__ from tensorbay.client.log import RequestLogging, ResponseLogging from tensorbay.exception import ResponseError, ResponseErrorDistributor +from tensorbay.utility import config logger = logging.getLogger(__name__) @@ -47,33 +48,6 @@ def _get_allowed_methods_keyword() -> str: _ALLOWED_METHODS = _get_allowed_methods_keyword() -class Config: - """This is a base class defining the concept of Request Config. - - Attributes: - max_retries: Maximum retry times of the request. - allowed_retry_methods: The allowed methods for retrying request. - allowed_retry_status: The allowed status for retrying request. - If both methods and status are fitted, the retrying strategy will work. - timeout: Timeout value of the request in seconds. - is_internal: Whether the request is from internal. - - """ - - def __init__(self) -> None: - - self.max_retries = 3 - self.allowed_retry_methods = ["HEAD", "OPTIONS", "POST", "PUT"] - self.allowed_retry_status = [429, 500, 502, 503, 504] - - self.timeout = 30 - self.is_internal = False - self._x_source = "PYTHON-SDK" - - -config = Config() - - class TimeoutHTTPAdapter(HTTPAdapter): """This class defines the http adapter for setting the timeout value. diff --git a/tensorbay/client/segment.py b/tensorbay/client/segment.py index 3a01683e7..99edbb517 100644 --- a/tensorbay/client/segment.py +++ b/tensorbay/client/segment.py @@ -34,13 +34,12 @@ from ulid import ULID, from_timestamp from tensorbay.client.lazy import LazyPage, PagingList -from tensorbay.client.requests import config from tensorbay.client.status import Status from tensorbay.dataset import AuthData, Data, Frame, RemoteData from tensorbay.exception import FrameError, InvalidParamsError, ResourceNotExistError, ResponseError from tensorbay.label import Label from tensorbay.sensor.sensor import Sensor, Sensors -from tensorbay.utility import URL, FileMixin, chunked, locked +from tensorbay.utility import URL, FileMixin, chunked, config, locked if TYPE_CHECKING: from tensorbay.client.dataset import DatasetClient, FusionDatasetClient diff --git a/tensorbay/utility/__init__.py b/tensorbay/utility/__init__.py index d3ed28300..d68114a58 100644 --- a/tensorbay/utility/__init__.py +++ b/tensorbay/utility/__init__.py @@ -17,6 +17,7 @@ from tensorbay.utility.itertools import chunked from tensorbay.utility.name import NameList, NameMixin, SortedNameList from tensorbay.utility.repr import ReprMixin, ReprType, repr_config +from tensorbay.utility.request_config import config from tensorbay.utility.type import TypeEnum, TypeMixin, TypeRegister from tensorbay.utility.user import ( UserMapping, @@ -53,6 +54,7 @@ "camel", "chunked", "common_loads", + "config", "locked", "repr_config", "upper", diff --git a/tensorbay/utility/file.py b/tensorbay/utility/file.py index 1f4cd15c8..dc6960006 100644 --- a/tensorbay/utility/file.py +++ b/tensorbay/utility/file.py @@ -17,6 +17,7 @@ from _io import BufferedReader from tensorbay.utility.repr import ReprMixin +from tensorbay.utility.request_config import config class URL: @@ -167,7 +168,7 @@ def _urlopen(self) -> HTTPResponse: try: return urlopen( # type: ignore[no-any-return] - quote(self.url.get(), safe=printable), timeout=2 + quote(self.url.get(), safe=printable), timeout=config.timeout ) except HTTPError as error: if error.code == 403: diff --git a/tensorbay/utility/request_config.py b/tensorbay/utility/request_config.py new file mode 100644 index 000000000..d8f966b1f --- /dev/null +++ b/tensorbay/utility/request_config.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +# +# Copyright 2021 Graviti. Licensed under MIT License. +# + +"""The request related configs.""" + + +class Config: + """This is a base class defining the concept of Request Config. + + Attributes: + max_retries: Maximum retry times of the request. + allowed_retry_methods: The allowed methods for retrying request. + allowed_retry_status: The allowed status for retrying request. + If both methods and status are fitted, the retrying strategy will work. + timeout: Timeout value of the request in seconds. + is_internal: Whether the request is from internal. + + """ + + def __init__(self) -> None: + + self.max_retries = 3 + self.allowed_retry_methods = ["HEAD", "OPTIONS", "POST", "PUT"] + self.allowed_retry_status = [429, 500, 502, 503, 504] + + self.timeout = 30 + self.is_internal = False + self._x_source = "PYTHON-SDK" + + +config = Config()