Skip to content

Commit

Permalink
refactor(utility): use "config.timeout" as the argument of urlopen
Browse files Browse the repository at this point in the history
PR Closed: #1140
  • Loading branch information
zhen.chen authored and AChenQ committed Nov 30, 2021
1 parent f6b1a62 commit 33e1f7f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion tensorbay/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 1 addition & 27 deletions tensorbay/client/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions tensorbay/client/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tensorbay/utility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -53,6 +54,7 @@
"camel",
"chunked",
"common_loads",
"config",
"locked",
"repr_config",
"upper",
Expand Down
3 changes: 2 additions & 1 deletion tensorbay/utility/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from _io import BufferedReader

from tensorbay.utility.repr import ReprMixin
from tensorbay.utility.request_config import config


class URL:
Expand Down Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions tensorbay/utility/request_config.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 33e1f7f

Please sign in to comment.