@hapic.output_body(HelloResponseSchema()) def hello3(self, name: str, hapic_data: HapicData): return {"sentence": "Hello !", "name": name} @hapic.with_api_doc() @hapic.input_files(HelloFileSchema()) @hapic.output_file(["image/jpeg"]) def hellofile(self, hapic_data: HapicData): return hapic_data.files["myfile"] def bind(self, app): app.route("/hello/<name>", callback=self.hello) app.route("/hello/<name>", callback=self.hello2, method="POST") app.route("/hello3/<name>", callback=self.hello3) app.route("/hellofile", callback=self.hellofile) app = bottle.Bottle() controllers = Controllers() controllers.bind(app) hapic.set_context( BottleContext(app, default_error_builder=MarshmallowDefaultErrorBuilder())) print( yaml.dump(json.loads(json.dumps(hapic.generate_doc())), default_flow_style=False)) app.run(host="localhost", port=8080, debug=True)
'sentence': 'Hello !', 'name': name, } def bind(self, app): app.route('/hello/<name>', callback=self.hello) app.route('/hello/<name>', callback=self.hello2, method='POST') app.route('/hello3/<name>', callback=self.hello3) app = bottle.Bottle() controllers = Controllers() controllers.bind(app) # time.sleep(1) # s = hapic.generate_doc(app) # ss = json.loads(json.dumps(s)) # for path in ss['paths']: # for method in ss['paths'][path]: # for response_code in ss['paths'][path][method]['responses']: # ss['paths'][path][method]['responses'][int(response_code)] = ss['paths'][path][method]['responses'][response_code] # del ss['paths'][path][method]['responses'][int(response_code)] # print(yaml.dump(ss, default_flow_style=False)) # time.sleep(1) hapic.set_context(hapic.ext.bottle.BottleContext()) print(json.dumps(hapic.generate_doc(app))) app.run(host='localhost', port=8080, debug=True)
raise VlcPlayProblem( 'Error when trying to read "{}": file not exist'.format( file_name, ), detail=dict(file_path=file_name, )) player.play() return '', 204 @hapic.with_api_doc() @app.route('/error') @hapic.output_body(EmptyResponseSchema(), default_http_code=204) def error(): 1 / 0 # doc view context = FlaskContext(app) hapic.set_context(context) hapic.add_documentation_view('/api/doc') # Set our error builder context.default_error_builder = ErrorBuilder() context.handle_exception(Exception, http_code=500) context.handle_exception(ZeroDivisionError, http_code=500) # Enable debug mode context.debug = True # run server app.run(debug=True)
configurator.add_view(self.hello3, route_name="hello3", renderer="json") configurator.add_route("hellofile", "/hellofile", request_method="GET") configurator.add_view(self.hellofile, route_name="hellofile") configurator.add_route("hellofile2", "/hellofile2", request_method="GET") configurator.add_view(self.hellofile2, route_name="hellofile2") configurator.add_route("hellofile3", "/hellofile3", request_method="GET") configurator.add_view(self.hellofile3, route_name="hellofile3") configurator = Configurator(autocommit=True) controllers = Controllers() controllers.bind(configurator) hapic.set_context( PyramidContext(configurator, default_error_builder=MarshmallowDefaultErrorBuilder())) print(json.dumps(hapic.generate_doc())) server = make_server("0.0.0.0", 8080, configurator.make_wsgi_app()) server.serve_forever()