From ae70af4d79d0062b0ee6b3847006bec438df3fc9 Mon Sep 17 00:00:00 2001 From: James Kitson Date: Fri, 24 Feb 2023 11:27:17 -0500 Subject: [PATCH 1/2] add timeout option --- dynatrace/http_client.py | 8 +++++--- dynatrace/main.py | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/dynatrace/http_client.py b/dynatrace/http_client.py index 877100c..e7b55e7 100644 --- a/dynatrace/http_client.py +++ b/dynatrace/http_client.py @@ -47,7 +47,8 @@ def __init__( mc_jsession_id: Optional[str] = None, mc_b925d32c: Optional[str] = None, mc_sso_csrf_cookie: Optional[str] = None, - print_bodies: bool = False, + print_bodies: bool = False, + timeout: Optional[int] = None ): while base_url.endswith("/"): base_url = base_url[:-1] @@ -69,6 +70,7 @@ def __init__( self.log.addHandler(st) self.too_many_requests_strategy = too_many_requests_strategy + self.timeout = timeout retry_delay_s = retry_delay_ms / 1000 try: @@ -122,14 +124,14 @@ def make_request( print(method, url) if body: print(json.dumps(body, indent=2)) - r = s.request(method, url, headers=headers, params=params, json=body, verify=False, proxies=self.proxies, data=data, cookies=cookies, files=files) + r = s.request(method, url, headers=headers, params=params, json=body, verify=False, proxies=self.proxies, data=data, cookies=cookies, files=files, timeout=self.timeout) self.log.debug(f"Received response '{r}'") while r.status_code == 429 and self.too_many_requests_strategy == TOO_MANY_REQUESTS_WAIT: sleep_amount = int(r.headers.get("retry-after", 5)) self.log.warning(f"Sleeping for {sleep_amount}s because we have received an HTTP 429") time.sleep(sleep_amount) - r = requests.request(method, url, headers=headers, params=params, json=body, verify=False, proxies=self.proxies) + r = requests.request(method, url, headers=headers, params=params, json=body, verify=False, proxies=self.proxies, timeout=self.timeout) if r.status_code >= 400: raise Exception(f"Error making request to {url}: {r}. Response: {r.text}") diff --git a/dynatrace/main.py b/dynatrace/main.py index b0f3122..29d213f 100644 --- a/dynatrace/main.py +++ b/dynatrace/main.py @@ -72,7 +72,8 @@ def __init__( mc_jsession_id: Optional[str] = None, mc_b925d32c: Optional[str] = None, mc_sso_csrf_cookie: Optional[str] = None, - print_bodies = False + print_bodies = False, + timeout: Optional[int] = None ): self.__http_client = HttpClient( base_url, @@ -85,7 +86,8 @@ def __init__( mc_jsession_id, mc_b925d32c, mc_sso_csrf_cookie, - print_bodies + print_bodies, + timeout ) self.activegates: ActiveGateService = ActiveGateService(self.__http_client) From 2f5d18e14ae16d0d00e9a5f117807f1180da1cb2 Mon Sep 17 00:00:00 2001 From: James Kitson Date: Thu, 2 Mar 2023 10:05:58 -0500 Subject: [PATCH 2/2] increaase version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 884a027..355d7a3 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="dt", - version="1.1.57", + version="1.1.58", packages=find_packages(), install_requires=["requests>=2.22"], tests_require=["pytest", "mock", "tox"],