example_a.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. import json
  3. from http import HTTPStatus
  4. import bottle
  5. import time
  6. import yaml
  7. import hapic
  8. from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema, \
  9. ErrorResponseSchema, HelloQuerySchema
  10. from hapic.data import HapicData
  11. app = bottle.Bottle()
  12. # hapic.global_exception_handler(UnAuthExc, StandardErrorSchema)
  13. # hapic.global_exception_handler(UnAuthExc2, StandardErrorSchema)
  14. # hapic.global_exception_handler(UnAuthExc3, StandardErrorSchema)
  15. bottle.default_app.push(app)
  16. hapic.set_context(hapic.ext.bottle.bottle_context)
  17. def bob(f):
  18. def boby(*args, **kwargs):
  19. return f(*args, **kwargs)
  20. return boby
  21. @hapic.with_api_doc()
  22. # @hapic.ext.bottle.bottle_context()
  23. @hapic.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST)
  24. @hapic.input_path(HelloPathSchema())
  25. @hapic.input_query(HelloQuerySchema())
  26. @hapic.output_body(HelloResponseSchema())
  27. def hello(name: str, hapic_data: HapicData):
  28. """
  29. my endpoint hello
  30. ---
  31. get:
  32. description: my description
  33. parameters:
  34. - in: "path"
  35. description: "hello"
  36. name: "name"
  37. type: "string"
  38. responses:
  39. 200:
  40. description: A pet to be returned
  41. schema: HelloResponseSchema
  42. """
  43. if name == 'zero':
  44. raise ZeroDivisionError('Don\'t call him zero !')
  45. return {
  46. 'sentence': 'Hello !',
  47. 'name': name,
  48. }
  49. @hapic.with_api_doc()
  50. # @hapic.ext.bottle.bottle_context()
  51. # @hapic.error_schema(ErrorResponseSchema())
  52. @hapic.input_path(HelloPathSchema())
  53. @hapic.input_body(HelloJsonSchema())
  54. @hapic.output_body(HelloResponseSchema())
  55. @bob
  56. def hello2(name: str, hapic_data: HapicData):
  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(name: str):
  69. return {
  70. 'sentence': 'Hello !',
  71. 'name': name,
  72. }
  73. app.route('/hello/<name>', callback=hello)
  74. app.route('/hello/<name>', callback=hello2, method='POST')
  75. app.route('/hello3/<name>', callback=hello3)
  76. # time.sleep(1)
  77. # s = hapic.generate_doc(app)
  78. # ss = json.loads(json.dumps(s))
  79. # for path in ss['paths']:
  80. # for method in ss['paths'][path]:
  81. # for response_code in ss['paths'][path][method]['responses']:
  82. # ss['paths'][path][method]['responses'][int(response_code)] = ss['paths'][path][method]['responses'][response_code]
  83. # del ss['paths'][path][method]['responses'][int(response_code)]
  84. # print(yaml.dump(ss, default_flow_style=False))
  85. # time.sleep(1)
  86. print(json.dumps(hapic.generate_doc()))
  87. app.run(host='localhost', port=8080, debug=True)