pyramid_api.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.output_body(UserSchema(
  47. many=True,
  48. only=('id', 'username', 'display_name', 'company')
  49. ))
  50. def get_users2(self, context, request):
  51. """
  52. Obtain users list.
  53. """
  54. return [
  55. {
  56. 'id': 4,
  57. 'username': 'some_user',
  58. 'display_name': 'Damien Accorsi',
  59. 'company': 'Algoo',
  60. }
  61. ]
  62. @hapic.with_api_doc()
  63. @hapic.input_path(UserPathSchema())
  64. @hapic.output_body(UserSchema())
  65. def get_user(self, context, request, hapic_data: HapicData):
  66. """
  67. Obtain one user
  68. """
  69. return {
  70. 'id': 4,
  71. 'username': 'some_user',
  72. 'email_address': 'some.user@hapic.com',
  73. 'first_name': 'Damien',
  74. 'last_name': 'Accorsi',
  75. 'display_name': 'Damien Accorsi',
  76. 'company': 'Algoo',
  77. }
  78. @hapic.with_api_doc()
  79. # TODO - G.M - 2017-12-5 - Support input_forms ?
  80. # TODO - G.M - 2017-12-5 - Support exclude, only ?
  81. @hapic.input_body(UserSchema(exclude=('id',)))
  82. @hapic.output_body(UserSchema())
  83. def add_user(self, context, request, hapic_data: HapicData):
  84. """
  85. Add new user
  86. """
  87. return {
  88. 'id': 4,
  89. 'username': 'some_user',
  90. 'email_address': 'some.user@hapic.com',
  91. 'first_name': 'Damien',
  92. 'last_name': 'Accorsi',
  93. 'display_name': 'Damien Accorsi',
  94. 'company': 'Algoo',
  95. }
  96. @hapic.with_api_doc()
  97. @hapic.output_body(NoContentSchema(),
  98. default_http_code=204)
  99. @hapic.input_path(UserPathSchema())
  100. def del_user(self, context, request, hapic_data: HapicData):
  101. """
  102. delete user
  103. """
  104. return NoContentSchema()
  105. def bind(self, configurator: Configurator):
  106. configurator.add_route('about', '/about', request_method='GET')
  107. configurator.add_view(self.about, route_name='about', renderer='json')
  108. configurator.add_route('get_users', '/users', request_method='GET') # nopep8
  109. configurator.add_view(self.get_users, route_name='get_users', renderer='json') # nopep8
  110. configurator.add_route('get_users2', '/users2', request_method='GET') # nopep8
  111. configurator.add_view(self.get_users2, route_name='get_users2', renderer='json') # nopep8
  112. configurator.add_route('get_user', '/users/{id}', request_method='GET') # nopep8
  113. configurator.add_view(self.get_user, route_name='get_user', renderer='json') # nopep8
  114. configurator.add_route('add_user', '/users/', request_method='POST') # nopep8
  115. configurator.add_view(self.add_user, route_name='add_user', renderer='json') # nopep8
  116. configurator.add_route('del_user', '/users/{id}', request_method='DELETE') # nopep8
  117. configurator.add_view(self.del_user, route_name='del_user', renderer='json') # nopep8
  118. if __name__ == "__main__":
  119. configurator = Configurator(autocommit=True)
  120. controllers = PyramidController()
  121. controllers.bind(configurator)
  122. hapic.set_context(PyramidContext(configurator))
  123. time.sleep(1)
  124. s = json.dumps(
  125. hapic.generate_doc(
  126. title='Fake API',
  127. description='just an example of hapic API'
  128. )
  129. )
  130. time.sleep(1)
  131. # print swagger doc
  132. print(s)
  133. # Run app
  134. server = make_server('0.0.0.0', 8083, configurator.make_wsgi_app())
  135. server.serve_forever()