bottle_api.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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. return {
  30. 'item_nb': 1,
  31. 'items': [
  32. {
  33. 'id': 4,
  34. 'username': 'some_user',
  35. 'display_name': 'Damien Accorsi',
  36. 'company': 'Algoo',
  37. },
  38. ],
  39. 'pagination': {
  40. 'first_id': 0,
  41. 'last_id': 5,
  42. 'current_id': 0,
  43. }
  44. }
  45. @hapic.with_api_doc()
  46. @hapic.input_path(UserPathSchema())
  47. @hapic.output_body(UserSchema())
  48. def get_user(self, id, hapic_data: HapicData):
  49. """
  50. Obtain one user
  51. """
  52. return {
  53. 'id': 4,
  54. 'username': 'some_user',
  55. 'email_address': 'some.user@hapic.com',
  56. 'first_name': 'Damien',
  57. 'last_name': 'Accorsi',
  58. 'display_name': 'Damien Accorsi',
  59. 'company': 'Algoo',
  60. }
  61. @hapic.with_api_doc()
  62. # TODO - G.M - 2017-12-5 - Support input_forms ?
  63. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  64. @hapic.input_body(UserSchema(exclude=('id',)))
  65. @hapic.output_body(UserSchema())
  66. def add_user(self, hapic_data: HapicData):
  67. """
  68. Add new user
  69. """
  70. return {
  71. 'id': 4,
  72. 'username': 'some_user',
  73. 'email_address': 'some.user@hapic.com',
  74. 'first_name': 'Damien',
  75. 'last_name': 'Accorsi',
  76. 'display_name': 'Damien Accorsi',
  77. 'company': 'Algoo',
  78. }
  79. @hapic.with_api_doc()
  80. @hapic.output_body(NoContentSchema(),
  81. default_http_code=204)
  82. @hapic.input_path(UserPathSchema())
  83. def del_user(self, id, hapic_data: HapicData):
  84. """
  85. delete user
  86. """
  87. return NoContentSchema()
  88. def bind(self, app:bottle.Bottle):
  89. app.route('/about', callback=self.about)
  90. app.route('/users', callback=self.get_users)
  91. app.route('/users/<id>', callback=self.get_user)
  92. app.route('/users/', callback=self.add_user, method='POST')
  93. app.route('/users/<id>', callback=self.del_user, method='DELETE')
  94. if __name__ == "__main__":
  95. app = bottle.Bottle()
  96. controllers = BottleController()
  97. controllers.bind(app)
  98. hapic.set_context(BottleContext(app))
  99. time.sleep(1)
  100. s = json.dumps(hapic.generate_doc())
  101. time.sleep(1)
  102. # print swagger doc
  103. print(s)
  104. # Run app
  105. app.run(host='localhost', port=8081, debug=True)