pyramid_api.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. from pyramid.response import Response
  5. from pyramid.config import Configurator
  6. from wsgiref.simple_server import make_server
  7. import time
  8. from datetime import datetime
  9. import hapic
  10. from example.fake_api.schema import *
  11. from hapic.data import HapicData
  12. class NoContentException(Exception):
  13. pass
  14. class PyramidController(object):
  15. @hapic.with_api_doc()
  16. @hapic.output_body(AboutResponseSchema())
  17. def about(self, context, request):
  18. """
  19. General information about this API.
  20. """
  21. return {
  22. 'version': '1.2.3',
  23. 'datetime': datetime(2017, 12, 7, 10, 55, 8, 488996),
  24. }
  25. @hapic.with_api_doc()
  26. @hapic.output_body(ListsUserSchema())
  27. def get_users(self, context, request):
  28. """
  29. Obtain users list.
  30. """
  31. return {
  32. 'item_nb': 1,
  33. 'items': [
  34. {
  35. 'id': 4,
  36. 'username': 'some_user',
  37. 'display_name': 'Damien Accorsi',
  38. 'company': 'Algoo',
  39. },
  40. ],
  41. 'pagination': {
  42. 'first_id': 0,
  43. 'last_id': 5,
  44. 'current_id': 0,
  45. }
  46. }
  47. @hapic.with_api_doc()
  48. @hapic.input_path(UserPathSchema())
  49. @hapic.output_body(UserSchema())
  50. def get_user(self, context, request, hapic_data: HapicData):
  51. """
  52. Obtain one user
  53. """
  54. return {
  55. 'id': 4,
  56. 'username': 'some_user',
  57. 'email_address': 'some.user@hapic.com',
  58. 'first_name': 'Damien',
  59. 'last_name': 'Accorsi',
  60. 'display_name': 'Damien Accorsi',
  61. 'company': 'Algoo',
  62. }
  63. @hapic.with_api_doc()
  64. # TODO - G.M - 2017-12-5 - Support input_forms ?
  65. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  66. @hapic.input_body(UserSchema(exclude=('id',)))
  67. @hapic.output_body(UserSchema())
  68. def add_user(self, context, request, hapic_data: HapicData):
  69. """
  70. Add new user
  71. """
  72. return {
  73. 'id': 4,
  74. 'username': 'some_user',
  75. 'email_address': 'some.user@hapic.com',
  76. 'first_name': 'Damien',
  77. 'last_name': 'Accorsi',
  78. 'display_name': 'Damien Accorsi',
  79. 'company': 'Algoo',
  80. }
  81. @hapic.with_api_doc()
  82. @hapic.handle_exception(NoContentException, http_code=HTTPStatus.NO_CONTENT)
  83. @hapic.input_path(UserPathSchema())
  84. def del_user(self, context, request, hapic_data: HapicData):
  85. """
  86. delete user
  87. """
  88. # TODO - G.M - 2017-12-05 - Add better
  89. # way to doc response of this function, using response object ?
  90. # return Response(
  91. # status_code=204,
  92. # )
  93. raise NoContentException
  94. def bind(self, configurator: Configurator):
  95. configurator.add_route('about', '/about', request_method='GET')
  96. configurator.add_view(self.about, route_name='about', renderer='json')
  97. configurator.add_route('get_users', '/users', request_method='GET') # nopep8
  98. configurator.add_view(self.get_users, route_name='get_users', renderer='json') # nopep8
  99. configurator.add_route('get_user', '/users/{id}', request_method='GET') # nopep8
  100. configurator.add_view(self.get_user, route_name='get_user', renderer='json') # nopep8
  101. configurator.add_route('add_user', '/users/', request_method='POST') # nopep8
  102. configurator.add_view(self.add_user, route_name='add_user', renderer='json') # nopep8
  103. configurator.add_route('del_user', '/users/{id}', request_method='DELETE') # nopep8
  104. configurator.add_view(self.del_user, route_name='del_user', renderer='json') # nopep8
  105. if __name__ == "__main__":
  106. configurator = Configurator(autocommit=True)
  107. controllers = PyramidController()
  108. controllers.bind(configurator)
  109. hapic.set_context(hapic.ext.pyramid.PyramidContext(configurator))
  110. time.sleep(1)
  111. s = json.dumps(hapic.generate_doc())
  112. time.sleep(1)
  113. # print swagger doc
  114. print(s)
  115. # Run app
  116. server = make_server('0.0.0.0', 8083, configurator.make_wsgi_app())
  117. server.serve_forever()