Ejemplo n.º 1
0
import fastapi_jsonrpc as jsonrpc
from pydantic import BaseModel
from fastapi import Body

app = jsonrpc.API()

app_v1 = jsonrpc.Entrypoint('/api/v1/jsonrpc')


class MyError(jsonrpc.BaseError):
    CODE = 5000
    MESSAGE = 'My Error'

    class DataModel(BaseModel):
        details: str


@app_v1.method(errors=[MyError])
def echo(data: str = Body(..., example='123'), ) -> str:
    if data == 'error':
        raise MyError(data={'details': 'error'})
    else:
        return data


app.bind_entrypoint(app_v1)

if __name__ == '__main__':
    import uvicorn
    uvicorn.run('example1:app', port=5000, debug=True, access_log=True)
Ejemplo n.º 2
0
def app(ep):
    app = jsonrpc.API()
    app.bind_entrypoint(ep)
    return app