Module kibicara.platforms.test.webapi

Expand source code
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
#
# SPDX-License-Identifier: 0BSD

from fastapi import APIRouter, Depends, HTTPException, Response, status
from kibicara.platforms.test.bot import spawner
from kibicara.platforms.test.model import Test
from kibicara.platformapi import Message
from kibicara.webapi.hoods import get_hood
from ormantic.exceptions import NoMatch
from pydantic import BaseModel
from sqlite3 import IntegrityError


class BodyMessage(BaseModel):
    text: str


async def get_test(test_id: int, hood=Depends(get_hood)):
    try:
        return await Test.objects.get(id=test_id, hood=hood)
    except NoMatch:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)


router = APIRouter()


@router.get('/')
async def test_read_all(hood=Depends(get_hood)):
    return await Test.objects.filter(hood=hood).all()


@router.post('/', status_code=status.HTTP_201_CREATED)
async def test_create(response: Response, hood=Depends(get_hood)):
    try:
        test = await Test.objects.create(hood=hood)
        spawner.start(test)
        response.headers['Location'] = '%d' % test.id
        return test
    except IntegrityError:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT)


@router.get('/{test_id}')
async def test_read(test=Depends(get_test)):
    return test


@router.delete('/{test_id}', status_code=status.HTTP_204_NO_CONTENT)
async def test_delete(test=Depends(get_test)):
    spawner.stop(test)
    await test.delete()


@router.get('/{test_id}/messages/')
async def test_message_read_all(test=Depends(get_test)):
    return spawner.get(test).messages


@router.post('/{test_id}/messages/')
async def test_message_create(message: BodyMessage, test=Depends(get_test)):
    await spawner.get(test).publish(Message(message.text))
    return {}

Functions

async def get_test(test_id: int, hood=Depends(get_hood))
Expand source code
async def get_test(test_id: int, hood=Depends(get_hood)):
    try:
        return await Test.objects.get(id=test_id, hood=hood)
    except NoMatch:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
async def test_create(response: starlette.responses.Response, hood=Depends(get_hood))
Expand source code
@router.post('/', status_code=status.HTTP_201_CREATED)
async def test_create(response: Response, hood=Depends(get_hood)):
    try:
        test = await Test.objects.create(hood=hood)
        spawner.start(test)
        response.headers['Location'] = '%d' % test.id
        return test
    except IntegrityError:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT)
async def test_delete(test=Depends(get_test))
Expand source code
@router.delete('/{test_id}', status_code=status.HTTP_204_NO_CONTENT)
async def test_delete(test=Depends(get_test)):
    spawner.stop(test)
    await test.delete()
async def test_message_create(message: BodyMessage, test=Depends(get_test))
Expand source code
@router.post('/{test_id}/messages/')
async def test_message_create(message: BodyMessage, test=Depends(get_test)):
    await spawner.get(test).publish(Message(message.text))
    return {}
async def test_message_read_all(test=Depends(get_test))
Expand source code
@router.get('/{test_id}/messages/')
async def test_message_read_all(test=Depends(get_test)):
    return spawner.get(test).messages
async def test_read(test=Depends(get_test))
Expand source code
@router.get('/{test_id}')
async def test_read(test=Depends(get_test)):
    return test
async def test_read_all(hood=Depends(get_hood))
Expand source code
@router.get('/')
async def test_read_all(hood=Depends(get_hood)):
    return await Test.objects.filter(hood=hood).all()

Classes

class BodyMessage (**data: Any)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class BodyMessage(BaseModel):
    text: str

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Class variables

var text : str