flask_api.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. resp = flask.Response(
  87. status=204,
  88. )
  89. # See tests.func.fake_api.test_flask.test_func_flask_fake_api
  90. del resp.headers['Content-Type']
  91. return resp
  92. def bind(self, app: flask.Flask):
  93. app.add_url_rule('/about',
  94. view_func=self.about)
  95. app.add_url_rule('/users',
  96. view_func=self.get_users)
  97. app.add_url_rule('/users/<id>',
  98. view_func=self.get_user)
  99. app.add_url_rule('/users/',
  100. view_func=self.add_user,
  101. methods=['POST'])
  102. app.add_url_rule('/users/<id>',
  103. view_func=self.del_user,
  104. methods=['DELETE'])
  105. if __name__ == "__main__":
  106. app = flask.Flask(__name__)
  107. controllers = FlaskController()
  108. controllers.bind(app)
  109. hapic.set_context(FlaskContext(app))
  110. time.sleep(1)
  111. s = json.dumps(
  112. hapic.generate_doc(
  113. title='Fake API',
  114. description='just an example of hapic API'
  115. )
  116. )
  117. time.sleep(1)
  118. # print swagger doc
  119. print(s)
  120. # Run app
  121. app.run(host='localhost', port=8082, debug=True)