mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 09:27:43 +00:00
openapi: Modify curl example generation logic.
This commit adds support for using the x-curl-examples-parameters parameter in OpenAPI data to fetch curl examples configuration. This also contains any descriptions necessary for each example, and directly generates all possible curl examples directly. A follow-up commit is needed to modify the templates accordingly.
This commit is contained in:
committed by
Tim Abbott
parent
a56ad2a26a
commit
246262fb57
@@ -21,6 +21,7 @@ import zerver.openapi.python_examples
|
|||||||
from zerver.openapi.openapi import (
|
from zerver.openapi.openapi import (
|
||||||
check_requires_administrator,
|
check_requires_administrator,
|
||||||
generate_openapi_fixture,
|
generate_openapi_fixture,
|
||||||
|
get_curl_include_exclude,
|
||||||
get_openapi_description,
|
get_openapi_description,
|
||||||
get_openapi_summary,
|
get_openapi_summary,
|
||||||
openapi_spec,
|
openapi_spec,
|
||||||
@@ -347,8 +348,6 @@ def generate_curl_example(
|
|||||||
def render_curl_example(
|
def render_curl_example(
|
||||||
function: str,
|
function: str,
|
||||||
api_url: str,
|
api_url: str,
|
||||||
exclude: Optional[List[str]] = None,
|
|
||||||
include: Optional[List[str]] = None,
|
|
||||||
) -> List[str]:
|
) -> List[str]:
|
||||||
"""A simple wrapper around generate_curl_example."""
|
"""A simple wrapper around generate_curl_example."""
|
||||||
parts = function.split(":")
|
parts = function.split(":")
|
||||||
@@ -360,9 +359,18 @@ def render_curl_example(
|
|||||||
if len(parts) > 3:
|
if len(parts) > 3:
|
||||||
kwargs["auth_api_key"] = parts[3]
|
kwargs["auth_api_key"] = parts[3]
|
||||||
kwargs["api_url"] = api_url
|
kwargs["api_url"] = api_url
|
||||||
kwargs["exclude"] = exclude
|
rendered_example = []
|
||||||
kwargs["include"] = include
|
for element in get_curl_include_exclude(endpoint, method):
|
||||||
return generate_curl_example(endpoint, method, **kwargs)
|
kwargs["include"] = None
|
||||||
|
kwargs["exclude"] = None
|
||||||
|
if element["type"] == "include":
|
||||||
|
kwargs["include"] = element["parameters"]["enum"]
|
||||||
|
if element["type"] == "exclude":
|
||||||
|
kwargs["exclude"] = element["parameters"]["enum"]
|
||||||
|
if "description" in element:
|
||||||
|
rendered_example.extend(element["description"].splitlines())
|
||||||
|
rendered_example = rendered_example + generate_curl_example(endpoint, method, **kwargs)
|
||||||
|
return rendered_example
|
||||||
|
|
||||||
|
|
||||||
SUPPORTED_LANGUAGES: Dict[str, Any] = {
|
SUPPORTED_LANGUAGES: Dict[str, Any] = {
|
||||||
|
|||||||
@@ -214,6 +214,18 @@ def get_openapi_fixture_description(endpoint: str, method: str, status_code: str
|
|||||||
return get_schema(endpoint, method, status_code)["description"]
|
return get_schema(endpoint, method, status_code)["description"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_curl_include_exclude(endpoint: str, method: str) -> List[Dict[str, Any]]:
|
||||||
|
"""Fetch all the kinds of parameters required for curl examples."""
|
||||||
|
if (
|
||||||
|
"x-curl-examples-parameters"
|
||||||
|
not in openapi_spec.openapi()["paths"][endpoint][method.lower()]
|
||||||
|
):
|
||||||
|
return [{"type": "exclude", "parameters": {"enum": [""]}}]
|
||||||
|
return openapi_spec.openapi()["paths"][endpoint][method.lower()]["x-curl-examples-parameters"][
|
||||||
|
"oneOf"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def check_requires_administrator(endpoint: str, method: str) -> bool:
|
def check_requires_administrator(endpoint: str, method: str) -> bool:
|
||||||
"""Fetch if the endpoint requires admin config."""
|
"""Fetch if the endpoint requires admin config."""
|
||||||
return openapi_spec.openapi()["paths"][endpoint][method.lower()].get(
|
return openapi_spec.openapi()["paths"][endpoint][method.lower()].get(
|
||||||
|
|||||||
Reference in New Issue
Block a user