def _invoke( rest_method, endpoint, token=None, # pylint: disable=too-many-arguments auth_header_type: AuthHeaderType = AuthHeaderType.BEARER, content_type: ContentType = ContentType.JSON, data=None, raise_for_status: bool = True, additional_headers: dict = None, generate_token: bool = True): """Invoke different method depending on the input.""" # just to avoid the duplicate code for PUT and POSt current_app.logger.debug(f'<_invoke-{rest_method}') headers = {'Content-Type': content_type.value} if not token and generate_token: token = _get_token() if token: headers.update( {'Authorization': auth_header_type.value.format(token)}) if additional_headers: headers.update(additional_headers) if content_type == ContentType.JSON: data = json.dumps(data) current_app.logger.debug('Endpoint : {}'.format(endpoint)) current_app.logger.debug('headers : {}'.format(headers)) response = None try: invoke_rest_method = getattr(requests, rest_method) response = invoke_rest_method(endpoint, data=data, headers=headers, timeout=current_app.config.get( 'CONNECT_TIMEOUT', 10)) if raise_for_status: response.raise_for_status() except (ReqConnectionError, ConnectTimeout) as exc: current_app.logger.error('---Error on POST---') current_app.logger.error(exc) raise ServiceUnavailableException(exc) except HTTPError as exc: current_app.logger.error( 'HTTPError on POST with status code {}'.format( response.status_code if response else '')) if response and response.status_code >= 500: raise ServiceUnavailableException(exc) raise exc finally: RestService.__log_response(response) current_app.logger.debug('>post') return response
def post( endpoint, token=None, # pylint: disable=too-many-arguments auth_header_type: AuthHeaderType = AuthHeaderType.BEARER, content_type: ContentType = ContentType.JSON, data=None, raise_for_status: bool = True): """POST service.""" current_app.logger.debug('<post') headers = { 'Authorization': auth_header_type.value.format(token), 'Content-Type': content_type.value } if content_type == ContentType.JSON: data = json.dumps(data) current_app.logger.debug('Endpoint : {}'.format(endpoint)) current_app.logger.debug('headers : {}'.format(headers)) # current_app.logger.debug('data : {}'.format(data)) response = None try: response = requests.post( endpoint, data=data, headers=headers, timeout=current_app.config.get('CONNECT_TIMEOUT')) if raise_for_status: response.raise_for_status() except (ReqConnectionError, ConnectTimeout) as exc: current_app.logger.error('---Error on POST---') current_app.logger.error(exc) raise ServiceUnavailableException(exc) except HTTPError as exc: current_app.logger.error( 'HTTPError on POST with status code {}'.format( response.status_code if response else '')) if response and response.status_code >= 500: raise ServiceUnavailableException(exc) raise exc finally: current_app.logger.debug( response.headers if response else 'Empty Response Headers') current_app.logger.info( 'response : {}'.format(response.text if response else '')) current_app.logger.debug('>post') return response
def get( endpoint, token=None, # pylint: disable=too-many-arguments auth_header_type: AuthHeaderType = AuthHeaderType.BEARER, content_type: ContentType = ContentType.JSON, retry_on_failure: bool = False, additional_headers: Dict = None): """GET service.""" current_app.logger.debug('<GET') headers = {'Content-Type': content_type.value} if additional_headers: headers.update(additional_headers) if token: headers['Authorization'] = auth_header_type.value.format(token) current_app.logger.debug('Endpoint : {}'.format(endpoint)) current_app.logger.debug('headers : {}'.format(headers)) session = requests.Session() if retry_on_failure: session.mount(endpoint, RETRY_ADAPTER) response = None try: response = session.get( endpoint, headers=headers, timeout=current_app.config.get('CONNECT_TIMEOUT')) response.raise_for_status() except (ReqConnectionError, ConnectTimeout) as exc: current_app.logger.error('---Error on GET---') current_app.logger.error(exc) raise ServiceUnavailableException(exc) except HTTPError as exc: current_app.logger.error( 'HTTPError on GET with status code {}'.format( response.status_code if response else '')) if response and response.status_code >= 500: raise ServiceUnavailableException(exc) raise exc finally: current_app.logger.debug( response.headers if response else 'Empty Response Headers') current_app.logger.info( 'response : {}'.format(response.text if response else '')) current_app.logger.debug('>GET') return response