Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add multiple server support to python client
  • Loading branch information
wing328 committed Jan 23, 2019
commit ea5397cac7301e4d5929a2a1332e0c0235d42f57
32 changes: 32 additions & 0 deletions bin/openapi3/python-petstore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/python -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g python -o samples/openapi3/client/petstore/python/ -DpackageName=petstore_api $@"

java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,68 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
"Version of the API: {{version}}\n"\
"SDK Package Version: {{packageVersion}}".\
format(env=sys.platform, pyversion=sys.version)

def get_host_settings(self):
"""Gets an array of host settings

:return: An array of host settings
"""
return [
{{#servers}}
{
'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}}
]

def get_host_from_settings(self, index, variables={}):
"""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
:return: URL based on host settings
"""

servers = get_host_settings

# check array index out of bound
if index < 0 or index > len(servers):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second expression should be >=

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Just pushed out a fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. I'm also reviewing other clients and filing a fix accordingly if the same issue is found.

raise ValueError("Invalid index %i when selecting the host settings. Must be less than %i" % index % len(servers)) # noqa: E501

server = servers[index]
url = server['url']

# go through variable and assign a value
for variable_name in server['variables']:
if name in variables[varible_name]:
if variables[variable_name] in variables[variable_name]['enum_values']:
url = url.replace("{" + variable_name + "}", variables[variable_name])
else:
raise ValueError("The variable `%s` in the host URL has invalid value %s. Must be %s." % variable_name % variables[variable_name] % variables[variable_name]['enum_values'])
else:
# use default value
url = url.replace("{" + variable_name + "}", variables[variable_name]['default_value'])
end

return url
3 changes: 1 addition & 2 deletions samples/client/petstore/python/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure HTTP basic authorization: http_basic_test
configuration = petstore_api.Configuration()
# Configure HTTP basic authorization: http_basic_test
configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure API key authorization: api_key_query
configuration = petstore_api.Configuration()
# Configure API key authorization: api_key_query
configuration.api_key['api_key_query'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key_query'] = 'Bearer'
Expand Down
27 changes: 9 additions & 18 deletions samples/client/petstore/python/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -80,9 +79,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -135,9 +133,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -189,9 +186,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -243,9 +239,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure API key authorization: api_key
configuration = petstore_api.Configuration()
# Configure API key authorization: api_key
configuration.api_key['api_key'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key'] = 'Bearer'
Expand Down Expand Up @@ -297,9 +292,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -348,9 +342,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -403,9 +396,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down Expand Up @@ -459,9 +451,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: petstore_auth
configuration = petstore_api.Configuration()
# Configure OAuth2 access token for authorization: petstore_auth
configuration.access_token = 'YOUR_ACCESS_TOKEN'

# create an instance of the API class
Expand Down
3 changes: 1 addition & 2 deletions samples/client/petstore/python/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ import time
import petstore_api
from petstore_api.rest import ApiException
from pprint import pprint

# Configure API key authorization: api_key
configuration = petstore_api.Configuration()
# Configure API key authorization: api_key
configuration.api_key['api_key'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['api_key'] = 'Bearer'
Expand Down
42 changes: 42 additions & 0 deletions samples/client/petstore/python/petstore_api/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,45 @@ def to_debug_report(self):
"Version of the API: 1.0.0\n"\
"SDK Package Version: 1.0.0".\
format(env=sys.platform, pyversion=sys.version)

def get_host_settings(self):
"""Gets an array of host settings

:return: An array of host settings
"""
return [
{
'url': "http://petstore.swagger.io:80/v2",
'description': "No description provided",
}
]

def get_host_from_settings(self, index, variables={}):
"""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
:return: URL based on host settings
"""

servers = get_host_settings

# check array index out of bound
if index < 0 or index > len(servers):
raise ValueError("Invalid index %i when selecting the host settings. Must be less than %i" % index % len(servers)) # noqa: E501

server = servers[index]
url = server['url']

# go through variable and assign a value
for variable_name in server['variables']:
if name in variables[varible_name]:
if variables[variable_name] in variables[variable_name]['enum_values']:
url = url.replace("{" + variable_name + "}", variables[variable_name])
else:
raise ValueError("The variable `%s` in the host URL has invalid value %s. Must be %s." % variable_name % variables[variable_name] % variables[variable_name]['enum_values'])
else:
# use default value
url = url.replace("{" + variable_name + "}", variables[variable_name]['default_value'])
end

return url
64 changes: 64 additions & 0 deletions samples/openapi3/client/petstore/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
venv/
.python-version

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

#Ipython Notebook
.ipynb_checkpoints
23 changes: 23 additions & 0 deletions samples/openapi3/client/petstore/python/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.0.0-SNAPSHOT
14 changes: 14 additions & 0 deletions samples/openapi3/client/petstore/python/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ref: https://docs.travis-ci.com/user/languages/python
language: python
python:
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "3.5"
#- "3.5-dev" # 3.5 development branch
#- "nightly" # points to the latest development branch e.g. 3.6-dev
# command to install dependencies
install: "pip install -r requirements.txt"
# command to run tests
script: nosetests
Loading