예제 #1
0
    async def get(self):
        """Processing of GET request."""

        async with self.request.app["db"].acquire() as conn:
            objects = await self.get_list_func(conn)

        return web.json_response(text=to_json(objects))
예제 #2
0
def test_json_serializing():
    """"""

    date = datetime.datetime.now()

    with pytest.raises(TypeError):
        json.dumps(date)

    assert json.loads(to_json(date)) == str(date)

    for example in (1, 1.0, True, None, False, "d", "<div>", [1, 2, 3, {
            1: 2
    }], {
            2: "3",
            "4": True
    }, (1, 2, 3)):
        assert json.dumps(example, indent=4) == to_json(example)
예제 #3
0
    def response(self) -> web.Response:
        """
        Return the response of not found record.

        :return: Response of not found record
        :rtype: web.Response
        """

        return web.json_response(text=to_json(self.error_dict()), status=404)
예제 #4
0
    async def get(self):
        post_id = int(self.request.match_info.get("post_id"))

        async with self.request.app["db"].acquire() as conn:
            try:
                post = await get_post_or_exception(conn, post_id=post_id)
            except PostNotFoundException as exc:
                return exc.response()

        return web.json_response(text=to_json(post))
예제 #5
0
    async def comments(request: web.Request):
        post_id = int(request.match_info.get("post_id"))

        async with request.app["db"].acquire() as conn:
            try:
                comments_count = await get_posts_comments(conn,
                                                          post_id=post_id)
            except PostNotFoundException as exc:
                return exc.response()

        return web.json_response(text=to_json({"comments": comments_count}))
예제 #6
0
async def hello(request: web.Request) -> web.Response:
    """
    First, single and test view of application.

    :param request: Input request
    :type request: web.Request
    :return: Static json string `{"status": "OK"}`
    :rtype: web.Response
    """

    return web.json_response(text=to_json({"status": "OK"}))
예제 #7
0
    async def delete(self):
        """Processing of DELETE request."""

        field_id = int(self.request.match_info.get(self.field))

        async with self.request.app["db"].acquire() as conn:
            try:
                result = await self.delete_func(conn, **{self.field: field_id})
            except RecordNotFoundException as exc:
                return exc.response()

        return web.json_response(text=to_json({"deleted": result}))
예제 #8
0
    async def get(self):
        """Processing of GET request."""

        field_id = int(self.request.match_info.get(self.field))

        async with self.request.app["db"].acquire() as conn:
            try:
                obj = await self.get_func(conn, **{self.field: field_id})
            except RecordNotFoundException as exc:
                return exc.response()

        return web.json_response(text=to_json(obj))
예제 #9
0
    async def post(self):
        """Processing of POST request."""

        json_data = await self.request.json()

        arguments = {
            key: value
            for key, value in json_data.items() if key in self.create_fields
        }

        async with self.request.app["db"].acquire() as conn:
            obj_id = await self.create_func(conn, **arguments)
            obj = await self.get_func(conn, **{self.field: obj_id})

        return web.json_response(text=to_json(obj))
예제 #10
0
    async def post(self):
        arguments = await self.request.json()

        async with self.request.app["db"].acquire() as conn:
            succeed, post_id, errors = await create_post(
                conn,
                user_id=arguments.get("user_id"),
                text=arguments.get("text"),
                image=arguments.get("image"),
            )

            if succeed:
                response, status = (
                    await get_post_or_exception(conn, post_id=post_id),
                    201,
                )
            else:
                response, status = {"errors": errors}, 400

        return web.json_response(text=to_json(response), status=status)
예제 #11
0
    async def get(self):
        async with self.request.app["db"].acquire() as conn:
            posts = await get_posts(conn)

        return web.json_response(text=to_json(posts))