example_a_bottle.py 3.0KB

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