example_a.py 1.5KB

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