From 84b13fdd1bd917de47d8985b0a68c294f5b57a40 Mon Sep 17 00:00:00 2001 From: Medovikov Yakov Date: Thu, 30 Jan 2020 12:33:58 +0300 Subject: [PATCH 1/5] [TRAVELQA-530] - support request_timeout param for init --- reqtools/sessions.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/reqtools/sessions.py b/reqtools/sessions.py index ac1fbe6..d8c9fcd 100644 --- a/reqtools/sessions.py +++ b/reqtools/sessions.py @@ -13,11 +13,14 @@ class RemoteApiSession(Session): __attrs__ = Session.__attrs__ + ['_base_url', '_prefix'] - def __init__(self, base_url: str, *, prefix: str = None): + def __init__(self, base_url: str, *, prefix: str = None, request_timeout: int = None): super().__init__() self._base_url = base_url self._prefix = prefix + self._reqest_timeout = 5 + if request_timeout: + self._reqest_timeout = request_timeout def __repr__(self): return f'{self.__class__.__name__}({self.url})' @@ -42,7 +45,8 @@ def request(self, method: str, url_path: str, **kwargs): f'Performing "{method}" request to "{url}"' f'\nRequest params: {self._serialize(kwargs)}') - resp = super().request(method, url, **kwargs) + resp = super().request(method, url, + timeout=self._reqest_timeout, **kwargs) try: resp_content = self._serialize(resp.json()) From 7e467515a9fb47b7dfe927ce5be2e71ced756c52 Mon Sep 17 00:00:00 2001 From: Medovikov Yakov Date: Thu, 30 Jan 2020 12:42:39 +0300 Subject: [PATCH 2/5] [TRAVELQA-530] - support request_timeout param for init (pep8 update) --- reqtools/sessions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reqtools/sessions.py b/reqtools/sessions.py index d8c9fcd..7d9005d 100644 --- a/reqtools/sessions.py +++ b/reqtools/sessions.py @@ -45,8 +45,7 @@ def request(self, method: str, url_path: str, **kwargs): f'Performing "{method}" request to "{url}"' f'\nRequest params: {self._serialize(kwargs)}') - resp = super().request(method, url, - timeout=self._reqest_timeout, **kwargs) + resp = super().request(method, url, timeout=self._reqest_timeout, **kwargs) try: resp_content = self._serialize(resp.json()) From 9d34388fd767b1b0ad93bcc1c45d5d0be94384c1 Mon Sep 17 00:00:00 2001 From: Medovikov Yakov Date: Fri, 31 Jan 2020 15:39:55 +0300 Subject: [PATCH 3/5] [TRAVELQA-530] - fix: if timeout is set in kwargs, we ignore default value. --- reqtools/sessions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/reqtools/sessions.py b/reqtools/sessions.py index 7d9005d..a9e43a4 100644 --- a/reqtools/sessions.py +++ b/reqtools/sessions.py @@ -38,6 +38,7 @@ def url(self): return self._build_url(self._base_url, self._prefix) def request(self, method: str, url_path: str, **kwargs): + kwargs.setdefault('timeout', self._reqest_timeout) url = self._build_url(self.url, url_path) @@ -45,7 +46,7 @@ def request(self, method: str, url_path: str, **kwargs): f'Performing "{method}" request to "{url}"' f'\nRequest params: {self._serialize(kwargs)}') - resp = super().request(method, url, timeout=self._reqest_timeout, **kwargs) + resp = super().request(method, url, **kwargs) try: resp_content = self._serialize(resp.json()) From c4b000dc3005843b3f828aa3ab55990ad946c87e Mon Sep 17 00:00:00 2001 From: Medovikov Yakov Date: Tue, 4 Feb 2020 16:50:42 +0300 Subject: [PATCH 4/5] [TRAVELQA-530] - compatibility fix: if timeout is not set - do not send it to request function. --- reqtools/sessions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reqtools/sessions.py b/reqtools/sessions.py index a9e43a4..d78521b 100644 --- a/reqtools/sessions.py +++ b/reqtools/sessions.py @@ -18,9 +18,8 @@ def __init__(self, base_url: str, *, prefix: str = None, request_timeout: int = self._base_url = base_url self._prefix = prefix - self._reqest_timeout = 5 if request_timeout: - self._reqest_timeout = request_timeout + self._request_timeout = request_timeout def __repr__(self): return f'{self.__class__.__name__}({self.url})' @@ -38,7 +37,8 @@ def url(self): return self._build_url(self._base_url, self._prefix) def request(self, method: str, url_path: str, **kwargs): - kwargs.setdefault('timeout', self._reqest_timeout) + if self._request_timeout: + kwargs.setdefault('timeout', self._request_timeout) url = self._build_url(self.url, url_path) From 69d7a4d8047af74b6a4e1343a8542b4773f42275 Mon Sep 17 00:00:00 2001 From: Medovikov Yakov Date: Tue, 4 Feb 2020 17:19:16 +0300 Subject: [PATCH 5/5] [TRAVELQA-530] - fix: do not fail, if no request_timeout is set --- reqtools/sessions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reqtools/sessions.py b/reqtools/sessions.py index d78521b..5203b55 100644 --- a/reqtools/sessions.py +++ b/reqtools/sessions.py @@ -18,8 +18,7 @@ def __init__(self, base_url: str, *, prefix: str = None, request_timeout: int = self._base_url = base_url self._prefix = prefix - if request_timeout: - self._request_timeout = request_timeout + self._request_timeout = request_timeout def __repr__(self): return f'{self.__class__.__name__}({self.url})'