Skip to content

Commit

Permalink
Improve docstring layout
Browse files Browse the repository at this point in the history
  • Loading branch information
gacou54 committed May 30, 2024
1 parent 3d9faf6 commit 56b6886
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 84 deletions.
7 changes: 2 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "BSD-3-Clause"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions scripts/make_orthanc_client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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):
config = simple_openapi_client.Config(client_name='AsyncOrthanc' if async_mode else 'Orthanc')

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)
Expand Down
73 changes: 37 additions & 36 deletions simple_openapi_client/templates/async_method.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
73 changes: 37 additions & 36 deletions simple_openapi_client/templates/method.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
13 changes: 8 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -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()

Expand All @@ -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)
Expand Down

0 comments on commit 56b6886

Please sign in to comment.