example_a.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. def bob(f):
  13. def boby(*args, **kwargs):
  14. return f(*args, **kwargs)
  15. return boby
  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(name: str, 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. if name == 'zero':
  39. raise ZeroDivisionError('Don\'t call him zero !')
  40. return {
  41. 'sentence': 'Hello !',
  42. 'name': name,
  43. }
  44. @hapic.with_api_doc()
  45. # @hapic.ext.bottle.bottle_context()
  46. # @hapic.error_schema(ErrorResponseSchema())
  47. @hapic.input_path(HelloPathSchema())
  48. @hapic.input_body(HelloJsonSchema())
  49. @hapic.output_body(HelloResponseSchema())
  50. @bob
  51. def hello2(name: str, hapic_data: HapicData):
  52. return {
  53. 'sentence': 'Hello !',
  54. 'name': name,
  55. 'color': hapic_data.body.get('color'),
  56. }
  57. kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
  58. @hapic.with_api_doc()
  59. # @hapic.ext.bottle.bottle_context()
  60. # @hapic.error_schema(ErrorResponseSchema())
  61. @hapic.input_path(HelloPathSchema())
  62. @hapic.output_body(HelloResponseSchema())
  63. def hello3(name: str):
  64. return {
  65. 'sentence': 'Hello !',
  66. 'name': name,
  67. }
  68. app.route('/hello/<name>', callback=hello)
  69. app.route('/hello/<name>', callback=hello2, method='POST')
  70. app.route('/hello3/<name>', callback=hello3)
  71. # time.sleep(1)
  72. # s = hapic.generate_doc(app)
  73. # ss = json.loads(json.dumps(s))
  74. # for path in ss['paths']:
  75. # for method in ss['paths'][path]:
  76. # for response_code in ss['paths'][path][method]['responses']:
  77. # ss['paths'][path][method]['responses'][int(response_code)] = ss['paths'][path][method]['responses'][response_code]
  78. # del ss['paths'][path][method]['responses'][int(response_code)]
  79. # print(yaml.dump(ss, default_flow_style=False))
  80. # time.sleep(1)
  81. print(json.dumps(hapic.generate_doc(app)))
  82. app.run(host='localhost', port=8080, debug=True)