bottle_api.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.output_body(UserSchema(
  48. many=True,
  49. only=('id', 'username', 'display_name', 'company')
  50. ))
  51. def get_users2(self):
  52. """
  53. Obtain users list.
  54. """
  55. return [
  56. {
  57. 'id': 4,
  58. 'username': 'some_user',
  59. 'display_name': 'Damien Accorsi',
  60. 'company': 'Algoo',
  61. }
  62. ]
  63. @hapic.with_api_doc()
  64. @hapic.input_path(UserPathSchema())
  65. @hapic.output_body(UserSchema())
  66. def get_user(self, id, hapic_data: HapicData):
  67. """
  68. Obtain one 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. # TODO - G.M - 2017-12-5 - Support input_forms ?
  81. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  82. @hapic.input_body(UserSchema(exclude=('id',)))
  83. @hapic.output_body(UserSchema())
  84. def add_user(self, hapic_data: HapicData):
  85. """
  86. Add new user
  87. """
  88. return {
  89. 'id': 4,
  90. 'username': 'some_user',
  91. 'email_address': 'some.user@hapic.com',
  92. 'first_name': 'Damien',
  93. 'last_name': 'Accorsi',
  94. 'display_name': 'Damien Accorsi',
  95. 'company': 'Algoo',
  96. }
  97. @hapic.with_api_doc()
  98. @hapic.output_body(NoContentSchema(),
  99. default_http_code=204)
  100. @hapic.input_path(UserPathSchema())
  101. def del_user(self, id, hapic_data: HapicData):
  102. """
  103. delete user
  104. """
  105. return NoContentSchema()
  106. def bind(self, app:bottle.Bottle):
  107. app.route('/about', callback=self.about)
  108. app.route('/users', callback=self.get_users)
  109. app.route('/users2', callback=self.get_users2)
  110. app.route('/users/<id>', callback=self.get_user)
  111. app.route('/users/', callback=self.add_user, method='POST')
  112. app.route('/users/<id>', callback=self.del_user, method='DELETE')
  113. if __name__ == "__main__":
  114. app = bottle.Bottle()
  115. controllers = BottleController()
  116. controllers.bind(app)
  117. hapic.set_context(BottleContext(app))
  118. time.sleep(1)
  119. s = json.dumps(
  120. hapic.generate_doc(
  121. title='Fake API',
  122. description='just an example of hapic API'
  123. )
  124. )
  125. time.sleep(1)
  126. # print swagger doc
  127. print(s)
  128. # Run app
  129. app.run(host='localhost', port=8081, debug=True)