Skip to content

Commit 0a75c08

Browse files
chore(python) update generator to 7.20 (#252)
* chore(python) update generator to 7.20 - migrate templates - write customization readme - bump generator version * fix(py) adjust template comments
1 parent c7276e7 commit 0a75c08

9 files changed

Lines changed: 224 additions & 44 deletions

File tree

languages/python/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# STACKIT Python SDK Generator
2+
3+
## Quick Update Guide
4+
5+
| File | Action |
6+
|------------------------------------|-----------------------------------------|
7+
| `configuration.mustache` | do nothing |
8+
| `readme.mustache` | do nothing |
9+
| `pyproject.mustache` | check dependencies, adjust if necessary |
10+
| `rest.mustache` | port changes |
11+
| `api.mustache` | port changes |
12+
| `api_client.mustache` | port changes |
13+
| `model_generic.mustache` | port changes |
14+
| `model_oneof.mustache` | port changes |
15+
| `exceptions.mustache` | port changes |
16+
| `__init__package.mustache` | port changes |
17+
| `README_onlypackage.mustache` | do nothing, unchanged |
18+
| `api_doc.mustache` | do nothing, unchanged |
19+
| `api_doc_example.mustache` | do nothing, unchanged |
20+
| `api_response.mustache` | do nothing, unchanged |
21+
| `api_test.mustache` | do nothing, unchanged |
22+
| `asyncio` | do nothing, unchanged |
23+
| `common_README.mustache` | do nothing, unchanged |
24+
| `__init__.mustache` | do nothing, unchanged |
25+
| `__init__api.mustache` | do nothing, unchanged |
26+
| `__init__model.mustache` | do nothing, unchanged |
27+
| `git_push.sh.mustache` | do nothing, unchanged |
28+
| `github-workflow.mustache` | do nothing, unchanged |
29+
| `gitignore.mustache` | do nothing, unchanged |
30+
| `gitlab-ci.mustache` | do nothing, unchanged |
31+
| `model.mustache` | do nothing, unchanged |
32+
| `model_anyof.mustache` | do nothing, unchanged |
33+
| `model_doc.mustache` | do nothing, unchanged |
34+
| `model_enum.mustache` | do nothing, unchanged |
35+
| `model_test.mustache` | do nothing, unchanged |
36+
| `partial_api.mustache` | do nothing, unchanged |
37+
| `partial_api_args.mustache` | do nothing, unchanged |
38+
| `partial_header.mustache` | do nothing, unchanged |
39+
| `py.typed.mustache` | do nothing, unchanged |
40+
| `python_doc_auth_partial.mustache` | do nothing, unchanged |
41+
| `requirements.mustache` | do nothing, unchanged |
42+
| `rest.mustache` | do nothing, unchanged |
43+
| `setup.mustache` | do nothing, unchanged |
44+
| `setup_cfg.mustache` | do nothing, unchanged |
45+
| `signing.mustache` | do nothing, unchanged |
46+
| `test-requirements.mustache` | do nothing, unchanged |
47+
| `tornado` | do nothing, unchanged |
48+
| `tox.mustache` | do nothing, unchanged |
49+
| `travis.mustache` | do nothing, unchanged |
50+
| `exports_api.mustache` | do nothing, unchanged |
51+
| `exports_model.mustache` | do nothing, unchanged |
52+
| `exports_package.mustache` | do nothing, unchanged |
53+
54+
55+
If upstream contains a template, not listed here, adjust the table accordingly.
56+
57+
## Template adjustments
58+
59+
The following templates were customized but don't need to be adjusted when updating the Python SDK generator to a newer
60+
upstream version:
61+
62+
- `configuration.mustache`: This template was entirely overwritten to provide a custom `HostConfiguration`. The
63+
`Configuration` from `core` is used instead of generating a new one for each service.
64+
- `readme.mustache`: it's the readme
65+
- `pyproject.mustache`: heavily customized to use `uv`, just check the dependencies
66+
67+
The following templates were customized and need to be checked for adjustments when updating the Python SDK generator to
68+
a newer upstream version:
69+
70+
- `rest.mustache`:
71+
- uses `requests` instead of `urllib3` for making HTTP requests. This is done to use the same library as `core` does.
72+
`core` uses `requests` because of an ADR: "HTTP Client for Python Core Implementation" (internal link).
73+
- upstream also configures a `urllib3.PoolManager` for each API client. In `requests` this is done by using
74+
`requests.Session()` for doing requests. A global pool can be used by setting `custom_http_session` on `Config`.
75+
- `api.mustache`:
76+
- customized to use `core`'s `Configuration`
77+
- `api_client.mustache`:
78+
- customized to use `core`'s `Configuration`
79+
- `update_params_for_auth` and `_apply_auth_params` removed, authentication is done in `core`
80+
- `model_generic.mustache`:
81+
- contains a temporary workaround for the year 0 issue
82+
- `model_oneof.mustache`:
83+
- workaround for pattern containing leading and trailing `/`
84+
- removal of `ValueError` if multiple matches are found
85+
- `exceptions.mustache`:
86+
- use `requests` instead of `urllib3`
87+
- `__init__package.mustache`:
88+
- customized imports

languages/python/templates/__init__package.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/__init__package.mustache for the original template }}
12
# coding: utf-8
23

34
# flake8: noqa
45

56
{{>partial_header}}
67

8+
79
__version__ = "{{packageVersion}}"
810

