"""Route handlers for Subaccount Bulk endpoints.""" from account.api import app from account.constants import error from account.logic import ( subaccount, ) from account.utils import pagination from account.utils.api_utils import validate_request_data from account.validation.schemas.lookup import ( LookupSubaccountsBySubaccountIds, LookupSubaccountsByUuids, ) from flask import Response, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify @app.route('//subaccounts', methods=['GET']) def get_vendor_subaccounts(vendor_id): """Get list of subaccounts. Gets list of subaccounts given . Args: vendor_id (int): unique identifier for vendor. Returns: Flask.response: contains list of dicts of subaccount information. When a vendor id has no subaccounts, this vendor is considered to not be a D3 and should have no concept of subaccounts. ows-account returns a 404 response to obfuscate the subaccounts concept. """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401 ) ) page = pagination.get_pagination(request) validation = flask_request.verify_grass_access(request, required=False, vendor=vendor_id) if not validation: return flaskify(validation) data = subaccount.get_subaccounts(vendor_id, page_offset=page.offset, page_limit=page.limit) return flaskify(data) @app.route('/subaccounts/names/dataloader', methods=['POST']) def get_subaccounts_names() -> Response: """Handler to get subaccount names in bulk Expects JSON payload with a list of vendor uuids. Example payload: ['uuid1', 'uuid2'] Returns: Flask.response. """ subaccount_uuids = request.get_json() if not isinstance(subaccount_uuids, list) or not all( isinstance(item, str) for item in subaccount_uuids ): return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) try: return flaskify(response.Response(subaccount.get_subaccount_names(subaccount_uuids))) except Exception as e: return flaskify(response.create_fatal_response(e.args)) @app.route('/lookup/subaccounts/uuids/', methods=['POST']) @validate_request_data(LookupSubaccountsByUuids()) def lookup_subaccounts_by_uuids(): """Get list of subaccount ids using their uuids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ schema = LookupSubaccountsByUuids() request_payload = schema.load(request.get_json()) uuids = request_payload['uuids'] fetch_flags = request_payload.get('fetch_flags', []) if not uuids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(subaccount.lookup_subaccounts_by_uuids(uuids, fetch_flags)) @app.route('/lookup/subaccounts/subaccount-ids/', methods=['POST']) @validate_request_data(LookupSubaccountsBySubaccountIds()) def lookup_subaccounts_by_ids(): """Get list of subaccount uuids using their subaccount_ids. NOTE: This endpoint is configured to require NO access rule checks. This is because it is serving as a Policy Information Point for Permission Platform. Do not expose other properties or attributes via this endpoint. Returns: Flask.response. """ schema = LookupSubaccountsBySubaccountIds() request_payload = schema.load(request.get_json()) subaccount_ids = request_payload.get('subaccount_ids', []) fetch_flags = request_payload.get('fetch_flags', []) if not subaccount_ids: return flaskify( response.create_error_response( error.ERROR_CODE_INVALID_INPUT, error.ERROR_MESSAGE_INVALID_REQUEST ) ) return flaskify(subaccount.lookup_subaccounts_by_subaccount_ids(subaccount_ids, fetch_flags))