diff --git a/opsgenie_sdk/api_client.py b/opsgenie_sdk/api_client.py index 6d090ad..394a141 100644 --- a/opsgenie_sdk/api_client.py +++ b/opsgenie_sdk/api_client.py @@ -708,6 +708,9 @@ def __deserialize_model(self, data, klass): value = data[klass.attribute_map[attr]] kwargs[attr] = self.__deserialize(value, attr_type) + if hasattr(klass, "url") and hasattr(klass, "id"): + kwargs["api_client"] = self + instance = klass(**kwargs) if hasattr(instance, 'get_real_child_model'): diff --git a/opsgenie_sdk/models/success_response.py b/opsgenie_sdk/models/success_response.py index 8c3387b..e908c1e 100644 --- a/opsgenie_sdk/models/success_response.py +++ b/opsgenie_sdk/models/success_response.py @@ -17,9 +17,7 @@ import six import time -import json -import certifi -import urllib3 +import random import opsgenie_sdk from opsgenie_sdk.models.success_data import SuccessData # noqa: F401,E501 @@ -56,7 +54,7 @@ class SuccessResponse(object): } attribute_map['url'] = 'url' - def __init__(self, request_id=None, took=0.0, result=None, data=None, url=None): # noqa: E501 + def __init__(self, request_id=None, took=0.0, result=None, data=None, url=None, api_client=None): # noqa: E501 """SuccessResponse - a model defined in OpenAPI""" # noqa: E501 self._request_id = None @@ -72,13 +70,11 @@ def __init__(self, request_id=None, took=0.0, result=None, data=None, url=None): if data is not None: self.data = data - if url is not None: - self.url = url - else: - self.url = None + self.url = url + self.api_client = api_client self._id = None - self.conf = opsgenie_sdk.configuration.Configuration() + self.conf = api_client.configuration self.logger = self.conf.logger["package_logger"] @property def request_id(self): @@ -190,96 +186,67 @@ def url(self, url): self._url = url def retrieve_result(self): - conf = self.conf - api_key = conf.api_key_prefix.get('Authorization') + ' ' + conf.api_key.get('Authorization') - http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) - headers = { - 'Authorization': api_key - } - - request_url = self._build_request_url_(conf.host) - - attempt_count = 0 - MAX_NUMBER_OF_RETRIES = conf.short_polling_max_retries + domain_api_client = self._get_domain_api_client_() + MAX_NUMBER_OF_RETRIES = self.conf.short_polling_max_retries condition = True response = None + attempt_count = 0 while condition: if attempt_count > 0: - time.sleep(2 * attempt_count * 0.2) + sleep_time = random.uniform(0, (0.2 * 2 ** attempt_count)) + time.sleep(sleep_time) try: - self.logger.debug( - str(attempt_count + 1) + ' attempt to retrieve result with request_id: ' + str(self.request_id)) - response = http.request(method='GET', url=request_url, headers=headers) - except ApiException as err: - print("Exception when calling success_response->retrieve_result: %s\n" % err) - - response_headers = response.headers - if response_headers is not None: - rate_limit_state = response_headers.get('X-RateLimit-State') - status_code = response.status - - if rate_limit_state == 'THROTTLED': - should_retry = True - self.logger.debug('Should retry because X-RateLimit-State is THROTTLED') - elif status_code == 429: - should_retry = True - self.logger.debug('Should retry because Status is 429') - elif 500 <= status_code <= 599: - should_retry = True - self.logger.debug('Should retry because Status is ' + status_code) - else: - should_retry = False - response_body = response.data - response_body_decoded = json.loads(response_body) - response_body_decoded_data = response_body_decoded.get('data') - if response_body_decoded_data is not None: - self._id = response_body_decoded_data.get('alertId') - if self._id is None: - self._id = response_body_decoded_data.get('incidentId') - - attempt_count += 1 - - if should_retry and attempt_count < MAX_NUMBER_OF_RETRIES: - condition = True - else: - condition = False + response = domain_api_client.get_incident_request_status(request_id=self.request_id) + except AttributeError: + response = domain_api_client.get_request_status(request_id=self.request_id) + + if response.data.is_success is True: + should_retry = False + + if hasattr(response.data, "alert_id"): + self._id = response.data.alert_id + elif hasattr(response.data, "incident_id"): + self._id = response.data.incident_id else: + should_retry = True + + if should_retry and attempt_count < MAX_NUMBER_OF_RETRIES: condition = True + else: + condition = False + + attempt_count += 1 return response def retrieve_resulting_action(self): - conf = self.conf - api_key = conf.api_key_prefix.get('Authorization') + ' ' + conf.api_key.get('Authorization') - http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) - headers = { - 'Authorization': api_key - } - if self._id is None: self.retrieve_result() - request_url = self._build_request_url_(conf.host) + domain_api_client = self._get_domain_api_client_() try: - response = http.request(method='GET', url=request_url, headers=headers) + action = domain_api_client.get_alert(identifier=self._id) + except AttributeError: + action = domain_api_client.get_incident(identifier=self._id) - return response - except ApiException as err: - print("Exception when calling success_response->retrieve_result: %s\n" % err) + return action - def _build_request_url_(self, host): + def _get_domain_api_client_(self): if 'alerts' in self.url: - return host + '/v2/alerts/requests/' + self.request_id + return opsgenie_sdk.AlertApi(api_client=self.api_client) elif 'incident' in self.url: - return host + '/v1/incidents/requests/' + self.request_id + return opsgenie_sdk.IncidentApi(api_client=self.api_client) else: raise ApiException(reason='Short polling is not currently supported for this domain') @property def id(self): + if self._id is None: + self.retrieve_result() + return self._id def to_dict(self): diff --git a/opsgenie_sdk/rest.py b/opsgenie_sdk/rest.py index 0ecb787..e159fb5 100644 --- a/opsgenie_sdk/rest.py +++ b/opsgenie_sdk/rest.py @@ -263,6 +263,7 @@ def request(self, method, url, query_params=None, headers=None, exception = exceptions.build_exception(response=r) raise exception + self.retries = -1 return r def GET(self, url, headers=None, query_params=None, _preload_content=True, diff --git a/samples/alert.py b/samples/alert.py index cd09120..fe03ef8 100644 --- a/samples/alert.py +++ b/samples/alert.py @@ -1,5 +1,4 @@ import getopt -import json import sys import opsgenie_sdk @@ -150,12 +149,17 @@ def main(argv): print('Create Alert:') response = alert.create_alert() + alert_id = response.id + print() print('Retrieve Result:') result = response.retrieve_result() - print(json.loads(result.data)) + print(result) - alert_id = response.id + print() + print('Retrieve Resulting Action:') + alert1 = response.retrieve_resulting_action() + print(alert1) print() print('Get Alert:') diff --git a/samples/incident.py b/samples/incident.py index 4cfa047..4d174f2 100644 --- a/samples/incident.py +++ b/samples/incident.py @@ -1,12 +1,10 @@ import getopt -import json import sys import opsgenie_sdk from opsgenie_sdk.metrics.observer import Observer from opsgenie_sdk.rest import ApiException - class MetricObserver(Observer): def notify(self, publisher): print("{}: '{}' has now metric data = {}".format(type(self).__name__, @@ -126,7 +124,12 @@ def main(argv): print() print('Retrieve Result:') result = response.retrieve_result() - print(json.loads(result.data)) + print(result) + + print() + print('Retrieve Resulting Action:') + incident1 = response.retrieve_resulting_action() + print(incident1) incident_id = response.id diff --git a/setup.py b/setup.py index 90aa8cf..0e64345 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ # prerequisite: setuptools # http://pypi.python.org/pypi/setuptools -REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil", "numpy >= 1.16.3", "retry >= 0.9.2"] +REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil", "numpy >= 1.16.3", "retry >= 0.9.2", "setuptools >= 21.0.0"] with open("README.md", "r") as fh: long_description = fh.read() diff --git a/templates/api_client.mustache b/templates/api_client.mustache index 2fa2cbd..d95878f 100755 --- a/templates/api_client.mustache +++ b/templates/api_client.mustache @@ -714,6 +714,9 @@ class ApiClient(object): value = data[klass.attribute_map[attr]] kwargs[attr] = self.__deserialize(value, attr_type) + if hasattr(klass, "url") and hasattr(klass, "id"): + kwargs["api_client"] = self + instance = klass(**kwargs) if hasattr(instance, 'get_real_child_model'): diff --git a/templates/model.mustache b/templates/model.mustache index 311238b..89fe15c 100755 --- a/templates/model.mustache +++ b/templates/model.mustache @@ -11,9 +11,7 @@ import six {{#model}} {{#vendorExtensions.x-opsgenie-success-response}} import time -import json -import certifi -import urllib3 +import random import opsgenie_sdk from opsgenie_sdk.models.success_data import SuccessData # noqa: F401,E501 @@ -65,7 +63,7 @@ class {{classname}}(object): } {{/discriminator}} - def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}{{#vendorExtensions.x-opsgenie-success-response}}, url=None{{/vendorExtensions.x-opsgenie-success-response}}): # noqa: E501 + def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}{{#vendorExtensions.x-opsgenie-success-response}}, url=None, api_client=None{{/vendorExtensions.x-opsgenie-success-response}}): # noqa: E501 """{{classname}} - a model defined in OpenAPI""" # noqa: E501 {{#vars}}{{#-first}} {{/-first}} @@ -87,13 +85,11 @@ class {{classname}}(object): {{/isNullable}} {{/required}} {{/vars}}{{#vendorExtensions.x-opsgenie-success-response}} - if url is not None: - self.url = url - else: - self.url = None + self.url = url + self.api_client = api_client self._id = None - self.conf = opsgenie_sdk.configuration.Configuration() + self.conf = api_client.configuration self.logger = self.conf.logger["package_logger"]{{/vendorExtensions.x-opsgenie-success-response}} {{#vars}} @property @@ -222,96 +218,67 @@ class {{classname}}(object): self._url = url def retrieve_result(self): - conf = self.conf - api_key = conf.api_key_prefix.get('Authorization') + ' ' + conf.api_key.get('Authorization') - http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) - headers = { - 'Authorization': api_key - } - - request_url = self._build_request_url_(conf.host) - - attempt_count = 0 - MAX_NUMBER_OF_RETRIES = conf.short_polling_max_retries + domain_api_client = self._get_domain_api_client_() + MAX_NUMBER_OF_RETRIES = self.conf.short_polling_max_retries condition = True response = None + attempt_count = 0 while condition: if attempt_count > 0: - time.sleep(2 * attempt_count * 0.2) + sleep_time = random.uniform(0, (0.2 * 2 ** attempt_count)) + time.sleep(sleep_time) try: - self.logger.debug( - str(attempt_count + 1) + ' attempt to retrieve result with request_id: ' + str(self.request_id)) - response = http.request(method='GET', url=request_url, headers=headers) - except ApiException as err: - print("Exception when calling success_response->retrieve_result: %s\n" % err) - - response_headers = response.headers - if response_headers is not None: - rate_limit_state = response_headers.get('X-RateLimit-State') - status_code = response.status - - if rate_limit_state == 'THROTTLED': - should_retry = True - self.logger.debug('Should retry because X-RateLimit-State is THROTTLED') - elif status_code == 429: - should_retry = True - self.logger.debug('Should retry because Status is 429') - elif 500 <= status_code <= 599: - should_retry = True - self.logger.debug('Should retry because Status is ' + status_code) - else: - should_retry = False - response_body = response.data - response_body_decoded = json.loads(response_body) - response_body_decoded_data = response_body_decoded.get('data') - if response_body_decoded_data is not None: - self._id = response_body_decoded_data.get('alertId') - if self._id is None: - self._id = response_body_decoded_data.get('incidentId') - - attempt_count += 1 - - if should_retry and attempt_count < MAX_NUMBER_OF_RETRIES: - condition = True - else: - condition = False + response = domain_api_client.get_incident_request_status(request_id=self.request_id) + except AttributeError: + response = domain_api_client.get_request_status(request_id=self.request_id) + + if response.data.is_success is True: + should_retry = False + + if hasattr(response.data, "alert_id"): + self._id = response.data.alert_id + elif hasattr(response.data, "incident_id"): + self._id = response.data.incident_id else: + should_retry = True + + if should_retry and attempt_count < MAX_NUMBER_OF_RETRIES: condition = True + else: + condition = False + + attempt_count += 1 return response def retrieve_resulting_action(self): - conf = self.conf - api_key = conf.api_key_prefix.get('Authorization') + ' ' + conf.api_key.get('Authorization') - http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) - headers = { - 'Authorization': api_key - } - if self._id is None: self.retrieve_result() - request_url = self._build_request_url_(conf.host) + domain_api_client = self._get_domain_api_client_() try: - response = http.request(method='GET', url=request_url, headers=headers) + action = domain_api_client.get_alert(identifier=self._id) + except AttributeError: + action = domain_api_client.get_incident(identifier=self._id) - return response - except ApiException as err: - print("Exception when calling success_response->retrieve_result: %s\n" % err) + return action - def _build_request_url_(self, host): + def _get_domain_api_client_(self): if 'alerts' in self.url: - return host + '/v2/alerts/requests/' + self.request_id + return opsgenie_sdk.AlertApi(api_client=self.api_client) elif 'incident' in self.url: - return host + '/v1/incidents/requests/' + self.request_id + return opsgenie_sdk.IncidentApi(api_client=self.api_client) else: raise ApiException(reason='Short polling is not currently supported for this domain') @property def id(self): + if self._id is None: + self.retrieve_result() + return self._id {{/vendorExtensions.x-opsgenie-success-response}} diff --git a/templates/rest.mustache b/templates/rest.mustache index 2d45830..37dc655 100755 --- a/templates/rest.mustache +++ b/templates/rest.mustache @@ -254,6 +254,7 @@ class RESTClientObject(object): exception = exceptions.build_exception(response=r) raise exception + self.retries = -1 return r def GET(self, url, headers=None, query_params=None, _preload_content=True, diff --git a/templates/setup.mustache b/templates/setup.mustache index 227e267..d3a8f16 100755 --- a/templates/setup.mustache +++ b/templates/setup.mustache @@ -18,7 +18,7 @@ VERSION = "{{packageVersion}}" # prerequisite: setuptools # http://pypi.python.org/pypi/setuptools -REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil", "numpy >= 1.16.3", "retry >= 0.9.2"] +REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil", "numpy >= 1.16.3", "retry >= 0.9.2", "setuptools >= 21.0.0"] {{#asyncio}} REQUIRES.append("aiohttp >= 3.0.0") {{/asyncio}}