password: str


@dataclass
class UserModel(object):
    id: str
    name: str


@dataclass
class GetUserPath(object):
    id: str


@hapic.with_api_doc()
@hapic.input_path(GetUserPath)
@hapic.output_body(UserModel)
async def get_user(request, hapic_data: HapicData):
    return UserModel(id=hapic_data.path.id, name="Bob")


app = web.Application()
app.add_routes([web.get(r"/user/{id}", get_user)])

context = AiohttpContext(app,
                         default_error_builder=SerpycoDefaultErrorBuilder())
hapic.set_context(context)
hapic.add_documentation_view("/api/doc")
print(json.dumps(hapic.generate_doc()))
web.run_app(app)
@hapic.with_api_doc()
@hapic.input_path(EmptyPath)
@hapic.input_body(PartialSensor)
@hapic.output_body(Sensor)
async def PATCH_sensor(request, hapic_data: HapicData):
    print(hapic_data.body)
    if hapic_data.body.name and sensor.name != hapic_data.body.name:
        sensor.name = hapic_data.body.name

    if hapic_data.body.location:
        if sensor.location != hapic_data.body.location:
            sensor.location = hapic_data.body.location

    return sensor


app = web.Application()
app.add_routes([
    web.get(r"/about", GET_about),
    web.get(r"/sensor", GET_sensor),
    web.patch(r"/sensor", PATCH_sensor),
])

hapic.set_context(
    AiohttpContext(app, default_error_builder=SerpycoDefaultErrorBuilder()))

hapic.add_documentation_view("/api/doc", "DOC", "Generated doc")
print(json.dumps(hapic.generate_doc()))
aiohttp_autoreload.start()
web.run_app(app)
Esempio n. 3
0
    app = bottle.Bottle()
    controllers = BottleController()
    controllers.bind(app)
    hapic.set_context(BottleContext(app, default_error_builder=SerpycoDefaultErrorBuilder()))

    print("")
    print("")
    print("GENERATING OPENAPI DOCUMENTATION")

    doc_title = "Demo API documentation"
    doc_description = (
        "This documentation has been generated from "
        "code. You can see it using swagger: "
        "http://editor2.swagger.io/"
    )
    hapic.add_documentation_view("/doc/", doc_title, doc_description)
    openapi_file_name = "api-documentation.json"
    with open(openapi_file_name, "w") as openapi_file_handle:
        openapi_file_handle.write(
            json.dumps(hapic.generate_doc(title=doc_title, description=doc_description))
        )

    print("Documentation generated in {}".format(openapi_file_name))
    time.sleep(1)

    print("")
    print("")
    print("RUNNING BOTTLE SERVER NOW")
    print("DOCUMENTATION AVAILABLE AT /doc/")
    # Run app
    app.run(host="127.0.0.1", port=8081, debug=True)