serve_pyramid.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. try: # Python 3.5+
  4. from http import HTTPStatus
  5. except ImportError:
  6. from http import client as HTTPStatus
  7. import json
  8. from pyramid.config import Configurator
  9. import time
  10. from wsgiref.simple_server import make_server
  11. from hapic import Hapic
  12. from hapic.data import HapicData
  13. from hapic.ext.pyramid import PyramidContext
  14. from example.usermanagement.schema import AboutSchema
  15. from example.usermanagement.schema import NoContentSchema
  16. from example.usermanagement.schema import UserDigestSchema
  17. from example.usermanagement.schema import UserIdPathSchema
  18. from example.usermanagement.schema import UserSchema
  19. from example.usermanagement.userlib import User
  20. from example.usermanagement.userlib import UserLib
  21. from example.usermanagement.userlib import UserNotFound
  22. hapic = Hapic()
  23. class PyramidController(object):
  24. @hapic.with_api_doc()
  25. @hapic.output_body(AboutSchema())
  26. def about(self, context, request):
  27. """
  28. This endpoint allow to check that the API is running. This description
  29. is generated from the docstring of the method.
  30. """
  31. return {
  32. 'version': '1.2.3',
  33. 'datetime': datetime.now(),
  34. }
  35. @hapic.with_api_doc()
  36. @hapic.output_body(UserDigestSchema(many=True))
  37. def get_users(self, context, request):
  38. """
  39. Obtain users list.
  40. """
  41. return UserLib().get_users()
  42. @hapic.with_api_doc()
  43. @hapic.handle_exception(UserNotFound, HTTPStatus.NOT_FOUND)
  44. @hapic.input_path(UserIdPathSchema())
  45. @hapic.output_body(UserSchema())
  46. def get_user(self, context, request, hapic_data: HapicData):
  47. """
  48. Return a user taken from the list or return a 404
  49. """
  50. return UserLib().get_user(int(hapic_data.path['id']))
  51. @hapic.with_api_doc()
  52. # TODO - G.M - 2017-12-5 - Support input_forms ?
  53. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  54. @hapic.input_body(UserSchema(exclude=('id',)))
  55. @hapic.output_body(UserSchema())
  56. def add_user(self, context, request, hapic_data: HapicData):
  57. """
  58. Add a user to the list
  59. """
  60. print(hapic_data.body)
  61. new_user = User(**hapic_data.body)
  62. return UserLib().add_user(new_user)
  63. @hapic.with_api_doc()
  64. @hapic.handle_exception(UserNotFound, HTTPStatus.NOT_FOUND)
  65. @hapic.output_body(NoContentSchema(), default_http_code=204)
  66. @hapic.input_path(UserIdPathSchema())
  67. def del_user(self, context, request, hapic_data: HapicData):
  68. UserLib().del_user(int(hapic_data.path['id']))
  69. return NoContentSchema()
  70. def bind(self, configurator: Configurator):
  71. configurator.add_route('about', '/about', request_method='GET')
  72. configurator.add_view(self.about, route_name='about', renderer='json')
  73. configurator.add_route('get_users', '/users', request_method='GET') # nopep8
  74. configurator.add_view(self.get_users, route_name='get_users', renderer='json') # nopep8
  75. configurator.add_route('get_user', '/users/{id}', request_method='GET') # nopep8
  76. configurator.add_view(self.get_user, route_name='get_user', renderer='json') # nopep8
  77. configurator.add_route('add_user', '/users', request_method='POST') # nopep8
  78. configurator.add_view(self.add_user, route_name='add_user', renderer='json') # nopep8
  79. configurator.add_route('del_user', '/users/{id}', request_method='DELETE') # nopep8
  80. configurator.add_view(self.del_user, route_name='del_user', renderer='json') # nopep8
  81. if __name__ == "__main__":
  82. configurator = Configurator(autocommit=True)
  83. controllers = PyramidController()
  84. controllers.bind(configurator)
  85. hapic.set_context(PyramidContext(configurator))
  86. print('')
  87. print('')
  88. print('GENERATING OPENAPI DOCUMENTATION')
  89. openapi_file_name = 'api-documentation.json'
  90. with open(openapi_file_name, 'w') as openapi_file_handle:
  91. openapi_file_handle.write(
  92. json.dumps(
  93. hapic.generate_doc(
  94. title='Demo API documentation',
  95. description='This documentation has been generated from '
  96. 'code. You can see it using swagger: '
  97. 'http://editor2.swagger.io/'
  98. )
  99. )
  100. )
  101. print('Documentation generated in {}'.format(openapi_file_name))
  102. time.sleep(1)
  103. print('')
  104. print('')
  105. print('RUNNING PYRAMID SERVER NOW')
  106. # Run app
  107. server = make_server('127.0.0.1', 8083, configurator.make_wsgi_app())
  108. server.serve_forever()