From 56b68862f94aede140b76df00c187fed243ad463 Mon Sep 17 00:00:00 2001 From: Gabriel Couture Date: Wed, 29 May 2024 14:42:42 -0400 Subject: [PATCH] Improve docstring layout --- pyproject.toml | 7 +- scripts/make_orthanc_client.py | 4 +- .../templates/async_method.jinja | 73 ++++++++++--------- simple_openapi_client/templates/method.jinja | 73 ++++++++++--------- tests/test_client.py | 13 ++-- 5 files changed, 86 insertions(+), 84 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6443cb6..9bb3016 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "simple-openapi-client" -version = "0.5.3" +version = "0.5.4" description = "OpenAPI Python client generator that follows the KISS principle." authors = ["Gabriel Couture "] license = "BSD-3-Clause" @@ -11,10 +11,7 @@ homepage = 'https://github.com/gacou54/openapi-client' python = "^3.8" httpx = "^0" Jinja2 = "^3.1.2" -black = "^23.7.0" - -[tool.poetry.dev-dependencies] -pytest = "^7.1.2" +black = "^24.4.2" [tool.poetry.group.dev.dependencies] pytest = "^7.4.0" diff --git a/scripts/make_orthanc_client.py b/scripts/make_orthanc_client.py index 8a6b218..627bbef 100644 --- a/scripts/make_orthanc_client.py +++ b/scripts/make_orthanc_client.py @@ -1,6 +1,6 @@ import simple_openapi_client -ORTHANC_API_URL = 'https://api.orthanc-server.com/orthanc-openapi.json' +ORTHANC_API_URL = 'https://orthanc.uclouvain.be/api/orthanc-openapi.json' def generate_client(path: str, async_mode: bool = False): @@ -8,7 +8,7 @@ def generate_client(path: str, async_mode: bool = False): document = simple_openapi_client.parse_openapi(ORTHANC_API_URL) document = _apply_corrections_to_documents(document) - client_str = simple_openapi_client.make_client(document, config, async_mode=async_mode) + client_str = simple_openapi_client.make_client(document, config, async_mode=async_mode, use_black=True) with open(path, 'w') as file: file.write(client_str) diff --git a/simple_openapi_client/templates/async_method.jinja b/simple_openapi_client/templates/async_method.jinja index 1c65661..1714881 100644 --- a/simple_openapi_client/templates/async_method.jinja +++ b/simple_openapi_client/templates/async_method.jinja @@ -22,64 +22,65 @@ Tags: {{ ','.join(OPERATION.tags) }} {%- if OPERATION.externalDocs %}ExternaDocs: {{ OPERATION.externalDocs }}{% endif %} + {%- if PATH_PARAMETERS or REQUEST_BODY.json or REQUEST_BODY.files or REQUEST_BODY.data or QUERY_PARAMETERS or HEADERS %} + Parameters ---------- - {% if PATH_PARAMETERS -%} - {% for name, description in PATH_PARAMETERS.items() -%} + {%- if PATH_PARAMETERS %} + {%- for name, description in PATH_PARAMETERS.items() %} {{ name }} {{ description }} - {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.json %}json + {%- endfor %} + {%- endif %} + {%- if REQUEST_BODY.json %} + json {%- for request_data in REQUEST_BODY.json %} - {% if request_data -%} + {%- if request_data %} Dictionary with the following keys: {%- for property_name, property in request_data.properties.items() %} - "{{ property_name }}": {{ property['description'] }} + "{{ property_name }}": {{ property['description'] }} {%- endfor %} - {%- endif -%} + {%- endif %} {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.files %}files + {%- endif %} + {%- if REQUEST_BODY.files %} + files {%- for request_data in REQUEST_BODY.files %} - {% if request_data -%}Files: + {%- if request_data %} + Files: {%- for property_name, property in request_data.properties.items() %} "{{ property_name }}": {{ property['description'] }} {%- endfor %} - {%- endif -%} + {%- endif %} {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.data %} + {%- endif %} + {%- if REQUEST_BODY.data %} data {%- for request_data in REQUEST_BODY.data %} {{ request_data.description }} - {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.content %}content - {%- for request_data in REQUEST_BODY.content %} - - (Content-Type: "{{ request_data.content_type }}") {{ request_data.description }} - {% endfor -%} - {% endif -%} - - {% if QUERY_PARAMETERS -%} + {%- endfor -%} + {%- endif %} + {%- if REQUEST_BODY.content %} + content + {%- for request_data in REQUEST_BODY.content %} + - (Content-Type: "{{ request_data.content_type }}") {{ request_data.description }} + {%- endfor %} + {%- endif %} + {%- if QUERY_PARAMETERS %} params Dictionary of optional parameters: - {%- for name, query in QUERY_PARAMETERS.items() %} - "{{ name }}" ({{ query.type }}): {{ query.description }} - {%- endfor -%} - {% endif -%} - - {% if HEADERS -%} + {%- for name, query in QUERY_PARAMETERS.items() %} + "{{ name }}" ({{ query.type }}): {{ query.description }} + {%- endfor %} + {%- endif %} + {%- if HEADERS %} headers Dictionary of optional headers: - {% for name, header in HEADERS.items() -%} + {%- for name, header in HEADERS.items() %} "{{ name }}" ({{ header.type }}): {{ header.description }} - {% endfor -%} - {% endif %} + {%- endfor %} + {%- endif %} + {%- endif %} Returns ------- diff --git a/simple_openapi_client/templates/method.jinja b/simple_openapi_client/templates/method.jinja index e2370be..dac4daf 100644 --- a/simple_openapi_client/templates/method.jinja +++ b/simple_openapi_client/templates/method.jinja @@ -22,64 +22,65 @@ Tags: {{ ','.join(OPERATION.tags) }} {%- if OPERATION.externalDocs %}ExternaDocs: {{ OPERATION.externalDocs }}{% endif %} + {%- if PATH_PARAMETERS or REQUEST_BODY.json or REQUEST_BODY.files or REQUEST_BODY.data or QUERY_PARAMETERS or HEADERS %} + Parameters ---------- - {% if PATH_PARAMETERS -%} - {% for name, description in PATH_PARAMETERS.items() -%} + {%- if PATH_PARAMETERS %} + {%- for name, description in PATH_PARAMETERS.items() %} {{ name }} {{ description }} - {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.json %}json + {%- endfor %} + {%- endif %} + {%- if REQUEST_BODY.json %} + json {%- for request_data in REQUEST_BODY.json %} - {% if request_data -%} + {%- if request_data %} Dictionary with the following keys: {%- for property_name, property in request_data.properties.items() %} - "{{ property_name }}": {{ property['description'] }} + "{{ property_name }}": {{ property['description'] }} {%- endfor %} - {%- endif -%} + {%- endif %} {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.files %}files + {%- endif %} + {%- if REQUEST_BODY.files %} + files {%- for request_data in REQUEST_BODY.files %} - {% if request_data -%}Files: + {%- if request_data %} + Files: {%- for property_name, property in request_data.properties.items() %} "{{ property_name }}": {{ property['description'] }} {%- endfor %} - {%- endif -%} + {%- endif %} {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.data %} + {%- endif %} + {%- if REQUEST_BODY.data %} data {%- for request_data in REQUEST_BODY.data %} {{ request_data.description }} - {% endfor -%} - {% endif -%} - - {% if REQUEST_BODY.content %}content - {%- for request_data in REQUEST_BODY.content %} - - (Content-Type: "{{ request_data.content_type }}") {{ request_data.description }} - {% endfor -%} - {% endif -%} - - {% if QUERY_PARAMETERS -%} + {%- endfor -%} + {%- endif %} + {%- if REQUEST_BODY.content %} + content + {%- for request_data in REQUEST_BODY.content %} + - (Content-Type: "{{ request_data.content_type }}") {{ request_data.description }} + {%- endfor %} + {%- endif %} + {%- if QUERY_PARAMETERS %} params Dictionary of optional parameters: - {%- for name, query in QUERY_PARAMETERS.items() %} - "{{ name }}" ({{ query.type }}): {{ query.description }} - {%- endfor -%} - {% endif -%} - - {% if HEADERS -%} + {%- for name, query in QUERY_PARAMETERS.items() %} + "{{ name }}" ({{ query.type }}): {{ query.description }} + {%- endfor %} + {%- endif %} + {%- if HEADERS %} headers Dictionary of optional headers: - {% for name, header in HEADERS.items() -%} + {%- for name, header in HEADERS.items() %} "{{ name }}" ({{ header.type }}): {{ header.description }} - {% endfor -%} - {% endif %} + {%- endfor %} + {%- endif %} + {%- endif %} Returns ------- diff --git a/tests/test_client.py b/tests/test_client.py index 25a66a6..92ff3f0 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -4,7 +4,10 @@ import simple_openapi_client -OPENAPI_URL = 'https://api.orthanc-server.com/orthanc-openapi.json' + +ORTHANC_DEMO_URL = 'https://orthanc.uclouvain.be/demo' + +OPENAPI_URL = 'https://orthanc.uclouvain.be/api/orthanc-openapi.json' CLIENT_PATH = 'generated_client.py' CLIENT_MODULE = 'generated_client' @@ -66,7 +69,7 @@ def _apply_corrections_to_documents(document): def test_client(generate_client): client_class = getattr(__import__(CLIENT_MODULE), CLIENT_NAME) - client = client_class(url='https://demo.orthanc-server.com') + client = client_class(url=ORTHANC_DEMO_URL) result = client.get_system() @@ -75,7 +78,7 @@ def test_client(generate_client): assert 'DicomAet' in result assert 'DicomPort' in result - with client_class(url='https://demo.orthanc-server.com') as client: + with client_class(url=ORTHANC_DEMO_URL) as client: result = client.get_system() assert isinstance(result, dict) @@ -87,7 +90,7 @@ def test_client(generate_client): @pytest.mark.asyncio async def test_async_client(generate_client): async_client_class = getattr(__import__(ASYNC_CLIENT_MODULE), CLIENT_NAME) - async_client = async_client_class(url='https://demo.orthanc-server.com') + async_client = async_client_class(url=ORTHANC_DEMO_URL) result = await async_client.get_system() @@ -96,7 +99,7 @@ async def test_async_client(generate_client): assert 'DicomAet' in result assert 'DicomPort' in result - async with async_client_class(url='https://demo.orthanc-server.com') as async_client: + async with async_client_class(url=ORTHANC_DEMO_URL) as async_client: result = await async_client.get_system() assert isinstance(result, dict)