Skip to content

Commit

Permalink
feat: Adde GHA IC
Browse files Browse the repository at this point in the history
  • Loading branch information
micmurawski committed Jul 14, 2024
1 parent 5672864 commit 67ce71d
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 119 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/main.yml

This file was deleted.

46 changes: 46 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Publish Python Package

on:
release:
types: [published]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.2
poetry config virtualenvs.create false
poetry install
- name: Install
run: poetry install --with test
- name: Run Pytest
run: poetry run pytest
deploy:
runs-on: ubuntu-latest
needs: [ tests ]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.2
poetry config virtualenvs.create false
poetry install
- name: Build package
run: poetry build
- name: Publish package
run: poetry publish
env:
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.POETRY_PYPI_TOKEN_PYPI }}
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on: [pull_request, workflow_dispatch]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.2
poetry config virtualenvs.create false
poetry install
- name: Install
run: poetry install --with test
- name: Run Pytest
run: poetry run pytest
68 changes: 58 additions & 10 deletions gen/client_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,39 @@ def get_type_for_param(param: dict, swagger: dict | None = None) -> str:

param_type = param.get("type")
schema_type = get_path(param, "schema.type", None)
ref_path = get_path(param, "$ref", None)
ref_path = get_path(param, "$ref", get_path(param, "schema.$ref", None))
all_of = get_path(param, "allOf.0.$ref", None)
any_of = get_path(param, "anyOf", None)


if any_of:
raise Exception(param)
types = []
_list = any_of

for i in _list:
obj = get_path(
swagger,
i["$ref"][2:],
None,
"/"
)
_type = obj["type"]
items_type = get_path(obj, "items.type", "object")

types.append(
COMPLEX_TYPES_MAP[(_type, items_type)]
)
return "Union[%s]" % ", ".join(types)

while ref_path or all_of:
ref_path = ref_path or all_of
ref = get_path(swagger, ref_path[2:], separator="/", default=None)

ref_path = get_path(ref, "$ref", None)
all_of = get_path(ref, "allOf.0.$ref", None)
if ref_path.endswith(".json"):
return dict.__name__

ref_path = get_path(ref, "$ref", default=None)
all_of = get_path(ref, "allOf.0.$ref", default=None)
if not ref_path and not all_of:
return SIMPLE_TYPES_MAP[ref["type"]]

Expand All @@ -86,8 +106,36 @@ def get_type_for_param(param: dict, swagger: dict | None = None) -> str:

if schema_type in SIMPLE_TYPES_MAP:
return SIMPLE_TYPES_MAP[schema_type]
elif schema_type in COMPLEX_TYPES:
items_type_path = get_path(
param,
"schema.items.type",
get_path(
param,
"schema.items.$ref",
None
)
)
while True:
ref = get_path(swagger, items_type_path[2:], separator="/", default={})

if isinstance(ref, dict) and "$ref" in ref:
items_type_path = ref["$ref"]
continue
else:
_type = ref.get("type", items_type_path)
return COMPLEX_TYPES_MAP[(schema_type, _type)]

elif param_type in COMPLEX_TYPES:
items_type = get_path(param, "schema.items.type", default="object")
items_type = get_path(
param,
"schema.items.type",
default=get_path(
param,
"items.type",
default="object"
)
)
return COMPLEX_TYPES_MAP[(param_type, items_type)]
elif param_type in SIMPLE_TYPES_MAP:
return SIMPLE_TYPES_MAP[param_type]
Expand Down Expand Up @@ -164,7 +212,7 @@ def create_request_block(
requests_lines[1] = requests_lines[1][:-1] + ", params=params)"

if has_body:
requests_lines[1] = requests_lines[1][:-1] + ", json=data)"
requests_lines[1] = requests_lines[1][:-1] + ", json=request_data)"

return "\n".join(requests_lines)

Expand Down Expand Up @@ -398,7 +446,7 @@ def create_validation_block(path: str, method: str, swagger: dict, name: str) ->
model_class = schema_path.rsplit("/", 1)[-1]
lines = [
"if self.validation:",
"%svalidate_data(data, %s, %sAPIError)" % (INDENT, model_class, name),
"%svalidate_data(request_data, %s, %sAPIError)" % (INDENT, model_class, name),
]
return "\n".join(lines)
return ""
Expand All @@ -411,10 +459,10 @@ def create_body_block(required: list[dict], not_required: list[dict]) -> str:
return ""

if not required:
lines.append("data = {}")
lines.append("request_data = {}")
else:
lines.append(
"data = {"
"request_data = {"
)
for p in required:
converted_name = convert_to_snake_case(p['name'])
Expand All @@ -432,7 +480,7 @@ def create_body_block(required: list[dict], not_required: list[dict]) -> str:
)

lines.append(
"%sdata['%s'] = %s" % (INDENT, p['name'], converted_name)
"%srequest_data['%s'] = %s" % (INDENT, p['name'], converted_name)
)

return "\n".join(lines)
Expand Down
4 changes: 3 additions & 1 deletion gen/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import sys
from subprocess import PIPE, Popen
from typing import Any
from .swagger import SwaggerDoc

import yaml

from .swagger import SwaggerDoc

NOT_SET = object()


Expand Down
3 changes: 1 addition & 2 deletions gen/run_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import requests

from gen.client_generator import generate_clients
from gen.helpers import generate_models, put_inits, ruff_format
from gen.helpers import generate_models, ruff_format

WORKDIR = os.getcwd()

Expand Down Expand Up @@ -48,7 +48,6 @@ def remove_path_if_empty(path: str):
models_path = os.path.join(module_path, "models")
rel_module_path = module_path.replace(WORKDIR, ".")
os.makedirs(rel_module_path, exist_ok=True)
put_inits(rel_module_path)

swagger_path = os.path.join(module_path, "swagger.yaml")

Expand Down
Loading

0 comments on commit 67ce71d

Please sign in to comment.