+
+ def _subscription_mapper(self, _id, data, table):
+ """
+ Performs data transformation on subscription request
+ :param data: data to be trasformed
+ :param table: table in which transformed data are inserted
+ """
+ pass
+
+ def format_subscription(self, subs_data):
+ """
+ Brings lexicographical order for list items at any nested level. For subscriptions max level of nesting is 4.
+ :param subs_data: Subscription data to be ordered.
+ :return: None
+ """
+ if isinstance(subs_data, dict):
+ for key in subs_data.keys():
+ # Base case
+ if isinstance(subs_data[key], list):
+ subs_data[key].sort()
+ return
+ # Recursive case
+ self.format_subscription(subs_data[key])
+ return
+
+ def check_conflict_on_new(self, session, content):
+ """
+ Two subscriptions are equal if Auth username, CallbackUri and filter are same.
+ :param session: Session object.
+ :param content: Subscription data.
+ :return: None if no conflict otherwise, raises an exception.
+ """
+ # Get all subscriptions from db table subscriptions and compare.
+ self.format_subscription(content)
+ filter_dict = {"CallbackUri": content["CallbackUri"]}
+ if content.get("authentication"):
+ if content["authentication"].get("authType") == "basic":
+ filter_dict["authentication.authType"] = "basic"
+ # elif add other authTypes here
+ else:
+ filter_dict["authentication"] = None # For Items without authentication
+ existing_subscriptions = self.db.get_list("subscriptions", q_filter=filter_dict)
+ new_sub_pwd = None
+ if (
+ content.get("authentication")
+ and content["authentication"].get("authType") == "basic"
+ ):
+ new_sub_pwd = content["authentication"]["paramsBasic"]["password"]
+ content["authentication"]["paramsBasic"].pop("password", None)
+ for existing_subscription in existing_subscriptions:
+ sub_id = existing_subscription.pop("_id", None)
+ existing_subscription.pop("_admin", None)
+ existing_subscription.pop("schema_version", None)
+ if (
+ existing_subscription.get("authentication")
+ and existing_subscription["authentication"].get("authType") == "basic"
+ ):
+ existing_subscription["authentication"]["paramsBasic"].pop(
+ "password", None
+ )
+ # self.logger.debug(existing_subscription)
+ if existing_subscription == content:
+ raise EngineException(
+ "Subscription already exists with id: {}".format(sub_id),
+ HTTPStatus.CONFLICT,
+ )
+ if new_sub_pwd:
+ content["authentication"]["paramsBasic"]["password"] = new_sub_pwd
+ return
+