def feature_flags(request: HttpRequest) -> Dict[str, Any]: feature_flags_data = {"flags_enabled": [], "has_malformed_json": False} try: data_from_request = load_data_from_request(request) data = data_from_request["data"] except (json.decoder.JSONDecodeError, TypeError): feature_flags_data["has_malformed_json"] = True return feature_flags_data if not data: return feature_flags_data token = _get_token(data, request) is_personal_api_key = False if not token: token = PersonalAPIKeyAuthentication.find_key( request, data_from_request["body"], data if isinstance(data, dict) else None ) is_personal_api_key = True if not token: return feature_flags_data team = Team.objects.get_cached_from_token(token, is_personal_api_key) flags_enabled = [] feature_flags = FeatureFlag.objects.filter(team=team, active=True, deleted=False) for feature_flag in feature_flags: # distinct_id will always be a string, but data can have non-string values ("Any") if feature_flag.distinct_id_matches(data["distinct_id"]): flags_enabled.append(feature_flag.key) feature_flags_data["flags_enabled"] = flags_enabled return feature_flags_data
def get_team_from_token(request: HttpRequest, data_from_request: Dict[str, Any]) -> Union[Team, None]: data = data_from_request["data"] if not data: return None token = _get_token(data, request) is_personal_api_key = False if not token: token = PersonalAPIKeyAuthentication.find_key( request, data_from_request["body"], data if isinstance(data, dict) else None ) is_personal_api_key = True if token: return Team.objects.get_team_from_token(token, is_personal_api_key) return None
def get_event(request): now = timezone.now() try: data_from_request = load_data_from_request(request) data = data_from_request["data"] except TypeError: return cors_response( request, JsonResponse( { "code": "validation", "message": "Malformed request data. Make sure you're sending valid JSON.", }, status=400, ), ) if not data: return cors_response( request, JsonResponse( { "code": "validation", "message": "No data found. Make sure to use a POST request when sending the payload in the body of the request.", }, status=400, ), ) sent_at = _get_sent_at(data, request) token = _get_token(data, request) is_personal_api_key = False if not token: token = PersonalAPIKeyAuthentication.find_key( request, data_from_request["body"], data if isinstance(data, dict) else None) is_personal_api_key = True if not token: return cors_response( request, JsonResponse( { "code": "validation", "message": "Neither api_key nor personal_api_key set. You can find your project API key in PostHog project settings.", }, status=400, ), ) team = Team.objects.get_team_from_token(token, is_personal_api_key) if team is None: return cors_response( request, JsonResponse( { "code": "validation", "message": "Project or personal API key invalid. You can find your project API key in PostHog project settings.", }, status=400, ), ) if isinstance(data, dict): if data.get("batch"): # posthog-python and posthog-ruby data = data["batch"] assert data is not None elif "engage" in request.path_info: # JS identify call data["event"] = "$identify" # make sure it has an event name if isinstance(data, list): events = data else: events = [data] for event in events: try: distinct_id = _get_distinct_id(event) except KeyError: return cors_response( request, JsonResponse( { "code": "validation", "message": "You need to set user distinct ID field `distinct_id`.", "item": event, }, status=400, ), ) if "event" not in event: return cors_response( request, JsonResponse( { "code": "validation", "message": "You need to set event name field `event`.", "item": event, }, status=400, ), ) if check_ee_enabled(): process_event_ee.delay( distinct_id=distinct_id, ip=get_ip_address(request), site_url=request.build_absolute_uri("/")[:-1], data=event, team_id=team.id, now=now, sent_at=sent_at, ) # log the event to kafka write ahead log for processing log_event( distinct_id=distinct_id, ip=get_ip_address(request), site_url=request.build_absolute_uri("/")[:-1], data=event, team_id=team.id, now=now, sent_at=sent_at, ) else: process_event.delay( distinct_id=distinct_id, ip=get_ip_address(request), site_url=request.build_absolute_uri("/")[:-1], data=event, team_id=team.id, now=now, sent_at=sent_at, ) return cors_response(request, JsonResponse({"status": 1}))
def get_event(request): timer = statsd.Timer("%s_posthog_cloud" % (settings.STATSD_PREFIX, )) timer.start() now = timezone.now() try: data_from_request = load_data_from_request(request) data = data_from_request["data"] except TypeError: return cors_response( request, JsonResponse( { "code": "validation", "message": "Malformed request data. Make sure you're sending valid JSON.", }, status=400, ), ) if not data: return cors_response( request, JsonResponse( { "code": "validation", "message": "No data found. Make sure to use a POST request when sending the payload in the body of the request.", }, status=400, ), ) sent_at = _get_sent_at(data, request) token = _get_token(data, request) is_personal_api_key = False if not token: token = PersonalAPIKeyAuthentication.find_key( request, data_from_request["body"], data if isinstance(data, dict) else None) is_personal_api_key = True if not token: return cors_response( request, JsonResponse( { "code": "validation", "message": "Neither api_key nor personal_api_key set. You can find your project API key in PostHog project settings.", }, status=400, ), ) team = Team.objects.get_team_from_token(token, is_personal_api_key) if team is None: return cors_response( request, JsonResponse( { "code": "validation", "message": "Project or personal API key invalid. You can find your project API key in PostHog project settings.", }, status=400, ), ) if isinstance(data, dict): if data.get("batch"): # posthog-python and posthog-ruby data = data["batch"] assert data is not None elif "engage" in request.path_info: # JS identify call data["event"] = "$identify" # make sure it has an event name if isinstance(data, list): events = data else: events = [data] for event in events: try: distinct_id = _get_distinct_id(event) except KeyError: return cors_response( request, JsonResponse( { "code": "validation", "message": "You need to set user distinct ID field `distinct_id`.", "item": event, }, status=400, ), ) if "event" not in event: return cors_response( request, JsonResponse( { "code": "validation", "message": "You need to set event name field `event`.", "item": event, }, status=400, ), ) if check_ee_enabled(): process_event_ee( distinct_id=distinct_id, ip=get_ip_address(request), site_url=request.build_absolute_uri("/")[:-1], data=event, team_id=team.id, now=now, sent_at=sent_at, ) else: task_name = "posthog.tasks.process_event.process_event" celery_queue = settings.CELERY_DEFAULT_QUEUE if team.plugins_opt_in: task_name += "_with_plugins" celery_queue = settings.PLUGINS_CELERY_QUEUE celery_app.send_task( name=task_name, queue=celery_queue, args=[ distinct_id, get_ip_address(request), request.build_absolute_uri("/")[:-1], event, team.id, now.isoformat(), sent_at, ], ) if check_ee_enabled() and settings.LOG_TO_WAL: # log the event to kafka write ahead log for processing log_event( distinct_id=distinct_id, ip=get_ip_address(request), site_url=request.build_absolute_uri("/")[:-1], data=event, team_id=team.id, now=now, sent_at=sent_at, ) timer.stop("event_endpoint") return cors_response(request, JsonResponse({"status": 1}))