example_a_pyramid.py 3.3KB

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