example_a.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. import bottle
  3. import hapic
  4. from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema
  5. from hapic.hapic import HapicData
  6. app = bottle.Bottle()
  7. def bob(f):
  8. def boby(*args, **kwargs):
  9. return f(*args, **kwargs)
  10. return boby
  11. @hapic.with_api_doc()
  12. @hapic.ext.bottle.bottle_context()
  13. @hapic.input_path(HelloPathSchema())
  14. @hapic.output(HelloResponseSchema())
  15. def hello(name: str, hapic_data: HapicData):
  16. return {
  17. 'sentence': 'Hello !',
  18. 'name': name,
  19. }
  20. @hapic.with_api_doc()
  21. @hapic.ext.bottle.bottle_context()
  22. @hapic.input_path(HelloPathSchema())
  23. @hapic.input_body(HelloJsonSchema())
  24. @hapic.output(HelloResponseSchema())
  25. @bob
  26. def hello2(name: str, hapic_data: HapicData):
  27. return {
  28. 'sentence': 'Hello !',
  29. 'name': name,
  30. 'color': hapic_data.body.get('color'),
  31. }
  32. kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
  33. @hapic.with_api_doc()
  34. @hapic.ext.bottle.bottle_context()
  35. @hapic.output(HelloResponseSchema())
  36. def hello3(name: str, hapic_data: HapicData):
  37. return {
  38. 'sentence': 'Hello !',
  39. 'name': name,
  40. }
  41. app.route('/hello/<name>', callback=hello)
  42. app.route('/hello2/<name>', callback=hello2, method='POST')
  43. app.route('/hello3/<name>', callback=hello3)
  44. hapic.generate_doc(app)
  45. app.run(host='localhost', port=8080, debug=True)