def test_request_no_json(success_codes_no_json): from framework.core.requests import safe_json_request responses.add(success_codes_no_json['resp']) status_code, js = safe_json_request(method=success_codes_no_json['method'], url=url) assert 200 <= status_code < 300 assert js == dict(content=success_body)
def test_request_failure_codes_no_json(error_codes_no_json): from framework.core.requests import safe_json_request responses.add(error_codes_no_json['resp']) status_code, js = safe_json_request(method=error_codes_no_json['method'], url=url) assert 400 <= status_code < 600 assert js == dict(content=error_body)
def get_service_access_token(service_name, client_id=None, client_secret=None, refresh=False): from framework.core.settings import get_app_settings from framework.core.requests import safe_json_request app_settings = get_app_settings() if client_id is None or client_secret is None: client_id = app_settings['client_id'] client_secret = app_settings['client_secret'] token = check_token_cache(service_name=service_name, client_id=client_id, client_secret=client_secret) if token is None or refresh: body = dict(client_id=client_id, client_secret=client_secret, audience=get_audience(service_name=service_name), grant_type="client_credentials") status_code, js = safe_json_request(url=app_settings['auth_url'], method='POST', json=body) if status_code < 300: token = cache_token(token_response=js, service_name=service_name, client_id=client_id, client_secret=client_secret) return token
def get_auth_keys(): from framework.core.requests import safe_json_request from framework.core.settings import get_app_settings app_settings = get_app_settings() global __auth_keys__ if __auth_keys__ is None: status_code, js = safe_json_request(method='GET', url=app_settings['auth_keys_url']) if js: __auth_keys__ = js['keys'] return __auth_keys__
def get_user_scopes(user_token, service_name=None): from framework.core.requests import safe_json_request from framework.core.settings import get_app_settings app_settings = get_app_settings() scopes = [] audience = get_audience(service_name=service_name) status_code, js = safe_json_request( method='POST', url=app_settings['user_scopes_api'], headers=generate_oauth_headers(access_token=user_token), json=dict(audience=audience, scopes=[])) if status_code == 200: scopes = js['response'].get('available_scopes', []) return " ".join(scopes)
def handle_token_request(user, body): from connexion import request from framework.core.settings import get_app_settings from framework.core.requests import safe_json_request app_settings = get_app_settings() js = dict(client_id=user, client_secret=request.authorization.password, audience=get_audience(), **body) status_code, js = safe_json_request(url=app_settings['auth_url'], method='POST', json=js) return js, status_code
def test_request_retries_server_error_automatically(request_methods): from framework.core.requests import safe_json_request responses.add( responses.Response(method=request_methods['response'], url=url, body=error_body, status=500)) responses.add( responses.Response(method=request_methods['response'], url=url, json=test_json, status=200)) status_code, js = safe_json_request(method=request_methods['response'], url=url) assert status_code == 200
def test_request_timeout_return_code_empty_dict(request_methods): from framework.core.requests import safe_json_request status_code, js = safe_json_request(method=request_methods['response'], url=url) assert status_code is None assert js == dict()