<commit_before>from __future__ import absolute_import

__all__ = ('ApiClient',)

from django.core.urlresolvers import resolve
from rest_framework.test import APIRequestFactory, force_authenticate

from sentry.utils import json


class ApiError(Exception):
    def __init__(self, status_code, body):
        self.status_code = status_code
        self.body = body

    def __unicode__(self):
        return u'status={} body={}'.format(self.status_code, self.body)

    def __str__(self):
        return self.__unicode__().encode('utf-8')

    def __repr__(self):
        return u'<ApiError: {}>'.format(self.__unicode__())


class ApiClient(object):
    prefix = '/api/0'

    ApiError = ApiError

    def request(self, method, path, user=None, auth=None, params=None, data=None,
                is_sudo=None, is_superuser=None, request=None):
        full_path = self.prefix + path

        # we explicitly do not allow you to override the request *and* the user
        # as then other checks like is_superuser would need overwritten
        assert not (request and (user or auth)), 'use either request or auth'

        resolver_match = resolve(full_path)
        callback, callback_args, callback_kwargs = resolver_match

        if data:
            # we encode to ensure compatibility
            data = json.loads(json.dumps(data))

        rf = APIRequestFactory()
        mock_request = getattr(rf, method.lower())(full_path, data)

        if request:
            mock_request.auth = request.auth
            mock_request.user = request.user

            if is_sudo is None:
                mock_request.is_sudo = lambda: request.is_sudo()
            else:
                mock_request.is_sudo = lambda: is_sudo

            if is_superuser is None:
                mock_request.is_superuser = lambda: request.is_superuser()
            else:
                mock_request.is_superuser = lambda: is_superuser
        else:
            mock_request.auth = auth
            mock_request.user = user
            mock_request.is_sudo = lambda: is_sudo
            mock_request.is_superuser = lambda: is_superuser

        if request:<commit_msg>Handle no auth<commit_after>from __future__ import absolute_import

__all__ = ('ApiClient',)

from django.core.urlresolvers import resolve
from rest_framework.test import APIRequestFactory, force_authenticate

from sentry.utils import json


class ApiError(Exception):
    def __init__(self, status_code, body):
        self.status_code = status_code
        self.body = body

    def __unicode__(self):
        return u'status={} body={}'.format(self.status_code, self.body)

    def __str__(self):
        return self.__unicode__().encode('utf-8')

    def __repr__(self):
        return u'<ApiError: {}>'.format(self.__unicode__())


class ApiClient(object):
    prefix = '/api/0'

    ApiError = ApiError

    def request(self, method, path, user=None, auth=None, params=None, data=None,
                is_sudo=None, is_superuser=None, request=None):
        full_path = self.prefix + path

        # we explicitly do not allow you to override the request *and* the user
        # as then other checks like is_superuser would need overwritten
        assert not (request and (user or auth)), 'use either request or auth'

        resolver_match = resolve(full_path)
        callback, callback_args, callback_kwargs = resolver_match

        if data:
            # we encode to ensure compatibility
            data = json.loads(json.dumps(data))

        rf = APIRequestFactory()
        mock_request = getattr(rf, method.lower())(full_path, data)

        if request:
            mock_request.auth = getattr(request, 'auth', None)
            mock_request.user = request.user

            if is_sudo is None:
                mock_request.is_sudo = lambda: request.is_sudo()
            else:
                mock_request.is_sudo = lambda: is_sudo

            if is_superuser is None:
                mock_request.is_superuser = lambda: request.is_superuser()
            else:
                mock_request.is_superuser = lambda: is_superuser
        else:
            mock_request.auth = auth
            mock_request.user = user
            mock_request.is_sudo = lambda: is_sudo
            mock_request.is_superuser = lambda: is_superuser

        if request:
{'fraction_non_alphanumeric': 0.08543644362120861, 'fraction_numerical': 0.0009261403102570039, 'mean_word_length': 2.4394904458598727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6311063', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1201, 'n_words': 323}