def get_target_host(server_domain, cluster, client, secure): """Construct Target server hostname""" scheme_prefix = SCHEME.get("HTTP") if secure is False else SCHEME.get("HTTPS") if is_string(cluster): return "{}{}{}.{}".format(scheme_prefix, EDGE_CLUSTER_PREFIX, cluster, HOST) if is_string(server_domain): return "{}{}".format(scheme_prefix, server_domain) return "{}{}.{}".format(scheme_prefix, client, HOST)
def valid_tnt_id(tnt_id=None): """ :param tnt_id: (str) tntId :return: (str) tntId without the location hint """ if tnt_id and is_string(tnt_id): return tnt_id.split(".")[0] return None
def valid_notification(notification): """Checks for valid notification""" is_valid = notification and is_string(notification.id) \ and is_int(notification.timestamp) \ and notification.type in METRIC_TYPES if not is_valid: logger.error("{}\n{}".format(MESSAGES.get("NOTIFICATION_INVALID"), notification.to_str())) return is_valid
def get_lower_case_attributes(obj): """Put lowercase versions of object attributes onto the object :param obj: (dict) dict to traverse, required :return: (dict) new dict with lowercase versions of all the string attributes from original dict """ result = {} for key in obj.keys(): result["{}_lc".format(key)] = obj[key].lower() if is_string( obj[key]) else obj[key] return result
def get_session_id(cookies, user_session_id, uuid_method=create_uuid): """Get session ID from cookie""" cookie = cookies.get(SESSION_ID_COOKIE, {}) value = cookie.get("value") if is_string(value): return value if user_session_id: return user_session_id return uuid_method()
def valid_visitor_id(visitor_id, target_location_hint): """ :param visitor_id: (delivery_api_client.Model.visitor_id.VisitorId) visitor ID object :param target_location_hint: (str) Target location hint :return: (delivery_api_client.Model.visitor_id.VisitorId) updated copy of visitor ID object """ result = deepcopy(visitor_id) if visitor_id else VisitorId() if _no_ids(result): location_hint = ".{}_0".format(target_location_hint) if target_location_hint \ and is_string(target_location_hint) else "" result.tnt_id = "{}{}".format(create_uuid(), location_hint) return result
def _create_url_context(url): """Create URL context :param url: (str) URL :return: (target_decisioning_engine.types.decisioning_context.PageContext) Page context """ if not url or not is_string(url): url = "" url_attributes = parse_url(url) lc_attributes = get_lower_case_attributes(url_attributes) url_attributes.update(lc_attributes) return PageContext(**url_attributes)
def compute_allocation(client_id, activity_id, visitor_id, salt=CAMPAIGN_BUCKET_SALT): """ :param client_id: (str) client ID :param activity_id: (str) activity ID :param visitor_id: (str | delivery_api_client.Model.visitor_id.VisitorId) visitor ID :param salt: (str) hashing salt :return: (float) allocation value """ device_id = ".".join([ client_id, str(activity_id), visitor_id if visitor_id and is_string(visitor_id) else get_or_create_visitor_id(visitor_id), salt ]) return calculate_allocation_memoized(device_id)
def initialize(self): """Initialize ArtifactProvider and fetch initial artifact""" self.polling_interval = self._get_polling_interval() self.artifact_location = self.config.artifact_location if is_string(self.config.artifact_location) else \ determine_artifact_location(self.config) try: self.artifact = self._get_initial_artifact() self.artifact_tracer = ArtifactTracer( self.artifact_location, self.config.artifact_payload, self.polling_interval, self.polling_halted, self.artifact ) self.subscribe(self._artifact_tracer_update) finally: self._schedule_next_update()
def add_campaign_macro_values(html_content, rule, request_detail): """ :param html_content: (str) html offer content :param rule: (target_decisioning_engine.types.decisioning_artifact.Rule) rule :param request_detail: (delivery_api_client.Model.request_details.RequestDetails) request details :return: (str) Returns html content with updated campaign content """ if not html_content or not is_string(html_content): return html_content def replace_match(match): """ :param match: (MatchObject) match found by regex pattern :return: (str) Returns replacement string """ def replace_group_match(group_match): """ :param group_match: (MatchObject) match found by regex pattern :return: (str) Returns replacement string """ return MACRO_NAME_REPLACEMENTS[group_match.group(0)] if not match or not len(match.groups()) > 0: return match.group(0) # entire match macro_key = match.group(1) parts = re.sub(MACRO_NAME_REPLACEMENTS_REGEX, replace_group_match, macro_key, flags=re.IGNORECASE).split(".") if len(parts) > 2: parts = parts[len(parts) - 2:] filtered = [part for part in parts if part not in MACRO_NAME_REMOVALS] key = ".".join(filtered) parameters = request_detail.parameters if request_detail and request_detail.parameters else {} for item in [rule.get("meta"), request_detail, parameters]: replacement = get_value_from_object(item, key) if replacement is not None: return str(replacement) return match.group(0) return re.sub(MACRO_PATTERN_REGEX, replace_match, html_content, flags=re.IGNORECASE)
def parse_url(url): """parse url""" if not is_string(url): return _get_default_parsed_url(EMPTY_STRING) parsed = get_tld(url, as_object=True, fail_silently=True) if not parsed: return _get_default_parsed_url(url) path, query, fragment = [getattr(parsed.parsed_url, key, "") for key in ["path", "query", "fragment"]] return { "url": url, "path": path, "query": query, "fragment": fragment, "domain": parsed.domain, "subdomain": parsed.subdomain, "topLevelDomain": parsed.tld }
def __init__(self, config, target_options, artifact_trace): """ :param config: (target_decisioning_engine.types.decisioning_config.DecisioningConfig) config :param target_options: (target_decisioning_engine.types.target_delivery_request.TargetDeliveryRequest) options :param artifact_trace: (dict) artifact trace """ self.config = config self.target_options = target_options self.artifact_trace = artifact_trace self.client_code = config.client self.session_id = target_options.session_id self.request = target_options.request self.show_traces = self.request.trace is not None self.profile = None tnt_id_parts = self.request.id.tnt_id.split(".") if self.request.id and is_string(self.request.id.tnt_id) \ else [None, None] tnt_id = tnt_id_parts[0] profile_location = tnt_id_parts[1] if len(tnt_id_parts) >= 2 else None visitor_id = to_dict(self.request.id) visitor_id["tntId"] = tnt_id visitor_id["profileLocation"] = profile_location self.profile = {"visitorId": visitor_id}
def create_property(_property): """Validate DeliveryRequest property object""" return _property if _property and _property.token and is_string(_property.token) else None
def assert_string(value): """Throws AssertionError if value is not a str""" assert is_string(value), "'{}' is not a string".format(value)