[Python] Add Flag to Allow Ignoring Operation Servers (#18981)

* [Python] Add Flag to Allow Ignoring Operation Servers

* generate samples

* add tests
This commit is contained in:
Cameron Koegel 2024-06-26 03:08:13 -04:00 committed by GitHub
parent 38d189b159
commit 0d05ee35f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 108 additions and 5 deletions

View File

@ -232,7 +232,7 @@ class ApiClient:
body = self.sanitize_for_serialization(body)
# request url
if _host is None:
if _host is None or self.configuration.ignore_operation_servers:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead

View File

@ -24,6 +24,9 @@ class Configuration:
"""This class contains various settings of the API client.
:param host: Base url.
:param ignore_operation_servers
Boolean to ignore operation servers for the API client.
Config will use `host` as the base url regardless of the operation servers.
:param api_key: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
@ -148,6 +151,7 @@ conf = {{{packageName}}}.Configuration(
{{/hasHttpSignatureMethods}}
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
ignore_operation_servers=False,
ssl_ca_cert=None,
retries=None,
*,
@ -166,6 +170,9 @@ conf = {{{packageName}}}.Configuration(
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.ignore_operation_servers = ignore_operation_servers
"""Ignore operation servers
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""

View File

@ -228,7 +228,7 @@ class ApiClient:
body = self.sanitize_for_serialization(body)
# request url
if _host is None:
if _host is None or self.configuration.ignore_operation_servers:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead

View File

@ -33,6 +33,9 @@ class Configuration:
"""This class contains various settings of the API client.
:param host: Base url.
:param ignore_operation_servers
Boolean to ignore operation servers for the API client.
Config will use `host` as the base url regardless of the operation servers.
:param api_key: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
@ -84,6 +87,7 @@ conf = openapi_client.Configuration(
access_token=None,
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
ignore_operation_servers=False,
ssl_ca_cert=None,
retries=None,
*,
@ -102,6 +106,9 @@ conf = openapi_client.Configuration(
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.ignore_operation_servers = ignore_operation_servers
"""Ignore operation servers
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""

View File

@ -228,7 +228,7 @@ class ApiClient:
body = self.sanitize_for_serialization(body)
# request url
if _host is None:
if _host is None or self.configuration.ignore_operation_servers:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead

View File

@ -33,6 +33,9 @@ class Configuration:
"""This class contains various settings of the API client.
:param host: Base url.
:param ignore_operation_servers
Boolean to ignore operation servers for the API client.
Config will use `host` as the base url regardless of the operation servers.
:param api_key: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
@ -84,6 +87,7 @@ conf = openapi_client.Configuration(
access_token=None,
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
ignore_operation_servers=False,
ssl_ca_cert=None,
retries=None,
*,
@ -102,6 +106,9 @@ conf = openapi_client.Configuration(
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.ignore_operation_servers = ignore_operation_servers
"""Ignore operation servers
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""

View File

@ -230,7 +230,7 @@ class ApiClient:
body = self.sanitize_for_serialization(body)
# request url
if _host is None:
if _host is None or self.configuration.ignore_operation_servers:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead

View File

@ -31,6 +31,9 @@ class Configuration:
"""This class contains various settings of the API client.
:param host: Base url.
:param ignore_operation_servers
Boolean to ignore operation servers for the API client.
Config will use `host` as the base url regardless of the operation servers.
:param api_key: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
@ -143,6 +146,7 @@ conf = petstore_api.Configuration(
signing_info=None,
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
ignore_operation_servers=False,
ssl_ca_cert=None,
retries=None,
*,
@ -161,6 +165,9 @@ conf = petstore_api.Configuration(
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.ignore_operation_servers = ignore_operation_servers
"""Ignore operation servers
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""

View File

@ -8,6 +8,7 @@ import weakref
from tests.util import async_test
import petstore_api
HOST = 'http://localhost/v2'
class TestApiClient(unittest.TestCase):
@ -20,3 +21,31 @@ class TestApiClient(unittest.TestCase):
rest_pool_ref = client.rest_client.pool_manager
self.assertTrue(rest_pool_ref.closed)
@async_test
async def test_ignore_operation_servers(self):
config = petstore_api.Configuration(host=HOST)
client = petstore_api.ApiClient(config)
user_api_instance = petstore_api.api.user_api.UserApi(client)
config_ignore = petstore_api.Configuration(host=HOST, ignore_operation_servers=True)
client_ignore = petstore_api.ApiClient(config_ignore)
user_api_instance_ignore = petstore_api.api.user_api.UserApi(client_ignore)
params_to_serialize = {
'user': petstore_api.User(id=1, username='test'),
'_request_auth': None,
'_content_type': 'application/json',
'_headers': None,
'_host_index': 0
}
# operation servers should be used
_, url, *_ = user_api_instance._create_user_serialize(**params_to_serialize)
self.assertEqual(client.configuration.host, HOST)
self.assertEqual(url, 'http://petstore.swagger.io/v2/user')
# operation servers should be ignored
_, url_ignore, *_ = user_api_instance_ignore._create_user_serialize(**params_to_serialize)
self.assertEqual(client.configuration.host, HOST)
self.assertEqual(url_ignore, HOST + '/user')

View File

@ -227,7 +227,7 @@ class ApiClient:
body = self.sanitize_for_serialization(body)
# request url
if _host is None:
if _host is None or self.configuration.ignore_operation_servers:
url = self.configuration.host + resource_path
else:
# use server/host defined in path or operation instead

View File

@ -32,6 +32,9 @@ class Configuration:
"""This class contains various settings of the API client.
:param host: Base url.
:param ignore_operation_servers
Boolean to ignore operation servers for the API client.
Config will use `host` as the base url regardless of the operation servers.
:param api_key: Dict to store API key(s).
Each entry in the dict specifies an API key.
The dict key is the name of the security scheme in the OAS specification.
@ -144,6 +147,7 @@ conf = petstore_api.Configuration(
signing_info=None,
server_index=None, server_variables=None,
server_operation_index=None, server_operation_variables=None,
ignore_operation_servers=False,
ssl_ca_cert=None,
retries=None,
*,
@ -162,6 +166,9 @@ conf = petstore_api.Configuration(
self.server_operation_variables = server_operation_variables or {}
"""Default server variables
"""
self.ignore_operation_servers = ignore_operation_servers
"""Ignore operation servers
"""
self.temp_folder_path = None
"""Temp file folder for downloading files
"""

View File

@ -56,6 +56,33 @@ class ApiClientTests(unittest.TestCase):
self.assertEqual('test_username', client.configuration.username)
self.assertEqual('test_password', client.configuration.password)
def test_ignore_operation_servers(self):
config = petstore_api.Configuration(host=HOST)
client = petstore_api.ApiClient(config)
user_api_instance = petstore_api.api.user_api.UserApi(client)
config_ignore = petstore_api.Configuration(host=HOST, ignore_operation_servers=True)
client_ignore = petstore_api.ApiClient(config_ignore)
user_api_instance_ignore = petstore_api.api.user_api.UserApi(client_ignore)
params_to_serialize = {
'user': petstore_api.User(id=1, username='test'),
'_request_auth': None,
'_content_type': 'application/json',
'_headers': None,
'_host_index': 0
}
# operation servers should be used
_, url, *_ = user_api_instance._create_user_serialize(**params_to_serialize)
self.assertEqual(client.configuration.host, HOST)
self.assertEqual(url, 'http://petstore.swagger.io/v2/user')
# operation servers should be ignored
_, url_ignore, *_ = user_api_instance_ignore._create_user_serialize(**params_to_serialize)
self.assertEqual(client.configuration.host, HOST)
self.assertEqual(url_ignore, HOST + '/user')
def test_select_header_accept(self):
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
accept = self.api_client.select_header_accept(accepts)

View File

@ -58,6 +58,18 @@ class TestConfiguration(unittest.TestCase):
c1 = petstore_api.Configuration(access_token="12345")
self.assertEqual(c1.access_token, "12345")
def test_ignore_operation_servers(self):
self.config.ignore_operation_servers = True
self.assertTrue(self.config.ignore_operation_servers)
self.config.ignore_operation_servers = False
self.assertFalse(self.config.ignore_operation_servers)
c1 = petstore_api.Configuration(ignore_operation_servers=True)
self.assertTrue(c1.ignore_operation_servers)
c2 = petstore_api.Configuration()
self.assertFalse(c2.ignore_operation_servers)
def test_get_host_settings(self):
host_settings = self.config.get_host_settings()