<commit_before>import json

import aiohttp
from aiohttp import web
import pytest


class RestClientError(Exception):
    """Base exception class for RESTClient"""

    @property
    def status_code(self):
        return self.args[0]


class PlainRestError(RestClientError):
    """Answer is not JSON, for example for 500 Internal Server Error"""

    @property
    def error_text(self):
        return self.args[1]


class JsonRestError(RestClientError):
    """Answer is JSON error report"""

    @property
    def error_json(self):
        return self.args[1]


class AdminRESTClient:

    def __init__(self, url, *, admin_prefix=None, loop):
        self._loop = loop
        self._url = url
        self._admin_prefix = admin_prefix or 'admin'
        self._session = aiohttp.ClientSession(loop=loop)

    async def request(self, method, path, data=None, params=None,
                      headers=None, json_dumps=True):
        if json_dumps and (data is not None):
            data = json.dumps(data).encode('utf-8')
        url = '{}/{}/{}'.format(self._url, self._admin_prefix, path)
        resp = await self._session.request(method, url,
                                           params=params, data=data,
                                           headers=headers)
        body = await resp.read()
        if resp.status in (200, 201):
            jsoned = await resp.json()
            return jsoned
        elif resp.status == 500:
            raise PlainRestError(body.decode('utf-8'))
        else:
            try:<commit_msg>use jsonify to serialize dates<commit_after>import json

import aiohttp
from aiohttp import web
import pytest

from aiohttp_admin.utils import jsonify


class RestClientError(Exception):
    """Base exception class for RESTClient"""

    @property
    def status_code(self):
        return self.args[0]


class PlainRestError(RestClientError):
    """Answer is not JSON, for example for 500 Internal Server Error"""

    @property
    def error_text(self):
        return self.args[1]


class JsonRestError(RestClientError):
    """Answer is JSON error report"""

    @property
    def error_json(self):
        return self.args[1]


class AdminRESTClient:

    def __init__(self, url, *, admin_prefix=None, loop):
        self._loop = loop
        self._url = url
        self._admin_prefix = admin_prefix or 'admin'
        self._session = aiohttp.ClientSession(loop=loop)

    async def request(self, method, path, data=None, params=None,
                      headers=None, json_dumps=True):
        if json_dumps and (data is not None):
            data = jsonify(data).encode('utf-8')
        url = '{}/{}/{}'.format(self._url, self._admin_prefix, path)
        resp = await self._session.request(method, url,
                                           params=params, data=data,
                                           headers=headers)
        body = await resp.read()
        if resp.status in (200, 201):
            jsoned = await resp.json()
            return jsoned
        elif resp.status == 500:
            raise PlainRestError(body.decode('utf-8'))
        else:
            try:
{'fraction_non_alphanumeric': 0.10117721921730831, 'fraction_numerical': 0.010817690104995227, 'mean_word_length': 2.175757575757576, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6018986', 'n_tokens_mistral': 904, 'n_tokens_neox': 851, 'n_words': 243}