def _service_caller(self, method, url, headers, data=None): try: kwargs = {'timeout': self.timeout, 'headers': headers} if data: kwargs.update({"data": data}) response = self.breaker.call(method, url, **kwargs) if not response or response.status_code != 200: logging.getLogger(__name__).error( 'COTS cause message sub-service, Invalid response, ' 'status_code: {}'.format(response.status_code)) if response.status_code == 401: raise UnauthorizedOnSubService( 'Unauthorized on COTS message sub-service {} {}'. format(method, url)) raise ObjectNotFound('non 200 response on COTS cause message ' 'sub-service {} {}'.format(method, url)) return response except pybreaker.CircuitBreakerError as e: logging.getLogger(__name__).error( 'COTS cause message sub-service dead (error: {})'.format(e)) raise SubServiceError( 'COTS cause message sub-service circuit breaker open') except requests.Timeout as t: logging.getLogger(__name__).error( 'COTS cause message sub-service timeout (error: {})'.format(t)) raise SubServiceError('COTS cause message sub-service timeout') except (UnauthorizedOnSubService, ObjectNotFound): raise # Do not change exceptions that were just raised except Exception as e: logging.getLogger(__name__).exception( 'COTS cause message sub-service handling ' 'error : {}'.format(str(e))) raise SubServiceError(str(e))
def _service_caller(self, method, url, headers, data=None): try: kwargs = {"timeout": self.timeout, "headers": headers} if data: kwargs.update({"data": data}) response = method(url, **kwargs) if not response or response.status_code != 200: logging.getLogger(__name__).error( "COTS cause message sub-service, Invalid response, " "status_code: {}".format(response.status_code)) if response.status_code == 401: raise UnauthorizedOnSubService( "Unauthorized on COTS message sub-service {} {}". format(method, url)) raise ObjectNotFound( "non 200 response on COTS cause message sub-service {} {}". format(method, url)) return response except requests.Timeout as t: logging.getLogger(__name__).error( "COTS cause message sub-service timeout (error: {})".format(t)) raise SubServiceError("COTS cause message sub-service timeout") except (UnauthorizedOnSubService, ObjectNotFound): raise # Do not change exceptions that were just raised except Exception as e: logging.getLogger(__name__).exception( "COTS cause message sub-service handling error : {}".format( six.text_type(e))) raise SubServiceError(six.text_type(e))
def _call_webservice(self): access_token = self._get_access_token() if access_token is None: raise SubServiceError( 'Impossible to get a token for COTS cause message sub-service') headers = { 'X-API-Key': self.api_key, 'Authorization': 'Bearer {}'.format(access_token) } resp = self._service_caller(method=requests.get, url=self.resource_server, headers=headers) messages = {} if not resp: return messages for m in resp.json(): if 'id' in m and 'labelExt' in m: messages[m['id']] = m['labelExt'] return messages
def _call_webservice(self): access_token = self._get_access_token() if access_token is None: raise SubServiceError( "Impossible to get a token for COTS cause message sub-service") headers = { "X-API-Key": self.api_key, "Authorization": "Bearer {}".format(access_token) } resp = self._service_caller(method=requests.get, url=self.resource_server, headers=headers) messages = {} if not resp: return messages for m in resp.json(): if "id" in m and "labelExt" in m: messages[m["id"]] = m["labelExt"] return messages