bottle_api.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. import bottle
  5. import time
  6. from datetime import datetime
  7. import hapic
  8. from example.fake_api.schema import *
  9. from hapic.data import HapicData
  10. class NoContentException(Exception):
  11. pass
  12. class Controllers(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.now(),
  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.handle_exception(NoContentException, http_code=HTTPStatus.NO_CONTENT)
  81. @hapic.input_path(UserPathSchema())
  82. def del_user(self, id, hapic_data: HapicData):
  83. """
  84. delete user
  85. """
  86. # TODO - G.M - 2017-12-05 - Add better
  87. # way to doc response of this function, using response object ?
  88. # return bottle.Response(
  89. # status=204,
  90. # )
  91. raise NoContentException
  92. def bind(self, app:bottle.Bottle):
  93. app.route('/about', callback=self.about)
  94. app.route('/users', callback=self.get_users)
  95. app.route('/users/<id>', callback=self.get_user)
  96. app.route('/users/', callback=self.add_user, method='POST')
  97. app.route('/users/<id>', callback=self.del_user, method='DELETE')
  98. app = bottle.Bottle()
  99. controllers = Controllers()
  100. controllers.bind(app)
  101. hapic.set_context(hapic.ext.bottle.BottleContext(app))
  102. time.sleep(1)
  103. s = json.dumps(hapic.generate_doc())
  104. time.sleep(1)
  105. # print swagger doc
  106. print(s)
  107. # Run app
  108. app.run(host='localhost', port=8080, debug=True)