def post( endpoint, token, auth_header_type: AuthHeaderType, # pylint: disable=too-many-arguments content_type: ContentType, data, raise_for_error: bool = True, additional_headers: Dict = None, is_put: bool = False): """POST service.""" current_app.logger.debug('<post') headers = { 'Authorization': auth_header_type.value.format(token), 'Content-Type': content_type.value } if additional_headers: headers.update(additional_headers) if content_type == ContentType.JSON: data = json.dumps(data) current_app.logger.debug(f'Endpoint : {endpoint}') current_app.logger.debug(f'headers : {headers}') current_app.logger.debug(f'data : {data}') response = None try: if is_put: response = requests.put( endpoint, data=data, headers=headers, timeout=current_app.config.get('CONNECT_TIMEOUT')) else: response = requests.post( endpoint, data=data, headers=headers, timeout=current_app.config.get('CONNECT_TIMEOUT')) if raise_for_error: response.raise_for_status() except (ReqConnectionError, ConnectTimeout) as exc: current_app.logger.error('---Error on POST---') current_app.logger.error(exc) raise ServiceUnavailableException(exc) from exc except HTTPError as exc: current_app.logger.error( f"HTTPError on POST with status code {response.status_code if response else ''}" ) if response and response.status_code >= 500: raise ServiceUnavailableException(exc) from exc raise exc finally: OAuthService.__log_response(response) current_app.logger.debug('>post') return response
def get( endpoint, token, auth_header_type: AuthHeaderType, # pylint:disable=too-many-arguments content_type: ContentType, retry_on_failure: bool = False, return_none_if_404: bool = False, additional_headers: Dict = None): """GET service.""" current_app.logger.debug('<GET') headers = { 'Authorization': auth_header_type.value.format(token), 'Content-Type': content_type.value } if additional_headers is not None: headers.update(additional_headers) current_app.logger.debug(f'Endpoint : {endpoint}') current_app.logger.debug(f'headers : {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 POST---') current_app.logger.error(exc) raise ServiceUnavailableException(exc) from exc except HTTPError as exc: current_app.logger.error( f"HTTPError on POST with status code {response.status_code if response else ''}" ) if response is not None: if response.status_code >= 500: raise ServiceUnavailableException(exc) from exc if return_none_if_404 and response.status_code == 404: return None raise exc finally: OAuthService.__log_response(response) current_app.logger.debug('>GET') return response
def test_update_online_banking_account_when_cfs_down(session, client, jwt, app): """Assert that the payment records are created with 200, as there is no CFS update.""" token = jwt.create_jwt(get_claims(role=Role.SYSTEM.value), token_header) headers = { 'Authorization': f'Bearer {token}', 'content-type': 'application/json' } rv = client.post( '/api/v1/accounts', data=json.dumps( get_basic_account_payload( payment_method=PaymentMethod.ONLINE_BANKING.value)), headers=headers) auth_account_id = rv.json.get('accountId') # Mock ServiceUnavailableException with patch('pay_api.services.oauth_service.OAuthService.post', side_effect=ServiceUnavailableException( ConnectionError('mocked error'))): rv = client.put( f'/api/v1/accounts/{auth_account_id}', data=json.dumps( get_basic_account_payload( payment_method=PaymentMethod.ONLINE_BANKING.value)), headers=headers) assert rv.status_code == 202
def test_create_pad_account_when_cfs_down(session, client, jwt, app): """Assert that the payment records are created with 202.""" token = jwt.create_jwt(get_claims(role=Role.SYSTEM.value), token_header) headers = {'Authorization': f'Bearer {token}', 'content-type': 'application/json'} # Mock ServiceUnavailableException with patch('pay_api.services.oauth_service.OAuthService.post', side_effect=ServiceUnavailableException(ConnectionError('mocked error'))): rv = client.post('/api/v1/accounts', data=json.dumps(get_unlinked_pad_account_payload()), headers=headers) assert rv.status_code == 202
def get(endpoint, token, auth_header_type: AuthHeaderType, content_type: ContentType, retry_on_failure: bool = False): """GET service.""" current_app.logger.debug('<GET') headers = { 'Authorization': auth_header_type.value.format(token), 'Content-Type': content_type.value } 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 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 current_app.logger.info( 'response : {}'.format(response.text if response else '')) current_app.logger.debug('>GET') return response
def post(endpoint, token, auth_header_type: AuthHeaderType, content_type: ContentType, data): """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')) 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 current_app.logger.debug(response.headers if response else '') current_app.logger.info( 'response : {}'.format(response.text if response else '')) current_app.logger.debug('>post') return response