pyramid_api.py 4.1KB

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. from hapic import Hapic
  10. from example.fake_api.schema import *
  11. from hapic.data import HapicData
  12. from hapic.ext.pyramid import PyramidContext
  13. hapic = Hapic()
  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.output_body(NoContentSchema(),
  83. default_http_code=204)
  84. @hapic.input_path(UserPathSchema())
  85. def del_user(self, context, request, hapic_data: HapicData):
  86. """
  87. delete user
  88. """
  89. return NoContentSchema()
  90. def bind(self, configurator: Configurator):
  91. configurator.add_route('about', '/about', request_method='GET')
  92. configurator.add_view(self.about, route_name='about', renderer='json')
  93. configurator.add_route('get_users', '/users', request_method='GET') # nopep8
  94. configurator.add_view(self.get_users, route_name='get_users', renderer='json') # nopep8
  95. configurator.add_route('get_user', '/users/{id}', request_method='GET') # nopep8
  96. configurator.add_view(self.get_user, route_name='get_user', renderer='json') # nopep8
  97. configurator.add_route('add_user', '/users/', request_method='POST') # nopep8
  98. configurator.add_view(self.add_user, route_name='add_user', renderer='json') # nopep8
  99. configurator.add_route('del_user', '/users/{id}', request_method='DELETE') # nopep8
  100. configurator.add_view(self.del_user, route_name='del_user', renderer='json') # nopep8
  101. if __name__ == "__main__":
  102. configurator = Configurator(autocommit=True)
  103. controllers = PyramidController()
  104. controllers.bind(configurator)
  105. hapic.set_context(PyramidContext(configurator))
  106. time.sleep(1)
  107. s = json.dumps(
  108. hapic.generate_doc(
  109. title='Fake API',
  110. description='just an example of hapic API'
  111. )
  112. )
  113. time.sleep(1)
  114. # print swagger doc
  115. print(s)
  116. # Run app
  117. server = make_server('0.0.0.0', 8083, configurator.make_wsgi_app())
  118. server.serve_forever()