コード例 #1
0
def _handler_apply_params(method_yaml: dict, doc_info: DocInfo) -> None:
    # REQ PARAMS
    params: List[dict] = list()

    for param_info in doc_info.req_params:
        params.append(param_info.openapi_spec())

    if params:
        method_yaml["parameters"] = params

    # RESP PARAMS
    for http_code, response_info in doc_info.responses.items():
        headers: Dict[str, dict] = dict()

        for param_info in response_info.params:
            param_block = param_info.openapi_spec()
            param_block.pop("in")

            name = param_block.pop("name")
            headers[name] = param_block

        if headers:
            course = (
                gemma.PORT
                / gemma.Item("responses")
                / gemma.Item(http_code, factory=dict)
                / gemma.Item("headers")
            )
            course.place(method_yaml, headers)
コード例 #2
0
def _apply_example_data(
    method_yaml: dict,
    example_data: Any,
    schema: Optional[Union[marshmallow.Schema, MimeType]],
    resp_code: Optional[int],
) -> None:
    if isinstance(schema, marshmallow.Schema) and example_data is not None:
        example_data = schema.dump(example_data)

    if isinstance(example_data, (dict, list)):
        content_type = MimeType.JSON.value
    else:
        content_type = MimeType.TEXT.value

    if resp_code is not None:
        course = (
            gemma.PORT
            / gemma.Item("responses", factory=dict)
            / gemma.Item(resp_code, factory=dict)
        )
    else:
        course = gemma.PORT / gemma.Item("requestBody", factory=dict)

    course = (
        course
        / gemma.Item("content", factory=dict)
        / gemma.Item(content_type, factory=dict)
        / gemma.Item("example")
    )
    course.place(method_yaml, example_data)
コード例 #3
0
def _handler_create_responses(method_yaml: dict, doc_info: DocInfo) -> None:
    existing_responses = method_yaml.get("responses", dict())
    all_defined = [c for c in existing_responses] + [c for c in doc_info.responses]

    # If no responses are defined, we create a default ok response.
    if not any(code for code in all_defined if not _is_error_code(code)):
        existing_responses[200] = dict()

    # If no error responses are defined, we create a default error response. This
    # response code will be subbed out later for a "Default" error code.
    if not any(code for code in all_defined if _is_error_code(code)):
        existing_responses[DEFAULT_RESP_CODE] = dict()

    _create_doc_info_for_docstring_responses(existing_responses, doc_info)

    # Now we go through and create or merge documentation info that was hand-typed into
    # the docstring with the DocInfo object in the route's "Document" class.
    for http_code, response_info in doc_info.responses.items():
        try:
            response_block = method_yaml["responses"][http_code]
        except KeyError:
            course = (
                gemma.PORT
                / gemma.Item("responses", factory=dict)
                / gemma.Item(http_code, factory=dict)
            )
            response_block = dict()
            course.place(method_yaml, response_block)

        # Apply generic descriptions to those missing it.
        if response_info.description is None:
            if http_code == 201:
                response_info.description = "Created."
            elif not _is_error_code(http_code):
                response_info.description = "Ok."
            else:
                response_info.description = "Error."

        response_block["description"] = response_info.description
コード例 #4
0
def _apply_schema_to_code(
    schema: Union[marshmallow.Schema, MimeType],
    schema_name: str,
    code: int,
    http_block: str,
    method_yaml: dict,
    req_resp: str,
) -> None:
    # We don't apply schemas to error codes.
    if code != -1 and _is_error_code(code):
        return None

    types_course = gemma.PORT / gemma.Item(http_block, factory=dict)

    if req_resp == "resp":
        types_course = types_course / gemma.Item(code, factory=dict)

    types_course = types_course / gemma.Item("content", factory=dict)

    try:
        types_blocks = types_course.fetch(method_yaml)
    except gemma.NullNameError:
        # If there are not listed content types, that means no example data was
        # applied, so we need to generate the type blocks.
        types_blocks = dict()
        if isinstance(schema, marshmallow.Schema):
            types_blocks[MimeType.JSON.value] = dict()
        else:
            types_blocks[MimeType.TEXT.value] = dict()
        types_course.place(method_yaml, types_blocks)

    for type_block in types_blocks.values():
        if isinstance(schema, marshmallow.Schema):
            schema_link = f"#/components/schemas/{schema_name}"
            type_block["schema"] = {"$ref": schema_link}
        else:
            type_block["schema"] = {"type": "string"}
コード例 #5
0
def get_route_resp_example(route: dict,
                           code: int,
                           content_type: str = MimeType.JSON.value):
    course = (gemma.PORT / "get" / "responses" / str(code) / "content" /
              gemma.Item(content_type) / "example")
    return course.fetch(route)
コード例 #6
0
def get_route_req_example(route: dict,
                          content_type: str = MimeType.JSON.value):
    course = (gemma.PORT / "get" / "requestBody" / "content" /
              gemma.Item(content_type) / "example")
    return course.fetch(route)