pyramid_api.py 4.1KB

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