| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | # -*- coding: utf-8 -*-
import json
from http import HTTPStatus
import bottle
import time
import yaml
import hapic
from example import HelloResponseSchema, HelloPathSchema, HelloJsonSchema, \
    ErrorResponseSchema, HelloQuerySchema
from hapic.data import HapicData
app = bottle.Bottle()
# hapic.global_exception_handler(UnAuthExc, StandardErrorSchema)
# hapic.global_exception_handler(UnAuthExc2, StandardErrorSchema)
# hapic.global_exception_handler(UnAuthExc3, StandardErrorSchema)
bottle.default_app.push(app)
hapic.set_context(hapic.ext.bottle.bottle_context)
def bob(f):
    def boby(*args, **kwargs):
        return f(*args, **kwargs)
    return boby
@hapic.with_api_doc()
# @hapic.ext.bottle.bottle_context()
@hapic.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST)
@hapic.input_path(HelloPathSchema())
@hapic.input_query(HelloQuerySchema())
@hapic.output_body(HelloResponseSchema())
def hello(name: str, hapic_data: HapicData):
    """
    my endpoint hello
    ---
    get:
        description: my description
        parameters:
            - in: "path"
              description: "hello"
              name: "name"
              type: "string"
        responses:
            200:
                description: A pet to be returned
                schema: HelloResponseSchema
    """
    if name == 'zero':
        raise ZeroDivisionError('Don\'t call him zero !')
    return {
        'sentence': 'Hello !',
        'name': name,
    }
@hapic.with_api_doc()
# @hapic.ext.bottle.bottle_context()
# @hapic.error_schema(ErrorResponseSchema())
@hapic.input_path(HelloPathSchema())
@hapic.input_body(HelloJsonSchema())
@hapic.output_body(HelloResponseSchema())
@bob
def hello2(name: str, hapic_data: HapicData):
    return {
        'sentence': 'Hello !',
        'name': name,
        'color': hapic_data.body.get('color'),
    }
kwargs = {'validated_data': {'name': 'bob'}, 'name': 'bob'}
@hapic.with_api_doc()
# @hapic.ext.bottle.bottle_context()
# @hapic.error_schema(ErrorResponseSchema())
@hapic.input_path(HelloPathSchema())
@hapic.output_body(HelloResponseSchema())
def hello3(name: str):
    return {
        'sentence': 'Hello !',
        'name': name,
    }
app.route('/hello/<name>', callback=hello)
app.route('/hello/<name>', callback=hello2, method='POST')
app.route('/hello3/<name>', callback=hello3)
# time.sleep(1)
# s = hapic.generate_doc(app)
# ss = json.loads(json.dumps(s))
# for path in ss['paths']:
#     for method in ss['paths'][path]:
#         for response_code in ss['paths'][path][method]['responses']:
#             ss['paths'][path][method]['responses'][int(response_code)] = ss['paths'][path][method]['responses'][response_code]
#             del ss['paths'][path][method]['responses'][int(response_code)]
# print(yaml.dump(ss, default_flow_style=False))
# time.sleep(1)
print(json.dumps(hapic.generate_doc()))
app.run(host='localhost', port=8080, debug=True)
 |