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 0da00ab3d..bcc8d2043 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, ResponseError from tensorbay.label import Label from tensorbay.sensor.sensor import Sensor, Sensors -from tensorbay.utility import FileMixin, chunked, locked +from tensorbay.utility import 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 c973f0b5f..c66901639 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, @@ -52,6 +53,7 @@ "camel", "chunked", "common_loads", + "config", "locked", "repr_config", "upper", diff --git a/tensorbay/utility/file.py b/tensorbay/utility/file.py index 1d84d7d8e..1cb002093 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 FileMixin(ReprMixin): @@ -119,7 +120,7 @@ def _repr_head(self) -> str: def _urlopen(self) -> HTTPResponse: try: return urlopen( # type: ignore[no-any-return] - quote(self.get_url(), safe=printable), timeout=2 + quote(self.get_url(), 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()