Skip to content

Commit

Permalink
[Python] Add Flag to Allow Ignoring Operation Servers (#18981)
Browse files Browse the repository at this point in the history
* [Python] Add Flag to Allow Ignoring Operation Servers

* generate samples

* add tests
  • Loading branch information
ckoegel committed Jun 26, 2024
1 parent 38d189b commit 0d05ee3
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
*,
Expand All @@ -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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def param_serialize(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -84,6 +87,7 @@ def __init__(self, host=None,
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,
*,
Expand All @@ -102,6 +106,9 @@ def __init__(self, host=None,
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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def param_serialize(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -84,6 +87,7 @@ def __init__(self, host=None,
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,
*,
Expand All @@ -102,6 +106,9 @@ def __init__(self, host=None,
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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def param_serialize(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -143,6 +146,7 @@ def __init__(self, host=None,
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,
*,
Expand All @@ -161,6 +165,9 @@ def __init__(self, host=None,
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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tests.util import async_test
import petstore_api

HOST = 'http://localhost/v2'

class TestApiClient(unittest.TestCase):

Expand All @@ -20,3 +21,31 @@ async def test_context_manager_closes_client(self):
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')
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def param_serialize(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -144,6 +147,7 @@ def __init__(self, host=None,
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,
*,
Expand All @@ -162,6 +166,9 @@ def __init__(self, host=None,
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
"""
Expand Down
27 changes: 27 additions & 0 deletions samples/openapi3/client/petstore/python/tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ def test_configuration(self):
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ def testAccessTokenWhenConstructingConfiguration(self):
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()

Expand Down

0 comments on commit 0d05ee3

Please sign in to comment.