Exemplo n.º 1
0
def track_request_influxdb(request):
    """
    Sends API event data to InfluxDB

    :param request: (HttpRequest) the request being made
    """
    resource = get_resource_from_uri(request.path)

    if resource and resource in TRACKED_RESOURCE_ACTIONS:
        environment = Environment.get_from_cache(
            request.headers.get("X-Environment-Key"))
        if environment is None:
            return

        tags = {
            "resource": resource,
            "organisation": environment.project.organisation.get_unique_slug(),
            "organisation_id": environment.project.organisation_id,
            "project": environment.project.name,
            "project_id": environment.project_id,
        }

        influxdb = InfluxDBWrapper("api_call")
        influxdb.add_data_point("request_count", 1, tags=tags)
        influxdb.write()
Exemplo n.º 2
0
    def test_get_from_cache_returns_None_if_no_matching_environment(self):
        # Given
        api_key = "no-matching-env"

        # When
        env = Environment.get_from_cache(api_key)

        # Then
        assert env is None
Exemplo n.º 3
0
    def authenticate(self, request):
        try:
            environment = Environment.get_from_cache(
                request.META.get('HTTP_X_ENVIRONMENT_KEY'))
        except Environment.DoesNotExist:
            raise exceptions.AuthenticationFailed(
                'Invalid or missing Environment Key')

        if not self._can_serve_flags(environment):
            raise exceptions.AuthenticationFailed(
                'Organisation is disabled from serving flags.')

        request.environment = environment
Exemplo n.º 4
0
    def authenticate(self, request):
        try:
            api_key = request.META.get("HTTP_X_ENVIRONMENT_KEY")
            environment = Environment.get_from_cache(api_key)
        except Environment.DoesNotExist:
            raise AuthenticationFailed("Invalid or missing Environment Key")

        if not self._can_serve_flags(environment):
            raise AuthenticationFailed("Organisation is disabled from serving flags.")

        request.environment = environment

        # DRF authentication expects a two tuple to be returned containing User, auth
        return None, None
Exemplo n.º 5
0
    def test_get_from_cache_stores_environment_in_cache_on_success(
            self, mock_cache):
        # Given
        self.environment.save()
        mock_cache.get.return_value = None

        # When
        environment = Environment.get_from_cache(self.environment.api_key)

        # Then
        assert environment == self.environment
        mock_cache.set.assert_called_with(self.environment.api_key,
                                          self.environment,
                                          timeout=60)
Exemplo n.º 6
0
def track_request_googleanalytics(request):
    """
    Utility function to track a request to the API with the specified URI

    :param request: (HttpRequest) the request being made
    """
    pageview_data = DEFAULT_DATA + "t=pageview&dp=" + quote(request.path, safe='')
    # send pageview request
    requests.post(GOOGLE_ANALYTICS_COLLECT_URL, data=pageview_data)

    resource = get_resource_from_uri(request.path)

    if resource in TRACKED_RESOURCE_ACTIONS:
        environment = Environment.get_from_cache(request.headers.get('X-Environment-Key'))
        track_event(environment.project.organisation.get_unique_slug(), resource)
Exemplo n.º 7
0
def track_request(request):
    """
    Utility function to track a request to the API with the specified URI

    :param request: (HttpRequest) the request being made
    """
    uri = request.path
    data = DEFAULT_DATA + "t=pageview&dp=" + quote(uri, safe='')
    requests.post(GOOGLE_ANALYTICS_COLLECT_URL, data=data)

    resource = uri.split('/')[
        3]  # uri will be in the form /api/v1/<resource>/...
    if resource in TRACKED_RESOURCE_ACTIONS:
        environment = Environment.get_from_cache(
            request.headers.get('X-Environment-Key'))
        track_event(environment.project.organisation.get_unique_slug(),
                    TRACKED_RESOURCE_ACTIONS[resource])
Exemplo n.º 8
0
def track_request_influxdb(request):
    """
    Sends API event data to InfluxDB

    :param request: (HttpRequest) the request being made
    """
    resource = get_resource_from_uri(request.path)

    if resource:
        environment = Environment.get_from_cache(
            request.headers.get('X-Environment-Key'))

        tags = {
            "resource": resource,
            "organisation": environment.project.organisation.get_unique_slug(),
            "organisation_id": environment.project.organisation_id,
            "project": environment.project.name,
            "project_id": environment.project_id
        }

        influxdb = InfluxDBWrapper("api_call", "request_count", 1, tags=tags)
        influxdb.write()