Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #21 #29

Merged
merged 5 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions opsgenie_sdk/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
self.configuration = configuration
self.pool_threads = pool_threads

self.retrying = tenacity.Retrying(stop=tenacity.stop_after_attempt(configuration.retry_count),
self.retrying = tenacity.Retrying(stop=self.should_retry_stop,
wait=tenacity.wait_random_exponential(multiplier=configuration.back_off,
max=configuration.retry_max_delay,
min=configuration.retry_delay),
retry=(tenacity.retry_if_result(self.is_retry_enabled) and
((tenacity.retry_if_exception_type(RetryableException)) |
(tenacity.retry_if_exception_type(HTTPError)))))
max=configuration.retry_delay),
retry=(tenacity.retry_if_exception_type(RetryableException) |
(tenacity.retry_if_exception_type(HTTPError))))

self.rest_client = rest.RESTClientObject(configuration, retrying=self.retrying)
self.default_headers = {}
Expand All @@ -104,8 +102,11 @@ def __del__(self):
self._pool.join()
self._pool = None

def is_retry_enabled(self):
return self.configuration.retry_enabled
def should_retry_stop(self, retry_state):
if self.configuration.retry_enabled and retry_state.attempt_number <= self.configuration.retry_count and retry_state.seconds_since_start <= self.configuration.retry_max_delay:
return False

return True

@property
def pool(self):
Expand Down Expand Up @@ -192,13 +193,30 @@ def __call_api(
url = _host + resource_path

# perform request and return response
response_data = self.retrying.call(fn=self.request, method=method, url=url,
query_params=query_params,
headers=header_params,
post_params=post_params,
body=body,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
try:
response_data = self.retrying.call(fn=self.request, method=method, url=url,
query_params=query_params,
headers=header_params,
post_params=post_params,
body=body,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
except Exception as exception:
self._sdk_request_details = {
"query_params": query_params,
"headers": header_params,
"post_params": post_params,
"body": body,
"_preload_content": _preload_content,
"_request_timeout": _request_timeout
}
self.sdk_metric_publisher.build_metric(transaction_id=config.metrics_transaction_id,
duration=datetime.datetime.now() - self._request_start_time,
resource_path=url, error_type=type(exception),
error_message=str(exception),
sdk_request_details=self._sdk_request_details,
sdk_result_details="An Exception Was Thrown!")
raise exception

self.last_response = response_data

Expand Down
4 changes: 1 addition & 3 deletions opsgenie_sdk/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self):
# Retry count
self.retry_count = 5
# Delay time between attempts
self.retry_delay = 0
self.retry_delay = 30
# Maximum amount of delay
self.retry_max_delay = 60
# Multiplier applied to delay between attempts
Expand Down Expand Up @@ -116,8 +116,6 @@ def __init__(self):
self.proxy_headers = None
# Safe chars for path_param
self.safe_chars_for_path_param = ''
# Adding retries to override urllib3 default value 3
self.retries = None

self.metrics_transaction_id = None

Expand Down
6 changes: 4 additions & 2 deletions opsgenie_sdk/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def request(self, method, url, query_params=None, headers=None,
logger.debug("response body: %s", r.data)

data = self.decodeResponse(r.data)
http_metrics_request = [('method', method), ('url', url), ('query_params', query_params), ('headers', headers),
('body', body), ('post_params', post_params)]
if "message" not in data:
self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id,
duration=data["took"],
Expand All @@ -246,7 +248,7 @@ def request(self, method, url, query_params=None, headers=None,
error=False,
status=r.status,
status_code=r.status,
request=query_params)
request=http_metrics_request)
else:
self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id,
duration=data["took"],
Expand All @@ -255,7 +257,7 @@ def request(self, method, url, query_params=None, headers=None,
error=True,
status=r.status,
status_code=r.status,
request=query_params)
request=http_metrics_request)

should_retry = self.__checkHttpCode__(r.status)
if should_retry:
Expand Down
48 changes: 33 additions & 15 deletions templates/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ class ApiClient(object):
self.configuration = configuration
self.pool_threads = pool_threads

self.retrying = tenacity.Retrying(stop=tenacity.stop_after_attempt(configuration.retry_count),
self.retrying = tenacity.Retrying(stop=self.should_retry_stop,
wait=tenacity.wait_random_exponential(multiplier=configuration.back_off,
max=configuration.retry_max_delay,
min=configuration.retry_delay),
retry=(tenacity.retry_if_result(self.is_retry_enabled) and
((tenacity.retry_if_exception_type(RetryableException)) |
(tenacity.retry_if_exception_type(HTTPError)))))
max=configuration.retry_delay),
retry=(tenacity.retry_if_exception_type(RetryableException) |
(tenacity.retry_if_exception_type(HTTPError))))

