flask_api.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. import flask
  5. import time
  6. from datetime import datetime
  7. from hapic import Hapic
  8. from example.fake_api.schema import *
  9. from hapic.ext.flask import FlaskContext
  10. from hapic.data import HapicData
  11. hapic = Hapic()
  12. class FlaskController(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: flask.Flask):
  89. app.add_url_rule('/about',
  90. view_func=self.about)
  91. app.add_url_rule('/users',
  92. view_func=self.get_users)
  93. app.add_url_rule('/users/<id>',
  94. view_func=self.get_user)
  95. app.add_url_rule('/users/',
  96. view_func=self.add_user,
  97. methods=['POST'])
  98. app.add_url_rule('/users/<id>',
  99. view_func=self.del_user,
  100. methods=['DELETE'])
  101. if __name__ == "__main__":
  102. app = flask.Flask(__name__)
  103. controllers = FlaskController()
  104. controllers.bind(app)
  105. hapic.set_context(FlaskContext(app))
  106. time.sleep(1)
  107. s = json.dumps(
  108. hapic.generate_doc(
  109. title='Fake API',
  110. description='just an example of hapic API'
  111. )
  112. )
  113. time.sleep(1)
  114. # print swagger doc
  115. print(s)
  116. # Run app
  117. app.run(host='localhost', port=8082, debug=True)