example_a_bottle.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. import bottle
  5. import time
  6. import yaml
  7. import hapic
  8. from hapic.ext.bottle.context import BottleContext
  9. from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema, \
  10. ErrorResponseSchema, HelloQuerySchema, HelloFileSchema
  11. from hapic.data import HapicData
  12. def bob(f):
  13. def boby(*args, **kwargs):
  14. return f(*args, **kwargs)
  15. return boby
  16. class Controllers(object):
  17. @hapic.with_api_doc()
  18. # @hapic.ext.bottle.bottle_context()
  19. @hapic.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST)
  20. @hapic.input_path(HelloPathSchema())
  21. @hapic.input_query(HelloQuerySchema())
  22. @hapic.output_body(HelloResponseSchema())
  23. def hello(self, name: str, hapic_data: HapicData):
  24. """
  25. my endpoint hello
  26. ---
  27. get:
  28. description: my description
  29. parameters:
  30. - in: "path"
  31. description: "hello"
  32. name: "name"
  33. type: "string"
  34. responses:
  35. 200:
  36. description: A pet to be returned
  37. schema: HelloResponseSchema
  38. """
  39. if name == 'zero':
  40. raise ZeroDivisionError('Don\'t call him zero !')
  41. return {
  42. 'sentence': 'Hello !',
  43. 'name': name,
  44. }
  45. @hapic.with_api_doc()
  46. # @hapic.ext.bottle.bottle_context()
  47. # @hapic.error_schema(ErrorResponseSchema())
  48. @hapic.input_path(HelloPathSchema())
  49. @hapic.input_body(HelloJsonSchema())
  50. @hapic.output_body(HelloResponseSchema())
  51. @bob
  52. def hello2(self, name: str, hapic_data: HapicData):
  53. return {
  54. 'sentence': 'Hello !',
  55. 'name': name,
  56. 'color': hapic_data.body.get('color'),
  57. }
  58. kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
  59. @hapic.with_api_doc()
  60. # @hapic.ext.bottle.bottle_context()
  61. # @hapic.error_schema(ErrorResponseSchema())
  62. @hapic.input_path(HelloPathSchema())
  63. @hapic.output_body(HelloResponseSchema())
  64. def hello3(self, name: str, hapic_data: HapicData):
  65. return {
  66. 'sentence': 'Hello !',
  67. 'name': name,
  68. }
  69. @hapic.with_api_doc()
  70. @hapic.input_files(HelloFileSchema())
  71. @hapic.output_file(['image/jpeg'])
  72. def hellofile(self, hapic_data: HapicData):
  73. return hapic_data.files['myfile']
  74. def bind(self, app):
  75. app.route('/hello/<name>', callback=self.hello)
  76. app.route('/hello/<name>', callback=self.hello2, method='POST')
  77. app.route('/hello3/<name>', callback=self.hello3)
  78. app.route('/hellofile', callback=self.hellofile)
  79. app = bottle.Bottle()
  80. controllers = Controllers()
  81. controllers.bind(app)
  82. hapic.set_context(BottleContext(app))
  83. print(yaml.dump(
  84. json.loads(json.dumps(hapic.generate_doc())),
  85. default_flow_style=False,
  86. ))
  87. app.run(host='localhost', port=8080, debug=True)