self.rest_client = rest.RESTClientObject(configuration, retrying=self.retrying)
self.default_headers = {}
Expand All @@ -98,8 +96,11 @@ class ApiClient(object):
self._pool.join()
self._pool = None

def is_retry_enabled(self):
return self.configuration.retry_enabled
def should_retry_stop(self, retry_state):
if self.configuration.retry_enabled and retry_state.attempt_number <= self.configuration.retry_count and retry_state.seconds_since_start <= self.configuration.retry_max_delay:
return False

return True

@property
def pool(self):
Expand Down Expand Up @@ -189,13 +190,30 @@ class ApiClient(object):
url = _host + resource_path

# perform request and return response
response_data = {{#asyncio}}await {{/asyncio}}{{#tornado}}yield {{/tornado}}self.retrying.call(fn=self.request, method=method, url=url,
query_params=query_params,
headers=header_params,
post_params=post_params,
body=body,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
try:
response_data = {{#asyncio}}await {{/asyncio}}{{#tornado}}yield {{/tornado}}self.retrying.call(fn=self.request, method=method, url=url,
query_params=query_params,
headers=header_params,
post_params=post_params,
body=body,
_preload_content=_preload_content,
_request_timeout=_request_timeout)
except Exception as exception:
self._sdk_request_details = {
"query_params": query_params,
"headers": header_params,
"post_params": post_params,
"body": body,
"_preload_content": _preload_content,
"_request_timeout": _request_timeout
}
self.sdk_metric_publisher.build_metric(transaction_id=config.metrics_transaction_id,
duration=datetime.datetime.now() - self._request_start_time,
resource_path=url, error_type=type(exception),
error_message=str(exception),
sdk_request_details=self._sdk_request_details,
sdk_result_details="An Exception Was Thrown!")
raise exception

self.last_response = response_data

Expand Down
4 changes: 1 addition & 3 deletions templates/configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
# Retry count
self.retry_count = 5
# Delay time between attempts
self.retry_delay = 0
self.retry_delay = 30
# Maximum amount of delay
self.retry_max_delay = 60
# Multiplier applied to delay between attempts
Expand Down Expand Up @@ -117,8 +117,6 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
self.proxy_headers = None
# Safe chars for path_param
self.safe_chars_for_path_param = ''
# Adding retries to override urllib3 default value 3
self.retries = None

self.metrics_transaction_id = None

Expand Down
6 changes: 4 additions & 2 deletions templates/rest.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ class RESTClientObject(object):
logger.debug("response body: %s", r.data)

data = self.decodeResponse(r.data)
http_metrics_request = [('method', method), ('url', url), ('query_params', query_params), ('headers', headers),
('body', body), ('post_params', post_params)]
if "message" not in data:
self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id,
duration=data["took"],
Expand All @@ -237,7 +239,7 @@ class RESTClientObject(object):
error=False,
status=r.status,
status_code=r.status,
request=query_params)
request=http_metrics_request)
else:
self.http_metric.build_metric(transaction_id=self.configuration.metrics_transaction_id,
duration=data["took"],
Expand All @@ -246,7 +248,7 @@ class RESTClientObject(object):
error=True,
status=r.status,
status_code=r.status,
request=query_params)
request=http_metrics_request)

should_retry = self.__checkHttpCode__(r.status)
if should_retry:
Expand Down