serve_bottle.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. import bottle
  3. from datetime import datetime
  4. from http import HTTPStatus
  5. import json
  6. import time
  7. from hapic import Hapic
  8. from hapic.data import HapicData
  9. from hapic.ext.bottle import BottleContext
  10. from example.usermanagement.schema import AboutSchema
  11. from example.usermanagement.schema import NoContentSchema
  12. from example.usermanagement.schema import UserDigestSchema
  13. from example.usermanagement.schema import UserIdPathSchema
  14. from example.usermanagement.schema import UserSchema
  15. from example.usermanagement.userlib import User
  16. from example.usermanagement.userlib import UserLib
  17. from example.usermanagement.userlib import UserNotFound
  18. hapic = Hapic()
  19. class BottleController(object):
  20. @hapic.with_api_doc()
  21. @hapic.output_body(AboutSchema())
  22. def about(self, context, request):
  23. """
  24. This endpoint allow to check that the API is running. This description
  25. is generated from the docstring of the method.
  26. """
  27. return {
  28. 'version': '1.2.3',
  29. 'datetime': datetime.now(),
  30. }
  31. @hapic.with_api_doc()
  32. @hapic.output_body(UserDigestSchema(many=True))
  33. def get_users(self):
  34. """
  35. Obtain users list.
  36. """
  37. return UserLib().get_users()
  38. @hapic.with_api_doc()
  39. @hapic.handle_exception(UserNotFound, HTTPStatus.NOT_FOUND)
  40. @hapic.input_path(UserIdPathSchema())
  41. @hapic.output_body(UserSchema())
  42. def get_user(self, id, hapic_data: HapicData):
  43. """
  44. Return a user taken from the list or return a 404
  45. """
  46. return UserLib().get_user(int(hapic_data.path['id']))
  47. @hapic.with_api_doc()
  48. # TODO - G.M - 2017-12-5 - Support input_forms ?
  49. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  50. @hapic.input_body(UserSchema(exclude=('id',)))
  51. @hapic.output_body(UserSchema())
  52. def add_user(self, hapic_data: HapicData):
  53. """
  54. Add a user to the list
  55. """
  56. print(hapic_data.body)
  57. new_user = User(**hapic_data.body)
  58. return UserLib().add_user(new_user)
  59. @hapic.with_api_doc()
  60. @hapic.handle_exception(UserNotFound, HTTPStatus.NOT_FOUND)
  61. @hapic.output_body(NoContentSchema(), default_http_code=204)
  62. @hapic.input_path(UserIdPathSchema())
  63. def del_user(self, id, hapic_data: HapicData):
  64. UserLib().del_user(int(hapic_data.path['id']))
  65. return NoContentSchema()
  66. def bind(self, app:bottle.Bottle):
  67. app.route('/about', callback=self.about)
  68. app.route('/users', callback=self.get_users)
  69. app.route('/users/<id>', callback=self.get_user)
  70. app.route('/users', callback=self.add_user, method='POST')
  71. app.route('/users/<id>', callback=self.del_user, method='DELETE')
  72. if __name__ == "__main__":
  73. app = bottle.Bottle()
  74. controllers = BottleController()
  75. controllers.bind(app)
  76. hapic.set_context(BottleContext(app))
  77. print('')
  78. print('')
  79. print('GENERATING OPENAPI DOCUMENTATION')
  80. openapi_file_name = 'api-documentation.json'
  81. with open(openapi_file_name, 'w') as openapi_file_handle:
  82. openapi_file_handle.write(
  83. json.dumps(
  84. hapic.generate_doc(
  85. title='Demo API documentation',
  86. description='This documentation has been generated from '
  87. 'code. You can see it using swagger: '
  88. 'http://editor2.swagger.io/'
  89. )
  90. )
  91. )
  92. print('Documentation generated in {}'.format(openapi_file_name))
  93. time.sleep(1)
  94. print('')
  95. print('')
  96. print('RUNNING BOTTLE SERVER NOW')
  97. # Run app
  98. app.run(host='127.0.0.1', port=8081, debug=True)