911
# Define package exports
1012
__all__ = [
1113
{{#apiInfo}}{{#apis}}"{{classname}}",
1214
{{/apis}}{{/apiInfo}}"ApiResponse",
1315
"ApiClient",
16+
{{! TEMPLATE CUSTOMIZATION - BEGIN - custom config }}
1417
"HostConfiguration",
18+
{{! TEMPLATE CUSTOMIZATION - END - custom config }}
1519
"OpenApiException",
1620
"ApiTypeError",
1721
"ApiValueError",
@@ -23,6 +27,7 @@ __all__ = [
2327
{{/-last}}{{#-last}},{{/-last}}{{/model}}{{/models}}
2428
]
2529

30+
{{! TEMPLATE CUSTOMIZATION - BEGIN - custom imports }}
2631
# import apis into sdk package
2732
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}} as {{classname}}
2833
{{/apis}}{{/apiInfo}}
@@ -46,6 +51,7 @@ from {{packageName}}.signing import HttpSigningConfiguration as HttpSigningConfi
4651
from {{modelPackage}}.{{classFilename}} import {{classname}} as {{classname}}
4752
{{/model}}
4853
{{/models}}
54+
{{! TEMPLATE CUSTOMIZATION - END - custom imports }}
4955
{{#recursionLimit}}
5056

5157
__import__('sys').setrecursionlimit({{{.}}})

languages/python/templates/api.mustache

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/api.mustache for the original template }}
12
# coding: utf-8
23

34
{{>partial_header}}
@@ -10,7 +11,9 @@ from typing_extensions import Annotated
1011
{{import}}
1112
{{/imports}}
1213

14+
{{! TEMPLATE CUSTOMIZATION - BEGIN - custom config }}
1315
from stackit.core.configuration import Configuration
16+
{{! TEMPLATE CUSTOMIZATION - END - custom config }}
1417
from {{packageName}}.api_client import ApiClient, RequestSerialized
1518
from {{packageName}}.api_response import ApiResponse
1619
from {{packageName}}.rest import RESTResponseType
@@ -24,46 +27,51 @@ class {{classname}}:
2427
Do not edit the class manually.
2528
"""
2629

30+
{{! TEMPLATE CUSTOMIZATION - BEGIN - custom config }}
2731
def __init__(self, configuration: Configuration = None) -> None:
2832
if configuration is None:
2933
configuration = Configuration()
3034
self.configuration = configuration
3135
self.api_client = ApiClient(self.configuration)
36+
{{! TEMPLATE CUSTOMIZATION - END - custom config }}
3237
{{#operation}}
3338

3439

3540
@validate_call
36-
{{#asyncio}}async {{/asyncio}}def {{operationId}}{{>partial_api_args}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
41+
{{#async}}async {{/async}}def {{operationId}}{{>partial_api_args}} -> {{{returnType}}}{{^returnType}}None{{/returnType}}:
3742
{{>partial_api}}
38-
response_data = {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
43+
44+
response_data = {{#async}}await {{/async}}self.api_client.call_api(
3945
*_param,
4046
_request_timeout=_request_timeout
4147
)
42-
{{#asyncio}}await {{/asyncio}}response_data.read()
48+
{{#async}}await {{/async}}response_data.read()
4349
return self.api_client.response_deserialize(
4450
response_data=response_data,
4551
response_types_map=_response_types_map,
4652
).data
4753

4854

4955
@validate_call
50-
{{#asyncio}}async {{/asyncio}}def {{operationId}}_with_http_info{{>partial_api_args}} -> ApiResponse[{{{returnType}}}{{^returnType}}None{{/returnType}}]:
56+
{{#async}}async {{/async}}def {{operationId}}_with_http_info{{>partial_api_args}} -> ApiResponse[{{{returnType}}}{{^returnType}}None{{/returnType}}]:
5157
{{>partial_api}}
52-
response_data = {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
58+
59+
response_data = {{#async}}await {{/async}}self.api_client.call_api(
5360
*_param,
5461
_request_timeout=_request_timeout
5562
)
56-
{{#asyncio}}await {{/asyncio}}response_data.read()
63+
{{#async}}await {{/async}}response_data.read()
5764
return self.api_client.response_deserialize(
5865
response_data=response_data,
5966
response_types_map=_response_types_map,
6067
)
6168

6269

6370
@validate_call
64-
{{#asyncio}}async {{/asyncio}}def {{operationId}}_without_preload_content{{>partial_api_args}} -> RESTResponseType:
71+
{{#async}}async {{/async}}def {{operationId}}_without_preload_content{{>partial_api_args}} -> RESTResponseType:
6572
{{>partial_api}}
66-
response_data = {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
73+
74+
response_data = {{#async}}await {{/async}}self.api_client.call_api(
6775
*_param,
6876
_request_timeout=_request_timeout
6977
)
@@ -183,7 +191,7 @@ class {{classname}}:
183191
{{#constantParams}}
184192
{{#isQueryParam}}
185193
# Set client side default value of Query Param "{{baseName}}".
186-
_query_params['{{baseName}}'] = {{#_enum}}'{{{.}}}'{{/_enum}}
194+
_query_params.append(('{{baseName}}', {{#_enum}}'{{{.}}}'{{/_enum}}))
187195
{{/isQueryParam}}
188196
{{#isHeaderParam}}
189197
# Set client side default value of Header Param "{{baseName}}".

0 commit comments

Comments
 (0)