flask_api.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.output_body(UserSchema(
  46. many=True,
  47. only=('id', 'username', 'display_name', 'company')
  48. ))
  49. def get_users2(self):
  50. """
  51. Obtain users list.
  52. """
  53. return [
  54. {
  55. 'id': 4,
  56. 'username': 'some_user',
  57. 'display_name': 'Damien Accorsi',
  58. 'company': 'Algoo',
  59. }
  60. ]
  61. @hapic.with_api_doc()
  62. @hapic.input_path(UserPathSchema())
  63. @hapic.output_body(UserSchema())
  64. def get_user(self, id, hapic_data: HapicData):
  65. """
  66. Obtain one user
  67. """
  68. return {
  69. 'id': 4,
  70. 'username': 'some_user',
  71. 'email_address': 'some.user@hapic.com',
  72. 'first_name': 'Damien',
  73. 'last_name': 'Accorsi',
  74. 'display_name': 'Damien Accorsi',
  75. 'company': 'Algoo',
  76. }
  77. @hapic.with_api_doc()
  78. # TODO - G.M - 2017-12-5 - Support input_forms ?
  79. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  80. @hapic.input_body(UserSchema(exclude=('id',)))
  81. @hapic.output_body(UserSchema())
  82. def add_user(self, hapic_data: HapicData):
  83. """
  84. Add new user
  85. """
  86. return {
  87. 'id': 4,
  88. 'username': 'some_user',
  89. 'email_address': 'some.user@hapic.com',
  90. 'first_name': 'Damien',
  91. 'last_name': 'Accorsi',
  92. 'display_name': 'Damien Accorsi',
  93. 'company': 'Algoo',
  94. }
  95. @hapic.with_api_doc()
  96. @hapic.output_body(NoContentSchema(),
  97. default_http_code=204)
  98. @hapic.input_path(UserPathSchema())
  99. def del_user(self, id, hapic_data: HapicData):
  100. """
  101. delete user
  102. """
  103. resp = flask.Response(
  104. status=204,
  105. )
  106. # See tests.func.fake_api.test_flask.test_func_flask_fake_api
  107. del resp.headers['Content-Type']
  108. return resp
  109. def bind(self, app: flask.Flask):
  110. app.add_url_rule('/about',
  111. view_func=self.about)
  112. app.add_url_rule('/users',
  113. view_func=self.get_users)
  114. app.add_url_rule('/users2',
  115. view_func=self.get_users2)
  116. app.add_url_rule('/users/<id>',
  117. view_func=self.get_user)
  118. app.add_url_rule('/users/',
  119. view_func=self.add_user,
  120. methods=['POST'])
  121. app.add_url_rule('/users/<id>',
  122. view_func=self.del_user,
  123. methods=['DELETE'])
  124. if __name__ == "__main__":
  125. app = flask.Flask(__name__)
  126. controllers = FlaskController()
  127. controllers.bind(app)
  128. hapic.set_context(FlaskContext(app))
  129. time.sleep(1)
  130. s = json.dumps(
  131. hapic.generate_doc(
  132. title='Fake API',
  133. description='just an example of hapic API'
  134. )
  135. )
  136. time.sleep(1)
  137. # print swagger doc
  138. print(s)
  139. # Run app
  140. app.run(host='localhost', port=8082, debug=True)