async def test_unit__handle_exception_with_default_error_builder__ok__serpyco(
            self, test_client):
        from hapic.error.serpyco import SerpycoDefaultErrorBuilder
        from hapic.processor.serpyco import SerpycoProcessor

        app = AgnosticApp()
        hapic = Hapic()
        hapic.set_processor_class(SerpycoProcessor)
        hapic.set_context(
            AgnosticContext(
                app, default_error_builder=SerpycoDefaultErrorBuilder()))

        @hapic.with_api_doc()
        @hapic.handle_exception(ZeroDivisionError, http_code=400)
        def my_view():
            1 / 0

        response = my_view()
        json_ = json.loads(response.body)
        assert {
            "code": None,
            "details": {
                "error_detail": {}
            },
            "message": "division by zero",
        } == json_
def get_aiohttp_serpyco_app_hapic(app):
    from example.fake_api.aiohttp_serpyco import hapic

    hapic.reset_context()
    hapic.set_context(
        AiohttpContext(app,
                       default_error_builder=SerpycoDefaultErrorBuilder()))
    return hapic
Пример #3
0
 def get_default_error_builder(cls) -> ErrorBuilderInterface:
     """
     :return: Default error builder to use for this processor
     """
     return SerpycoDefaultErrorBuilder()
@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)
Пример #5
0
    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)
Пример #6
0
    def bind(self, app: bottle.Bottle):
        app.route("/about", callback=self.about)
        app.route("/users/", callback=self.get_users)
        app.route("/users/<id>", callback=self.get_user)
        app.route("/users/", callback=self.add_user, method="POST")
        app.route("/users/<id>", callback=self.del_user, method="DELETE")
        app.route("/users/<id>/avatar", callback=self.get_user_avatar)
        app.route("/users/<id>/avatar", callback=self.update_user_avatar, method="PUT")


if __name__ == "__main__":
    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(