bottle_api.py 3.3KB

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