example_a_pyramid.py 3.2KB

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