Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

newbie help

bbartling opened this issue · comments

Hi,

Would there be any chance I could get a tip getting started? Very cool creation btw...

So I am trying the Inject Path Parameters type of API from the link in the README. And I get a {"error": "Malformed JSON"} response just trying to test out some concept code for the API.

from typing import Optional, Literal

from aiohttp import web
from aiohttp_pydantic import PydanticView
from pydantic import BaseModel



ACTION_TYPE_MAPPING = Literal["read", "write", "release"]

PRIORITY_MAPPING = Literal["1", "2", "3","4",
                           "5", "6", "7", "8",
                           "9", "10", "11", "12",
                           "13", "14", "15", "16"
                           ]

OBJECT_TYPE_MAPPING = Literal["multiStateValue", "multiStateInput", "multiStateOutput",
                       "analogValue", "analogInput", "analogOutput",
                       "binaryValue", "binaryInput", "binaryOutput"]

BOOLEAN_ACTION_MAPPING = Literal["active", "inactive"]



class ValueModel(BaseModel):
    multiStateValue: Optional[int]
    multiStateInput: Optional[int]
    multiStateOutput: Optional[int]
    analogValue: Optional[int]
    analogInput: Optional[int]
    analogOutput: Optional[int]
    binaryValue: Optional[BOOLEAN_ACTION_MAPPING]
    binaryInput: Optional[BOOLEAN_ACTION_MAPPING]
    binaryOutput: Optional[BOOLEAN_ACTION_MAPPING]


# 3 required params
class ReadRequestModel(BaseModel):
    address: str
    object_type: OBJECT_TYPE_MAPPING
    object_instance: int

# 5 required params
class WriteRequestModel(BaseModel):
    address: str
    object_type: OBJECT_TYPE_MAPPING
    object_instance: int
    value: ValueModel  # can I call the ValueModel class?
    priority: PRIORITY_MAPPING


# 4 required params
class ReleaseRequestModel(BaseModel):
    address: str
    object_type: OBJECT_TYPE_MAPPING
    object_instance: int
    priority: PRIORITY_MAPPING


class BacnetReadView(PydanticView):
    async def get(self, read_req_string: ReadRequestModel):
        print(read_req_string.split())
        return web.json_response(read_req_string)
    
class BacnetReleaseView(PydanticView):
    async def get(self, release_req_string: WriteRequestModel):
        print(release_req_string.split())
        return web.json_response(release_req_string)

class BacnetWriteView(PydanticView):
    async def get(self, write_req_string: ReleaseRequestModel):
        print(write_req_string.split())
        return web.json_response(write_req_string)


app = web.Application()
app.router.add_get('/bacnet/read/{read_req_string}', BacnetReadView)
app.router.add_get('/bacnet/write/{write_req_string}', BacnetWriteView)
app.router.add_get('/bacnet/release/{release_req_string}', BacnetReleaseView)
web.run_app(app)

In the browser when running the app, am testing this:

bacnet/read/10.200.200.27 binaryOutput 3
bacnet/write/10.200.200.27 binaryOutput 3 active 12
bacnet/release/10.200.200.27 binaryOutput 3 12

Any tips or time to read/respond greatly appreciated. I know I have a ton of issues here particularly how to parse the injection string to the URL?