"""Orchard Features. This package takes HTTP request headers as a parameter and returns either a single feature or all features in a user context. """ from owsrequest import error_response from owsrequest.constants import headers as header_constants from owsrequest.context import RequestContext from pythonfeatures.logic import features as features_logic def get_all_features(request_context: RequestContext): """Get all features in the context of the user specified by headers. Args: request_context (RequestContext): Instance of owsrequest.context.RequestContext. Returns: dict: The dict of all features with user context. """ if type(request_context).__name__ != 'RequestContext': raise TypeError('request_context must be class RequestContext') if request_context.context_type == header_constants.CONTEXT_TYPE_ERROR: return error_response.create_error_incomplete_profile_headers() if request_context.context_type == header_constants.CONTEXT_TYPE_PROFILE: return features_logic.get_all_features_with_profile_context( request_context.profile_type, request_context.profile_id, request_context.identity_id) orchard_user_id = None vendor_id = None subaccount_id = None identity_id = None account_type = request_context.profile_type account_id = request_context.profile_id if request_context.context_type == header_constants.CONTEXT_TYPE_ACCOUNT: orchard_user_id = request_context.orchard_user_id identity_id = request_context.identity_id if account_type == header_constants.GRASS_ACCOUNT_TYPE_VENDOR: vendor_id = account_id elif account_type == header_constants.GRASS_ACCOUNT_TYPE_SUBACCOUNT: subaccount_id = account_id if request_context.context_type == header_constants.CONTEXT_TYPE_NONE: return features_logic.get_all_features_with_account_context( None, subaccount_id, vendor_id) else: return features_logic.get_all_features_with_account_context( orchard_user_id, subaccount_id, vendor_id, identity_id) def get_single_feature(feature_name, request_context): """Get single feature in the context of the user specified by headers. Args: feature_name (str): the name of the feature eg: ows_features_example request_context (RequestContext): Instance of owsrequest.context.RequestContext. Returns: str: The state of the feature given user context (control/enabled). """ if type(request_context).__name__ != 'RequestContext': raise TypeError('request_context must be class RequestContext') if request_context.context_type == header_constants.CONTEXT_TYPE_ERROR: return error_response.create_error_incomplete_profile_headers() if request_context.context_type == header_constants.CONTEXT_TYPE_PROFILE: return features_logic.get_single_feature_with_profile_context( feature_name, request_context.profile_type, request_context.profile_id, request_context.identity_id) orchard_user_id = None vendor_id = None subaccount_id = None identity_id = None account_type = request_context.profile_type account_id = request_context.profile_id if request_context.context_type == header_constants.CONTEXT_TYPE_ACCOUNT: orchard_user_id = request_context.orchard_user_id identity_id = request_context.identity_id if account_type == header_constants.GRASS_ACCOUNT_TYPE_VENDOR: vendor_id = account_id elif account_type == header_constants.GRASS_ACCOUNT_TYPE_SUBACCOUNT: subaccount_id = account_id if request_context.context_type == header_constants.CONTEXT_TYPE_NONE: return features_logic.get_single_feature_with_account_context( feature_name, None, subaccount_id, vendor_id) else: return features_logic.get_single_feature_with_account_context( feature_name, orchard_user_id, subaccount_id, vendor_id, identity_id) def get_single_feature_by_attributes(feature_name, attributes): """Get single feature with given attributes. Args: feature_name (str): the name of the feature eg: ows_features_example attributes(dict): parameter to filter features based on split rules eg: {'user_id': 'oa:562'}, {'user_id': 'alw:46997'}, {'vendor_id': 23904}, {'identity_id': '5c548bd462501638ea3ed444'} """ return features_logic.get_single_feature_by_attributes( feature_name, attributes)