Skip to content

Commit

Permalink
fix getattr (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
thehesiod authored Jun 4, 2020
1 parent c5bf6be commit 02ab083
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
47 changes: 42 additions & 5 deletions aiobotocore/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from botocore.awsrequest import prepare_request_dict
from botocore.client import logger, PaginatorDocstring, ClientCreator, \
BaseClient, ClientEndpointBridge
Expand Down Expand Up @@ -69,13 +71,48 @@ def _get_client_args(self, service_model, region_name, is_secure,
verify, credentials, scoped_config, client_config, endpoint_bridge)


class _AsyncAttrAdapter:
def __init__(self, coro):
self._coro = coro
self._args = None
self._kwargs = None

def __call__(self, *args, **kwargs):
# bind args
self._args = args
self._kwargs = kwargs
return self

async def _resolve(self):
event_response = await self._coro
if asyncio.iscoroutinefunction(event_response):
result = await event_response(*self._args, **self._kwargs)
return result

if callable(event_response):
result = event_response(*self._args, **self._kwargs)
if asyncio.iscoroutine(result):
result = await result
return result

return event_response

def __await__(self):
return self._resolve().__await__()


class AioBaseClient(BaseClient):
def __getattr__(self, item):
# TODO: this is not supported yet as emit_until_response is an async function
raise AttributeError(
"'%s' object has no attribute '%s'" % (
self.__class__.__name__, item)
async def _async_getattr(self, item):
event_name = 'getattr.%s.%s' % (
self._service_model.service_id.hyphenize(), item
)
handler, event_response = await self.meta.events.emit_until_response(
event_name, client=self)

return event_response

def __getattr__(self, item):
return _AsyncAttrAdapter(self._async_getattr(item))

async def _make_api_call(self, operation_name, api_params):
operation_model = self._service_model.operation_model(operation_name)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_mturk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from botocore.stub import Stubber, ANY
import pytest


_mturk_list_hits_response = {
'NumResults': 0,
'HITs': [],
'ResponseMetadata': {
'RequestId': '00000000-4989-4ffc-85cd-aaaaaaaaaaaa',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': '00000000-4989-4ffc-85cd-aaaaaaaaaaaa',
'content-type': 'application/x-amz-json-1.1',
'content-length': '26',
'date': 'Thu, 04 Jun 2020 00:48:16 GMT'
},
'RetryAttempts': 0
}
}


# Unfortunately moto does not support mturk yet
@pytest.mark.moto
@pytest.mark.asyncio
async def test_mturk_stubber(session):
async with session.create_client('mturk', region_name='us-east-1') as client:
with Stubber(client) as stubber:
stubber.add_response('list_hits_for_qualification_type',
_mturk_list_hits_response,
{'QualificationTypeId': ANY})

response = await client.list_hi_ts_for_qualification_type(
QualificationTypeId='string')
assert response == _mturk_list_hits_response

0 comments on commit 02ab083

Please sign in to comment.