bottle_api.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. import bottle
  5. import time
  6. from datetime import datetime
  7. from example.fake_api.model import User
  8. from hapic import Hapic
  9. from example.fake_api.schema import *
  10. from hapic.data import HapicData
  11. from hapic.ext.bottle import BottleContext
  12. hapic = Hapic()
  13. class BottleController(object):
  14. @hapic.with_api_doc()
  15. @hapic.output_body(AboutResponseSchema())
  16. def about(self):
  17. """
  18. General information about this API.
  19. """
  20. return {
  21. 'version': '1.2.3',
  22. 'datetime': datetime(2017, 12, 7, 10, 55, 8, 488996),
  23. }
  24. @hapic.with_api_doc()
  25. @hapic.output_body(ListsUserSchema())
  26. def get_users(self):
  27. """
  28. Obtain users list.
  29. """
  30. some_user = User(
  31. id=4,
  32. username='some_user',
  33. display_name='Damien Accorsi',
  34. company='Algoo',
  35. )
  36. return {
  37. 'item_nb': 1,
  38. 'items': [
  39. some_user,
  40. ],
  41. 'pagination': {
  42. 'first_id': 0,
  43. 'last_id': 5,
  44. 'current_id': 0,
  45. }
  46. }
  47. @hapic.with_api_doc()
  48. @hapic.input_path(UserPathSchema())
  49. @hapic.output_body(UserSchema())
  50. def get_user(self, id, hapic_data: HapicData):
  51. """
  52. Obtain one user
  53. """
  54. return {
  55. 'id': 4,
  56. 'username': 'some_user',
  57. 'email_address': 'some.user@hapic.com',
  58. 'first_name': 'Damien',
  59. 'last_name': 'Accorsi',
  60. 'display_name': 'Damien Accorsi',
  61. 'company': 'Algoo',
  62. }
  63. @hapic.with_api_doc()
  64. # TODO - G.M - 2017-12-5 - Support input_forms ?
  65. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  66. @hapic.input_body(UserSchema(exclude=('id',)))
  67. @hapic.output_body(UserSchema())
  68. def add_user(self, hapic_data: HapicData):
  69. """
  70. Add new user
  71. """
  72. return {
  73. 'id': 4,
  74. 'username': 'some_user',
  75. 'email_address': 'some.user@hapic.com',
  76. 'first_name': 'Damien',
  77. 'last_name': 'Accorsi',
  78. 'display_name': 'Damien Accorsi',
  79. 'company': 'Algoo',
  80. }
  81. @hapic.with_api_doc()
  82. @hapic.output_body(NoContentSchema(),
  83. default_http_code=204)
  84. @hapic.input_path(UserPathSchema())
  85. def del_user(self, id, hapic_data: HapicData):
  86. """
  87. delete user
  88. """
  89. return NoContentSchema()
  90. def bind(self, app:bottle.Bottle):
  91. app.route('/about', callback=self.about)
  92. app.route('/users', callback=self.get_users)
  93. app.route('/users/<id>', callback=self.get_user)
  94. app.route('/users/', callback=self.add_user, method='POST')
  95. app.route('/users/<id>', callback=self.del_user, method='DELETE')
  96. if __name__ == "__main__":
  97. app = bottle.Bottle()
  98. controllers = BottleController()
  99. controllers.bind(app)
  100. hapic.set_context(BottleContext(app))
  101. time.sleep(1)
  102. s = json.dumps(
  103. hapic.generate_doc(
  104. title='Fake API',
  105. description='just an example of hapic API'
  106. )
  107. )
  108. time.sleep(1)
  109. # print swagger doc
  110. print(s)
  111. # Run app
  112. app.run(host='localhost', port=8081, debug=True)