<commit_before>"""
Provides a JSON API for common components.
"""

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
import hmac
import hashlib
import base64
from secrets import compare_digest

from django.utils.decorators import method_decorator
from django.urls import path
from django.views.decorators.csrf import csrf_exempt

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import PermissionDenied, NotFound, NotAcceptable

from .models import WebhookEndpoint, WebhookMessage


class CsrfExemptMixin(object):
    """
    Exempts the view from CSRF requirements.
    """

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(CsrfExemptMixin, self).dispatch(*args, **kwargs)


class VerificationMethod:
    NONE = 0
    TOKEN = 1
    HMAC = 2


class WebhookView(CsrfExemptMixin, APIView):
    """
    Endpoint for receiving webhoks.
    """
    authentication_classes = []
    permission_classes = []
    model_class = WebhookEndpoint

    # Token
    TOKEN_NAME = "Token"
    VERIFICATION_METHOD = VerificationMethod.NONE

    MESSAGE_OK = "Message was received."
    MESSAGE_TOKEN_ERROR = "Incorrect token in header."

    def post(self, request, endpoint, *args, **kwargs):
        self.init(request, *args, **kwargs)
        # get webhook definition
        self.get_webhook(endpoint, *args, **kwargs)

        # check headers
        headers = request.headers
        try:
            payload = json.loads(request.body)
        except json.decoder.JSONDecodeError as error:
            raise NotAcceptable(error.msg)

        # validate
        self.validate_token(payload, headers, request)
        # process data
        message = self.save_data(payload, headers, request)
        self.process_payload(message, payload, headers)

        # return results
        return_kwargs = self.get_result(payload, headers, request)
        return Response(**return_kwargs)
<commit_msg>let tasks run async<commit_after>"""
Provides a JSON API for common components.
"""

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
import hmac
import hashlib
import base64
from secrets import compare_digest

from django.utils.decorators import method_decorator
from django.urls import path
from django.views.decorators.csrf import csrf_exempt

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import PermissionDenied, NotFound, NotAcceptable
from django_q.tasks import async_task

from .models import WebhookEndpoint, WebhookMessage


class CsrfExemptMixin(object):
    """
    Exempts the view from CSRF requirements.
    """

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(CsrfExemptMixin, self).dispatch(*args, **kwargs)


class VerificationMethod:
    NONE = 0
    TOKEN = 1
    HMAC = 2


class WebhookView(CsrfExemptMixin, APIView):
    """
    Endpoint for receiving webhoks.
    """
    authentication_classes = []
    permission_classes = []
    model_class = WebhookEndpoint
    run_async = False

    # Token
    TOKEN_NAME = "Token"
    VERIFICATION_METHOD = VerificationMethod.NONE

    MESSAGE_OK = "Message was received."
    MESSAGE_TOKEN_ERROR = "Incorrect token in header."

    def post(self, request, endpoint, *args, **kwargs):
        self.init(request, *args, **kwargs)
        # get webhook definition
        self.get_webhook(endpoint, *args, **kwargs)

        # check headers
        headers = request.headers
        try:
            payload = json.loads(request.body)
        except json.decoder.JSONDecodeError as error:
            raise NotAcceptable(error.msg)

        # validate
        self.validate_token(payload, headers, request)
        # process data
        message = self.save_data(payload, headers, request)
        if self.run_async:
            async_task(self._process_payload, message.id)
        else:
            message.worked_on = self.process_payload(message, payload, headers)
            message.save()

        # return results
        return_kwargs = self.get_result(payload, headers, request)
        return Response(**return_kwargs)

    def _process_payload(self, message_id):
        message = WebhookMessage.objects.get(message_id=message_id)
        process_result = self.process_payload(message, message.body, message.header)
        message.worked_on = process_result
        message.save()

{'fraction_non_alphanumeric': 0.08344370860927153, 'fraction_numerical': 0.0026490066225165563, 'mean_word_length': 3.2266791044776117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063859', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1263, 'n_words': 340}