Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.7k
[Python] Support for per-operation servers#6557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
81e594d4985f271fd2e656caf66f5eec729df1c32439206457420edb5422cfd280837f294d164f38fc728ec2cb7655ecd06e973600a57b4c57880681d19097ff976a4032533d3706f4d3a1d0ca66df647File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| generatorName: python-experimental | ||
| outputDir: samples/openapi3/client/features/dynamic-servers/python-experimental/ | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_0/features/dynamic-servers.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/python | ||
| additionalProperties: | ||
| packageName: dynamic_servers |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -67,6 +67,15 @@ class Configuration(object): | ||
| :param signing_info: Configuration parameters for the HTTP signature security scheme. | ||
| Must be an instance of {{{packageName}}}.signing.HttpSigningConfiguration | ||
| {{/hasHttpSignatureMethods}} | ||
| :param server_index: Index to servers configuration. | ||
| :param server_variables: Mapping with string values to replace variables in | ||
| templated server configuration. The validation of enums is performed for | ||
| variables with defined enum values before. | ||
| :param server_operation_index: Mapping from operation ID to an index to server | ||
| configuration. | ||
| :param server_operation_variables: Mapping from operation ID to a mapping with | ||
| string values to replace variables in templated server configuration. | ||
| The validation of enums is performed for variables with defined enum values before. | ||
| {{#hasAuthMethods}} | ||
| :Example: | ||
| @@ -155,20 +164,30 @@ conf = {{{packageName}}}.Configuration( | ||
| _default = None | ||
| def __init__(self, host="{{{basePath}}}", | ||
| def __init__(self, host=None, | ||
| api_key=None, api_key_prefix=None, | ||
| username=None, password=None, | ||
| discard_unknown_keys=False, | ||
| disabled_client_side_validations="", | ||
| {{#hasHttpSignatureMethods}} | ||
| signing_info=None, | ||
| {{/hasHttpSignatureMethods}} | ||
| server_index=None, server_variables=None, | ||
| server_operation_index=None, server_operation_variables=None, | ||
| ): | ||
| """Constructor | ||
| """ | ||
| self.host = host | ||
| self._base_path = "{{{basePath}}}" if host is None else host | ||
spacether marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """Default Base url | ||
| """ | ||
| self.server_index = 0 if server_index is None and host is None else server_index | ||
| self.server_operation_index = server_operation_index or {} | ||
| """Default server index | ||
| """ | ||
| self.server_variables = server_variables or {} | ||
| self.server_operation_variables = server_operation_variables or {} | ||
| """Default server variables | ||
| """ | ||
| self.temp_folder_path = None | ||
| """Temp file folder for downloading files | ||
| """ | ||
| @@ -565,14 +584,18 @@ conf = {{{packageName}}}.Configuration( | ||
| {{/servers}} | ||
| ] | ||
| def get_host_from_settings(self, index, variables=None): | ||
| def get_host_from_settings(self, index, variables=None, servers=None): | ||
| """Gets host URL based on the index and variables | ||
| :param index: array index of the host settings | ||
| :param variables: hash of variable and the corresponding value | ||
This comment was marked as resolved.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. jirikuncar marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| :param servers: an array of host settings or None | ||
| :return: URL based on host settings | ||
| """ | ||
| if index is None: | ||
| return self._base_path | ||
| variables = {} if variables is None else variables | ||
| servers = self.get_host_settings() | ||
| servers = self.get_host_settings() if servers is None else servers | ||
| try: | ||
| server = servers[index] | ||
| @@ -584,7 +607,7 @@ conf = {{{packageName}}}.Configuration( | ||
| url = server['url'] | ||
| # go through variables and replace placeholders | ||
| for variable_name, variable in server['variables'].items(): | ||
| for variable_name, variable in server.get('variables', {}).items(): | ||
| used_value = variables.get( | ||
| variable_name, variable['default_value']) | ||
| @@ -599,3 +622,14 @@ conf = {{{packageName}}}.Configuration( | ||
| url = url.replace("{" + variable_name + "}", used_value) | ||
| return url | ||
| @property | ||
| def host(self): | ||
| """Return generated host.""" | ||
| return self.get_host_from_settings(self.server_index, variables=self.server_variables) | ||
| @host.setter | ||
| def host(self, value): | ||
| """Fix base path.""" | ||
| self._base_path = value | ||
| self.server_index = None | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -99,9 +99,9 @@ class {{classname}}(object): | ||
| _check_return_type (bool): specifies if type checking | ||
| should be done one the data received from the server. | ||
| Default is True. | ||
| _host_index (int): specifies the index of the server | ||
| _host_index (int/None): specifies the index of the server | ||
| that we want to use. | ||
| Default is 0. | ||
| Default is read from the configuration. | ||
spacether marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| async_req (bool): execute request asynchronously | ||
| Returns: | ||
| @@ -127,7 +127,7 @@ class {{classname}}(object): | ||
| kwargs['_check_return_type'] = kwargs.get( | ||
| '_check_return_type', True | ||
| ) | ||
| kwargs['_host_index'] = kwargs.get('_host_index', 0) | ||
| kwargs['_host_index'] = kwargs.get('_host_index') | ||
| {{#requiredParams}} | ||
| kwargs['{{paramName}}'] = \ | ||
| {{paramName}} | ||
| @@ -156,13 +156,37 @@ class {{classname}}(object): | ||
| {{#-first}} | ||
| 'servers': [ | ||
| {{/-first}} | ||
| '{{{url}}}'{{^-last}},{{/-last}} | ||
| { | ||
| 'url': "{{{url}}}", | ||
| 'description': "{{{description}}}{{^description}}No description provided{{/description}}", | ||
| {{#variables}} | ||
| {{#-first}} | ||
| 'variables': { | ||
| {{/-first}} | ||
| '{{{name}}}': { | ||
| 'description': "{{{description}}}{{^description}}No description provided{{/description}}", | ||
| 'default_value': "{{{defaultValue}}}", | ||
| {{#enumValues}} | ||
| {{#-first}} | ||
| 'enum_values': [ | ||
| {{/-first}} | ||
| "{{{.}}}"{{^-last}},{{/-last}} | ||
| {{#-last}} | ||
| ] | ||
| {{/-last}} | ||
| {{/enumValues}} | ||
| }{{^-last}},{{/-last}} | ||
| {{#-last}} | ||
| } | ||
| {{/-last}} | ||
| {{/variables}} | ||
| }, | ||
| {{#-last}} | ||
| ] | ||
| {{/-last}} | ||
| {{/servers}} | ||
| {{^servers}} | ||
| 'servers': [], | ||
| 'servers': None, | ||
| {{/servers}} | ||
| }, | ||
| params_map={ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| openapi: 3.0.0 | ||
| info: | ||
| description: This specification shows how to use dynamic servers. | ||
| ||
| version: 1.0.0 | ||
| title: OpenAPI Extension with dynamic servers | ||
| license: | ||
| name: Apache-2.0 | ||
| url: 'https://www.apache.org/licenses/LICENSE-2.0.html' | ||
| tags: | ||
| - name: usage | ||
| description: Show usage of dynamic servers | ||
| servers: | ||
| - url: 'http://{server}.swagger.io:{port}/v2' | ||
| description: petstore server | ||
| variables: | ||
| server: | ||
| enum: | ||
| - 'petstore' | ||
| - 'qa-petstore' | ||
| - 'dev-petstore' | ||
| default: 'petstore' | ||
| port: | ||
| enum: | ||
| - '80' | ||
| - '8080' | ||
| default: '80' | ||
| - url: https://localhost:8080/{version} | ||
| description: The local server | ||
| variables: | ||
| version: | ||
| enum: | ||
| - 'v1' | ||
| - 'v2' | ||
| - 'v3' | ||
| default: 'v1' | ||
| paths: | ||
| /default: | ||
| get: | ||
| tags: | ||
| - usage | ||
| summary: Use default server | ||
| description: Use default server | ||
| operationId: defaultServer | ||
| responses: | ||
| '200': | ||
| description: successful operation | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| /custom: | ||
| get: | ||
| tags: | ||
| - usage | ||
| servers: | ||
| - url: https://{server}.swagger.io:{port}/v2 | ||
| variables: | ||
| server: | ||
| enum: | ||
| - 'custom-petstore' | ||
| - 'custom-qa-petstore' | ||
| - 'custom-dev-petstore' | ||
| default: 'custom-petstore' | ||
| port: | ||
| enum: | ||
| - '80' | ||
| - '8080' | ||
| default: '8080' | ||
| - url: https://localhost:8081/{version} | ||
| description: The local custom server | ||
| variables: | ||
| version: | ||
| enum: | ||
| - 'v1' | ||
| - 'v2' | ||
| - 'v3' | ||
| default: 'v2' | ||
| - url: https://third.example.com/{prefix} | ||
| description: The local custom server | ||
| variables: | ||
| prefix: | ||
| default: 'custom' | ||
| summary: Use custom server | ||
| description: Use custom server | ||
| operationId: customServer | ||
| responses: | ||
| '200': | ||
| description: successful operation | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.