def get_session_id(): session_id = "" try: app.logger.info("creating new session") create_session_request = requests.get( url=app.config["CREATE_SESSION_URL"]) create_session_request.raise_for_status() if HTTPStatus(create_session_request.status_code) is HTTPStatus.OK: session_id = create_session_request.cookies.get("session_id") g.session_id_override = session_id app.logger.info("session created", {'session_id': session_id}) return session_id except requests.exceptions.RequestException as exc: exception_type = type(exc).__name__ try: status_code = exc.response.status_code or "not_applicable" except AttributeError: status_code = "not_applicable" log_safe_exception(exc) raise NDOP_RequestError( f'session creation failed, original exception type {exception_type}, response status code {status_code}' )
def resend_code_by_pds(session_id): result = constants.UNKNOWN_RESULT headers = {"Content-type": "application/json"} cookies = dict(session_id=session_id) try: pds_search_request = requests.get( url=current_app.config["PDS_RESEND_CODE_URL"], headers=headers, cookies=cookies) pds_search_request.raise_for_status() if pds_search_request.status_code is HTTPStatus.OK.value: ret = pds_search_request.json() if ret.get("resend_count"): return ret.get("resend_count") return result except requests.exceptions.RequestException as exc: if isinstance(exc, requests.exceptions.HTTPError) \ and exc.response.status_code == HTTPStatus.NOT_ACCEPTABLE.value: return constants.RESEND_CODE_MAX_EXCEEDED exception_type = type(exc).__name__ status_code = exc.response.status_code or "not_applicable" log_safe_exception(exc) raise NDOP_RequestError( f'resend code failed, original exception type {exception_type}, response status code {status_code}' )
def check_status_of_pds_search_result(session_id): search_result = "" headers = {"Content-type": "application/json"} cookies = dict(session_id=session_id) try: app.logger.info("submitting pds search result request") pds_search_result_request = requests.get( url=app.config["PDS_SEARCH_RESULT_URL"], headers=headers, cookies=cookies, timeout=app.config["PDS_REQUEST_TIMEOUT"], ) pds_search_result_request.raise_for_status() if HTTPStatus(pds_search_result_request.status_code) is HTTPStatus.OK: result = pds_search_result_request.json() # TODO: Remove functional side effects of writing to session if result.get("sms"): session["sms"] = result.get("sms") if result.get("email"): session["email"] = result.get("email") search_result = result.get("search_result") app.logger.info("pds search result response received", {"search_result": search_result}) return search_result except requests.exceptions.RequestException as exc: if isinstance(exc, requests.exceptions.Timeout): return constants.PDS_REQUEST_TIMEOUT if isinstance(exc, requests.exceptions.HTTPError) \ and HTTPStatus(exc.response.status_code) == HTTPStatus.UNAUTHORIZED: return "invalid_user" if isinstance(exc, requests.exceptions.HTTPError) \ and HTTPStatus(exc.response.status_code) == HTTPStatus.UNPROCESSABLE_ENTITY: return "insufficient_data" if isinstance(exc, requests.exceptions.HTTPError) \ and HTTPStatus(exc.response.status_code) == HTTPStatus.NOT_ACCEPTABLE: return "age_restriction_error" exception_type = type(exc).__name__ status_code = exc.response.status_code or "not_applicable" log_safe_exception(exc) raise NDOP_RequestError( f'pds search result request failed, original exception type {exception_type}, response status code {status_code}' )
def request_code_by_pds(session_id, otp_delivery_type): result = constants.OTP_REQUEST_FAILURE json_content = json.dumps({'otp_delivery_type': str(otp_delivery_type)}) headers = {"Content-type": "application/json"} cookies = dict(session_id=session_id) try: pds_search_request = requests.post( url=current_app.config["PDS_REQUEST_CODE_URL"], headers=headers, data=json_content, cookies=cookies) pds_search_request.raise_for_status() if pds_search_request.status_code is HTTPStatus.OK.value: result = constants.OTP_REQUEST_SUCCESS return result except requests.exceptions.RequestException as exc: if isinstance(exc, requests.exceptions.HTTPError) \ and exc.response.status_code == 406: ret = exc.response.json() if ret.get("error") == \ constants.OTP_REQUEST_MAX_RETRIES: return constants.OTP_REQUEST_MAX_RETRIES return constants.OTP_REQUEST_FAILURE exception_type = type(exc).__name__ status_code = exc.response.status_code or "not_applicable" log_safe_exception(exc) raise NDOP_RequestError( f'request code failed, original exception type {exception_type}, response status code {status_code}' )
def handle_unexpected_error(exception): log_safe_exception(exception) return redirect_to_route('main.generic_error')