コード例 #1
0
def add_responses(swagger_operation, operation, ns, func):
    """
    Add responses to an operation.

    """
    # default error
    swagger_operation.responses["default"] = build_response(
        description="An error occcurred",
        resource=type_name(name_for(ErrorSchema())),
    )

    if getattr(func, "__doc__", None):
        description = func.__doc__.strip().splitlines()[0]
    else:
        description = "{} {}".format(operation.value.name, ns.subject_name)

    # resource request
    request_resource = get_request_schema(func)
    if isinstance(request_resource, string_types):
        if not hasattr(swagger_operation, "consumes"):
            swagger_operation.consumes = []
        swagger_operation.consumes.append(request_resource)

    # resources response
    response_resource = get_response_schema(func)
    if isinstance(response_resource, string_types):
        if not hasattr(swagger_operation, "produces"):
            swagger_operation.produces = []
        swagger_operation.produces.append(response_resource)
    else:
        swagger_operation.responses[str(operation.value.default_code)] = build_response(
            description=description,
            resource=response_resource,
        )
コード例 #2
0
def iter_definitions(definitions, operations):
    """
    Generate definitions to be converted to swagger schema.

    """
    # general error schema per errors.py
    for error_schema_class in [ErrorSchema, ErrorContextSchema, SubErrorSchema]:
        yield error_schema_class()

    # add all request and response schemas
    for operation, obj, rule, func in operations:
        yield get_request_schema(func)
        yield get_response_schema(func)
コード例 #3
0
ファイル: definitions.py プロジェクト: Sinon/microcosm-flask
def iter_definitions(definitions, operations):
    """
    Generate definitions to be converted to swagger schema.

    """
    # general error schema per errors.py
    for error_schema_class in [ErrorSchema, ErrorContextSchema, SubErrorSchema]:
        yield error_schema_class(), RequestSide.BOTH

    # add all request and response schemas
    for operation, obj, rule, func in operations:
        yield get_request_schema(func), RequestSide.REQUEST
        yield get_response_schema(func), RequestSide.RESPONSE
コード例 #4
0
def add_responses(swagger_operation, operation, ns, func):
    """
    Add responses to an operation.

    """
    # default error
    swagger_operation.responses["default"] = build_response(
        description="An error occurred",
        resource=type_name(name_for(ErrorSchema())),
    )

    if getattr(func, "__doc__", None):
        description = func.__doc__.strip().splitlines()[0]
    else:
        description = "{} {}".format(operation.value.name, ns.subject_name)

    if operation in (Operation.Upload, Operation.UploadFor):
        swagger_operation.consumes = [
            "multipart/form-data"
        ]

    # resource request
    request_resource = get_request_schema(func)
    if isinstance(request_resource, str):
        if not hasattr(swagger_operation, "consumes"):
            swagger_operation.consumes = []
        swagger_operation.consumes.append(request_resource)

    # resources response
    response_resource = get_response_schema(func)
    if isinstance(response_resource, str):
        if not hasattr(swagger_operation, "produces"):
            swagger_operation.produces = []
        swagger_operation.produces.append(response_resource)
    elif not response_resource:
        response_code = (
            204
            if operation.value.default_code == 200
            else operation.value.default_code
        )
        swagger_operation.responses[str(response_code)] = build_response(
            description=description,
        )
    else:
        swagger_operation.responses[str(operation.value.default_code)] = build_response(
            description=description,
            resource=response_resource,
        )