Skip to content

Commit ddfcc95

Browse files
authored
fix: use httpx in generated configuration.py (#22418)
* fix: use httpx in generated configuration.py * fix: add enum_values for httpx * chore: update petstore samples
1 parent c5e0d08 commit ddfcc95

File tree

7 files changed

+48
-17
lines changed

7 files changed

+48
-17
lines changed

modules/openapi-generator/src/main/resources/python/configuration.mustache

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{{>partial_header}}
44

55

6+
{{#async}}
7+
import base64
8+
{{/async}}
69
import copy
710
import http.client as httplib
811
import logging
@@ -14,7 +17,9 @@ import sys
1417
from typing import Any, ClassVar, Dict, List, Literal, Optional, TypedDict, Union
1518
from typing_extensions import NotRequired, Self
1619

20+
{{^async}}
1721
import urllib3
22+
{{/async}}
1823

1924
{{#hasHttpSignatureMethods}}
2025
from {{packageName}}.signing import HttpSigningConfiguration
@@ -187,7 +192,7 @@ class Configuration:
187192
:param ca_cert_data: verify the peer using concatenated CA certificate data
188193
in PEM (str) or DER (bytes) format.
189194
:param cert_file: the path to a client certificate file, for mTLS.
190-
:param key_file: the path to a client key file, for mTLS.
195+
:param key_file: the path to a client key file, for mTLS.
191196

192197
{{#hasAuthMethods}}
193198
:Example:
@@ -353,7 +358,9 @@ conf = {{{packageName}}}.Configuration(
353358
"""Logging Settings
354359
"""
355360
self.logger["package_logger"] = logging.getLogger("{{packageName}}")
361+
{{^async}}
356362
self.logger["urllib3_logger"] = logging.getLogger("urllib3")
363+
{{/async}}
357364
self.logger_format = '%(asctime)s %(levelname)s %(message)s'
358365
"""Log format
359366
"""
@@ -615,9 +622,17 @@ conf = {{{packageName}}}.Configuration(
615622
password = ""
616623
if self.password is not None:
617624
password = self.password
625+
626+
{{#async}}
627+
return "Basic " + base64.b64encode(
628+
(username + ":" + password).encode('utf-8')
629+
).decode('utf-8')
630+
{{/async}}
631+
{{^async}}
618632
return urllib3.util.make_headers(
619633
basic_auth=username + ':' + password
620634
).get('authorization')
635+
{{/async}}
621636

622637
def auth_settings(self)-> AuthSettings:
623638
"""Gets Auth Settings dict for api client.
@@ -720,6 +735,11 @@ conf = {{{packageName}}}.Configuration(
720735
]
721736
{{/-last}}
722737
{{/enumValues}}
738+
{{^enumValues}}
739+
{{#async}}
740+
'enum_values': []
741+
{{/async}}
742+
{{/enumValues}}
723743
}{{^-last}},{{/-last}}
724744
{{#-last}}
725745
}
@@ -762,6 +782,7 @@ conf = {{{packageName}}}.Configuration(
762782
variable_name, variable['default_value'])
763783

764784
if 'enum_values' in variable \
785+
and variable['enum_values'] \
765786
and used_value not in variable['enum_values']:
766787
raise ValueError(
767788
"The variable `{0}` in the host URL has invalid value "

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/configuration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Configuration:
166166
:param ca_cert_data: verify the peer using concatenated CA certificate data
167167
in PEM (str) or DER (bytes) format.
168168
:param cert_file: the path to a client certificate file, for mTLS.
169-
:param key_file: the path to a client key file, for mTLS.
169+
:param key_file: the path to a client key file, for mTLS.
170170
171171
:Example:
172172
@@ -504,6 +504,7 @@ def get_basic_auth_token(self) -> Optional[str]:
504504
password = ""
505505
if self.password is not None:
506506
password = self.password
507+
507508
return urllib3.util.make_headers(
508509
basic_auth=username + ':' + password
509510
).get('authorization')
@@ -587,6 +588,7 @@ def get_host_from_settings(
587588
variable_name, variable['default_value'])
588589

589590
if 'enum_values' in variable \
591+
and variable['enum_values'] \
590592
and used_value not in variable['enum_values']:
591593
raise ValueError(
592594
"The variable `{0}` in the host URL has invalid value "

samples/client/echo_api/python/openapi_client/configuration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Configuration:
166166
:param ca_cert_data: verify the peer using concatenated CA certificate data
167167
in PEM (str) or DER (bytes) format.
168168
:param cert_file: the path to a client certificate file, for mTLS.
169-
:param key_file: the path to a client key file, for mTLS.
169+
:param key_file: the path to a client key file, for mTLS.
170170
171171
:Example:
172172
@@ -504,6 +504,7 @@ def get_basic_auth_token(self) -> Optional[str]:
504504
password = ""
505505
if self.password is not None:
506506
password = self.password
507+
507508
return urllib3.util.make_headers(
508509
basic_auth=username + ':' + password
509510
).get('authorization')
@@ -587,6 +588,7 @@ def get_host_from_settings(
587588
variable_name, variable['default_value'])
588589

589590
if 'enum_values' in variable \
591+
and variable['enum_values'] \
590592
and used_value not in variable['enum_values']:
591593
raise ValueError(
592594
"The variable `{0}` in the host URL has invalid value "

samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
""" # noqa: E501
1313

1414

15+
import base64
1516
import copy
1617
import http.client as httplib
1718
import logging
@@ -20,7 +21,6 @@
2021
from typing import Any, ClassVar, Dict, List, Literal, Optional, TypedDict, Union
2122
from typing_extensions import NotRequired, Self
2223

23-
import urllib3
2424

2525
from petstore_api.signing import HttpSigningConfiguration
2626

@@ -171,7 +171,7 @@ class Configuration:
171171
:param ca_cert_data: verify the peer using concatenated CA certificate data
172172
in PEM (str) or DER (bytes) format.
173173
:param cert_file: the path to a client certificate file, for mTLS.
174-
:param key_file: the path to a client key file, for mTLS.
174+
:param key_file: the path to a client key file, for mTLS.
175175
176176
:Example:
177177
@@ -325,7 +325,6 @@ def __init__(
325325
"""Logging Settings
326326
"""
327327
self.logger["package_logger"] = logging.getLogger("petstore_api")
328-
self.logger["urllib3_logger"] = logging.getLogger("urllib3")
329328
self.logger_format = '%(asctime)s %(levelname)s %(message)s'
330329
"""Log format
331330
"""
@@ -574,9 +573,10 @@ def get_basic_auth_token(self) -> Optional[str]:
574573
password = ""
575574
if self.password is not None:
576575
password = self.password
577-
return urllib3.util.make_headers(
578-
basic_auth=username + ':' + password
579-
).get('authorization')
576+
577+
return "Basic " + base64.b64encode(
578+
(username + ":" + password).encode('utf-8')
579+
).decode('utf-8')
580580

581581
def auth_settings(self)-> AuthSettings:
582582
"""Gets Auth Settings dict for api client.
@@ -727,6 +727,7 @@ def get_host_from_settings(
727727
variable_name, variable['default_value'])
728728

729729
if 'enum_values' in variable \
730+
and variable['enum_values'] \
730731
and used_value not in variable['enum_values']:
731732
raise ValueError(
732733
"The variable `{0}` in the host URL has invalid value "

samples/openapi3/client/petstore/python-httpx/petstore_api/configuration.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
""" # noqa: E501
1313

1414

15+
import base64
1516
import copy
1617
import http.client as httplib
1718
import logging
@@ -20,7 +21,6 @@
2021
from typing import Any, ClassVar, Dict, List, Literal, Optional, TypedDict, Union
2122
from typing_extensions import NotRequired, Self
2223

23-
import urllib3
2424

2525
from petstore_api.signing import HttpSigningConfiguration
2626

@@ -171,7 +171,7 @@ class Configuration:
171171
:param ca_cert_data: verify the peer using concatenated CA certificate data
172172
in PEM (str) or DER (bytes) format.
173173
:param cert_file: the path to a client certificate file, for mTLS.
174-
:param key_file: the path to a client key file, for mTLS.
174+
:param key_file: the path to a client key file, for mTLS.
175175
176176
:Example:
177177
@@ -325,7 +325,6 @@ def __init__(
325325
"""Logging Settings
326326
"""
327327
self.logger["package_logger"] = logging.getLogger("petstore_api")
328-
self.logger["urllib3_logger"] = logging.getLogger("urllib3")
329328
self.logger_format = '%(asctime)s %(levelname)s %(message)s'
330329
"""Log format
331330
"""
@@ -574,9 +573,10 @@ def get_basic_auth_token(self) -> Optional[str]:
574573
password = ""
575574
if self.password is not None:
576575
password = self.password
577-
return urllib3.util.make_headers(
578-
basic_auth=username + ':' + password
579-
).get('authorization')
576+
577+
return "Basic " + base64.b64encode(
578+
(username + ":" + password).encode('utf-8')
579+
).decode('utf-8')
580580

581581
def auth_settings(self)-> AuthSettings:
582582
"""Gets Auth Settings dict for api client.
@@ -727,6 +727,7 @@ def get_host_from_settings(
727727
variable_name, variable['default_value'])
728728

729729
if 'enum_values' in variable \
730+
and variable['enum_values'] \
730731
and used_value not in variable['enum_values']:
731732
raise ValueError(
732733
"The variable `{0}` in the host URL has invalid value "

samples/openapi3/client/petstore/python-lazyImports/petstore_api/configuration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class Configuration:
172172
:param ca_cert_data: verify the peer using concatenated CA certificate data
173173
in PEM (str) or DER (bytes) format.
174174
:param cert_file: the path to a client certificate file, for mTLS.
175-
:param key_file: the path to a client key file, for mTLS.
175+
:param key_file: the path to a client key file, for mTLS.
176176
177177
:Example:
178178
@@ -578,6 +578,7 @@ def get_basic_auth_token(self) -> Optional[str]:
578578
password = ""
579579
if self.password is not None:
580580
password = self.password
581+
581582
return urllib3.util.make_headers(
582583
basic_auth=username + ':' + password
583584
).get('authorization')
@@ -731,6 +732,7 @@ def get_host_from_settings(
731732
variable_name, variable['default_value'])
732733

733734
if 'enum_values' in variable \
735+
and variable['enum_values'] \
734736
and used_value not in variable['enum_values']:
735737
raise ValueError(
736738
"The variable `{0}` in the host URL has invalid value "

samples/openapi3/client/petstore/python/petstore_api/configuration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class Configuration:
172172
:param ca_cert_data: verify the peer using concatenated CA certificate data
173173
in PEM (str) or DER (bytes) format.
174174
:param cert_file: the path to a client certificate file, for mTLS.
175-
:param key_file: the path to a client key file, for mTLS.
175+
:param key_file: the path to a client key file, for mTLS.
176176
177177
:Example:
178178
@@ -578,6 +578,7 @@ def get_basic_auth_token(self) -> Optional[str]:
578578
password = ""
579579
if self.password is not None:
580580
password = self.password
581+
581582
return urllib3.util.make_headers(
582583
basic_auth=username + ':' + password
583584
).get('authorization')
@@ -731,6 +732,7 @@ def get_host_from_settings(
731732
variable_name, variable['default_value'])
732733

733734
if 'enum_values' in variable \
735+
and variable['enum_values'] \
734736
and used_value not in variable['enum_values']:
735737
raise ValueError(
736738
"The variable `{0}` in the host URL has invalid value "

0 commit comments

Comments
 (0)