example_a2.py 4.3KB

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