1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
-
- from http import HTTPStatus
-
- import bottle
- import hapic
- from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema, \
- ErrorResponseSchema
- from hapic.data import HapicData
-
- app = bottle.Bottle()
-
-
- def bob(f):
- def boby(*args, **kwargs):
- return f(*args, **kwargs)
- return boby
-
-
- @hapic.with_api_doc()
-
- @hapic.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST)
- @hapic.input_path(HelloPathSchema())
- @hapic.output_body(HelloResponseSchema())
- def hello(name: str, hapic_data: HapicData):
- if name == 'zero':
- raise ZeroDivisionError('Don\'t call him zero !')
-
- return {
- 'sentence': 'Hello !',
- 'name': name,
- }
-
-
- @hapic.with_api_doc()
-
-
- @hapic.input_path(HelloPathSchema())
- @hapic.input_body(HelloJsonSchema())
- @hapic.output_body(HelloResponseSchema())
- @bob
- def hello2(name: str, hapic_data: HapicData):
- return {
- 'sentence': 'Hello !',
- 'name': name,
- 'color': hapic_data.body.get('color'),
- }
-
- kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
-
-
- @hapic.with_api_doc()
-
-
- @hapic.output_body(HelloResponseSchema())
- def hello3(name: str):
- return {
- 'sentence': 'Hello !',
- 'name': name,
- }
-
-
- app.route('/hello/<name>', callback=hello)
- app.route('/hello2/<name>', callback=hello2, method='POST')
- app.route('/hello3/<name>', callback=hello3)
-
- hapic.generate_doc(app)
- app.run(host='localhost', port=8080, debug=True)
|