Skip to content

Commit

Permalink
[python] asyncio supports _preload_content; remove unsed imports (#107)
Browse files Browse the repository at this point in the history
* chore: update python samples

* feat: python/asyncio support for _preload_content

* feat: remove unused imports from python clients; fix discriminator
  • Loading branch information
tomplus authored and wing328 committed May 24, 2018
1 parent 76b7307 commit 7184f1e
Show file tree
Hide file tree
Showing 31 changed files with 44 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ssl

import aiohttp
import certifi
import asyncio
# python 2 and python 3 compatibility library
from six.moves.urllib.parse import urlencode

Expand Down Expand Up @@ -70,6 +71,9 @@ class RESTClientObject(object):
connector=connector
)

def __del__(self):
asyncio.ensure_future(self.pool_manager.close())

async def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True,
_request_timeout=None):
Expand Down Expand Up @@ -152,15 +156,17 @@ class RESTClientObject(object):
declared content type."""
raise ApiException(status=0, reason=msg)

async with self.pool_manager.request(**args) as r:
r = await self.pool_manager.request(**args)
if _preload_content:

data = await r.text()
r = RESTResponse(r, data)

# log response body
logger.debug("response body: %s", r.data)
# log response body
logger.debug("response body: %s", r.data)

if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)
if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)

return r

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import pprint
import re # noqa: F401

import six
{{#imports}}{{#-first}}
{{/-first}}
{{import}} # noqa: F401,E501
{{/imports}}


{{#models}}
Expand Down Expand Up @@ -170,7 +166,7 @@ class {{classname}}(object):
{{#discriminator}}
def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

{{/discriminator}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python-asyncio/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Name | Type | Description | Notes
**enum_query_string** | **str**| Query parameter enum test (string) | [optional] [default to &#39;-efg&#39;]
**enum_query_integer** | **int**| Query parameter enum test (double) | [optional]
**enum_query_double** | **float**| Query parameter enum test (double) | [optional]
**enum_form_string_array** | **list[str]**| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string_array** | [**list[str]**](str.md)| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string** | **str**| Form parameter enum test (string) | [optional] [default to &#39;-efg&#39;]

### Return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def color(self, color):

def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class AnimalFarm(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501


class ArrayTest(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Cat(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Dog(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501


class EnumTest(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class MixedPropertiesAndAdditionalPropertiesClass(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

import six

from petstore_api.models.category import Category # noqa: F401,E501
from petstore_api.models.tag import Tag # noqa: F401,E501


class Pet(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
16 changes: 11 additions & 5 deletions samples/client/petstore/python-asyncio/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import aiohttp
import certifi
import asyncio
# python 2 and python 3 compatibility library
from six.moves.urllib.parse import urlencode

Expand Down Expand Up @@ -78,6 +79,9 @@ def __init__(self, configuration, pools_size=4, maxsize=4):
connector=connector
)

def __del__(self):
asyncio.ensure_future(self.pool_manager.close())

async def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True,
_request_timeout=None):
Expand Down Expand Up @@ -160,15 +164,17 @@ async def request(self, method, url, query_params=None, headers=None,
declared content type."""
raise ApiException(status=0, reason=msg)

async with self.pool_manager.request(**args) as r:
r = await self.pool_manager.request(**args)
if _preload_content:

data = await r.text()
r = RESTResponse(r, data)

# log response body
logger.debug("response body: %s", r.data)
# log response body
logger.debug("response body: %s", r.data)

if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)
if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)

return r

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python-tornado/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Name | Type | Description | Notes
**enum_query_string** | **str**| Query parameter enum test (string) | [optional] [default to &#39;-efg&#39;]
**enum_query_integer** | **int**| Query parameter enum test (double) | [optional]
**enum_query_double** | **float**| Query parameter enum test (double) | [optional]
**enum_form_string_array** | **list[str]**| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string_array** | [**list[str]**](str.md)| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string** | **str**| Form parameter enum test (string) | [optional] [default to &#39;-efg&#39;]

### Return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def color(self, color):

def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class AnimalFarm(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501


class ArrayTest(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Cat(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Dog(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501


class EnumTest(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class MixedPropertiesAndAdditionalPropertiesClass(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

import six

from petstore_api.models.category import Category # noqa: F401,E501
from petstore_api.models.tag import Tag # noqa: F401,E501


class Pet(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Name | Type | Description | Notes
**enum_query_string** | **str**| Query parameter enum test (string) | [optional] [default to &#39;-efg&#39;]
**enum_query_integer** | **int**| Query parameter enum test (double) | [optional]
**enum_query_double** | **float**| Query parameter enum test (double) | [optional]
**enum_form_string_array** | **list[str]**| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string_array** | [**list[str]**](str.md)| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string** | **str**| Form parameter enum test (string) | [optional] [default to &#39;-efg&#39;]

### Return type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def color(self, color):

def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class AnimalFarm(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501


class ArrayTest(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
2 changes: 0 additions & 2 deletions samples/client/petstore/python/petstore_api/models/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Cat(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
2 changes: 0 additions & 2 deletions samples/client/petstore/python/petstore_api/models/dog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Dog(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501


class EnumTest(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class MixedPropertiesAndAdditionalPropertiesClass(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
3 changes: 0 additions & 3 deletions samples/client/petstore/python/petstore_api/models/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

import six

from petstore_api.models.category import Category # noqa: F401,E501
from petstore_api.models.tag import Tag # noqa: F401,E501


class Pet(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Expand Down
15 changes: 15 additions & 0 deletions samples/client/petstore/python/tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ def test_deserialize_dict_str_pet(self):
self.assertTrue(isinstance(deserialized, dict))
self.assertTrue(isinstance(deserialized['pet'], petstore_api.Pet))

def test_deserialize_dict_str_dog(self):
""" deserialize dict(str, Dog), use discriminator"""
data = {
'dog': {
"id": 0,
"className": "Dog",
"color": "white",
"bread": "Jack Russel Terrier"
}
}

deserialized = self.deserialize(data, 'dict(str, Animal)')
self.assertTrue(isinstance(deserialized, dict))
self.assertTrue(isinstance(deserialized['dog'], petstore_api.Dog))

def test_deserialize_dict_str_int(self):
""" deserialize dict(str, int) """
data = {
Expand Down

0 comments on commit 7184f1e

Please sign in to comment.