Skip to content

Commit

Permalink
Merge pull request #69 from Dynatrace-James-Kitson/timeout-support
Browse files Browse the repository at this point in the history
add timeout option at Dynatrace object level
  • Loading branch information
dlopes7 authored Mar 6, 2023
2 parents 84119ce + 2f5d18e commit 28a370f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions dynatrace/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:
Expand Down Expand Up @@ -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}")
Expand Down
6 changes: 4 additions & 2 deletions dynatrace/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 28a370f

Please sign in to comment.