Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
curl https://api-ch-gva-2.exoscale.com/v2/openapi.json | ./.sort-enums.py | jq > exoscale/openapi.json

curl https://partner-api.exoscale.com/v1.alpha/openapi.json | ./.sort-enums.py | jq > exoscale/partner-api.json
- uses: astral-sh/setup-uv@v7
with:
enable-cache: true
- name: Verify with tests
run: uv run pytest
- name: Commit and push if changed
run: |-
git config user.name "Automated"
Expand Down
23 changes: 16 additions & 7 deletions exoscale/api/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def _get_ref(api_spec, path):
}


def _translate_type(typ):
if isinstance(typ, list):
non_null = [t for t in typ if t != "null"]
if len(non_null) == 1:
return f"{_type_translations[non_null[0]]} | None"
return " | ".join(_type_translations[t] for t in non_null)
return _type_translations[typ]


def _status_code_docstring(api_spec, operation, status_code):
[ctype] = operation["responses"][status_code]["content"].keys()
return_schema = operation["responses"][status_code]["content"][ctype][
Expand All @@ -71,7 +80,7 @@ def _status_code_docstring(api_spec, operation, status_code):
item = _ref
else:
item = prop
typ = _type_translations[item["type"]]
typ = _translate_type(item["type"])
desc = prop.get("description")
if "enum" in item:
choices = "``, ``".join(map(repr, item["enum"]))
Expand All @@ -87,11 +96,11 @@ def _status_code_docstring(api_spec, operation, status_code):
+ "\n\n * ".join([""] + list(body.values()))
)
elif "description" in ref:
doc = f"{_type_translations[ref['type']]}: {ref['description']}."
doc = f"{_translate_type(ref['type'])}: {ref['description']}."
else:
doc = _type_translations[ref["type"]]
doc = _translate_type(ref["type"])
else:
doc = _type_translations[return_schema["type"]]
doc = _translate_type(return_schema["type"])
return doc


Expand Down Expand Up @@ -213,9 +222,9 @@ def _create_operation_call(
name = param["name"]
if "$ref" in param["schema"]:
ref = _get_ref(api_spec, param["schema"]["$ref"])
typ = _type_translations[ref["type"]]
typ = _translate_type(ref["type"])
else:
typ = _type_translations[param["schema"]["type"]]
typ = _translate_type(param["schema"]["type"])
normalized_name = name.replace("-", "_")
normalized_names[normalized_name] = name
parameters[normalized_name] = f"{normalized_name} ({typ})."
Expand All @@ -236,7 +245,7 @@ def _create_operation_call(
item = ref
else:
item = prop
typ = _type_translations[item["type"]]
typ = _translate_type(item["type"])
desc = prop.get("description") or item.get("description")
if "enum" in item:
choices = "``, ``".join(map(repr, item["enum"]))
Expand Down