pyramid_api.py 4.1KB

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