flask_api.py 3.5KB

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