"""Interface to the ows-account microservice.""" from owsrequest import request from collaborator.constants import error, features, service_name from collaborator.utils import owsrequest from collaborator.utils.typing import Account def get_account_metadata(account: Account) -> dict: """Get account's metadata. Args: account (Account): The Account tuple Returns: dict """ account_url = f"/vendor/{account.id}" res = request.get(service_name.OWS_ACCOUNT, account_url) return owsrequest.unwrap_data_or_error(res, error.ERROR_CODE_OWS_ACCOUNT) def feature_control_enabled(account_id: str, feature_id: int) -> bool: """Check if an account has a feature control enabled. Args: account_id (str): The account ID feature_id (int): The feature control ID Returns: bool """ account_url = f"/vendor/{account_id}/features" res = request.get(service_name.OWS_ACCOUNT, account_url) res = owsrequest.unwrap_data_or_error(res, error.ERROR_CODE_OWS_ACCOUNT) feature_controls = res.get("items", []) return len([fc for fc in feature_controls if fc["feature_id"] == feature_id]) > 0 def has_direct_payments(account_id: str) -> bool: """Check if an account has a DP feature control enabled. Args: account_id (str): The account ID Returns: bool """ return feature_control_enabled(account_id, features.DIRECT_PAYMENTS_FEATURE_CONTROL) def get_vendors_for_feature(feature_id: int) -> list: """Get vendors for a feature. Args: feature_id (int): The feature ID Returns: list """ path = f"/vendor/features/{feature_id}?page_limit=0" res = request.get( service_name.OWS_ACCOUNT, path, headers=owsrequest.get_default_ows_headers() ) res = owsrequest.unwrap_data_or_error(res, error.ERROR_CODE_OWS_ACCOUNT) vendors_for_feature = res.get("items", []) return vendors_for_feature def get_subaccount(subaccount_id: str) -> dict: """Get subaccount by ID. Args: subaccount_id (str): The subaccount ID. Returns: dict """ path = f"/subaccount/{subaccount_id}" res = request.get( service_name.OWS_ACCOUNT, path, headers=owsrequest.get_default_ows_headers() ) subaccount = owsrequest.unwrap_data_or_error(res, error.ERROR_CODE_OWS_ACCOUNT) return subaccount