example_a_pyramid.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. #import bottle
  5. from pyramid.config import Configurator
  6. from wsgiref.simple_server import make_server
  7. import time
  8. import yaml
  9. import uuid
  10. from beaker.middleware import SessionMiddleware
  11. import hapic
  12. from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema, \
  13. ErrorResponseSchema, HelloQuerySchema
  14. from hapic.data import HapicData
  15. # hapic.global_exception_handler(UnAuthExc, StandardErrorSchema)
  16. # hapic.global_exception_handler(UnAuthExc2, StandardErrorSchema)
  17. # hapic.global_exception_handler(UnAuthExc3, StandardErrorSchema)
  18. # bottle.default_app.push(app)
  19. # session_opts = {
  20. # 'session.type': 'file',
  21. # 'session.data_dir': '/tmp',
  22. # 'session.cookie_expires': 3600,
  23. # 'session.auto': True
  24. # }
  25. # session_middleware = SessionMiddleware(
  26. # app,
  27. # session_opts,
  28. # environ_key='beaker.session',
  29. # key='beaker.session.id',
  30. # )
  31. # app = session_middleware.wrap_app
  32. def bob(f):
  33. def boby(*args, **kwargs):
  34. return f(*args, **kwargs)
  35. return boby
  36. class Controllers(object):
  37. @hapic.with_api_doc()
  38. # @hapic.ext.bottle.bottle_context()
  39. @hapic.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST)
  40. @hapic.input_path(HelloPathSchema())
  41. @hapic.input_query(HelloQuerySchema())
  42. @hapic.output_body(HelloResponseSchema())
  43. def hello(self, context, request, hapic_data: HapicData):
  44. """
  45. my endpoint hello
  46. ---
  47. get:
  48. description: my description
  49. parameters:
  50. - in: "path"
  51. description: "hello"
  52. name: "name"
  53. type: "string"
  54. responses:
  55. 200:
  56. description: A pet to be returned
  57. schema: HelloResponseSchema
  58. """
  59. name = request.matchdict.get('name', None)
  60. if name == 'zero':
  61. raise ZeroDivisionError('Don\'t call him zero !')
  62. return {
  63. 'sentence': 'Hello !',
  64. 'name': name,
  65. }
  66. @hapic.with_api_doc()
  67. # @hapic.ext.bottle.bottle_context()
  68. # @hapic.error_schema(ErrorResponseSchema())
  69. @hapic.input_path(HelloPathSchema())
  70. @hapic.input_body(HelloJsonSchema())
  71. @hapic.output_body(HelloResponseSchema())
  72. @bob
  73. def hello2(self, context, request, hapic_data: HapicData):
  74. name = request.matchdict.get('name', None)
  75. return {
  76. 'sentence': 'Hello !',
  77. 'name': name,
  78. 'color': hapic_data.body.get('color'),
  79. }
  80. kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
  81. @hapic.with_api_doc()
  82. # @hapic.ext.bottle.bottle_context()
  83. # @hapic.error_schema(ErrorResponseSchema())
  84. @hapic.input_path(HelloPathSchema())
  85. @hapic.output_body(HelloResponseSchema())
  86. def hello3(self, context, request, hapic_data: HapicData):
  87. name = request.matchdict.get('name', None)
  88. return {
  89. 'sentence': 'Hello !',
  90. 'name': name,
  91. }
  92. def bind(self, configurator: Configurator):
  93. configurator.add_route('hello', '/hello/{name}', request_method='GET')
  94. configurator.add_view(self.hello, route_name='hello', renderer='json')
  95. configurator.add_route('hello2', '/hello/{name}', request_method='POST') # nopep8
  96. configurator.add_view(self.hello2, route_name='hello2', renderer='json') # nopep8
  97. configurator.add_route('hello3', '/hello3/{name}', request_method='GET') # nopep8
  98. configurator.add_view(self.hello3, route_name='hello3', renderer='json') # nopep8
  99. configurator = Configurator(autocommit=True)
  100. controllers = Controllers()
  101. controllers.bind(configurator)
  102. hapic.set_context(hapic.ext.pyramid.PyramidContext(configurator))
  103. print(json.dumps(hapic.generate_doc()))
  104. server = make_server('0.0.0.0', 8080, configurator.make_wsgi_app())
  105. server.serve_forever()