diff --git a/aiohttp_requests/__init__.py b/aiohttp_requests/__init__.py index 16b0917..df1806d 100644 --- a/aiohttp_requests/__init__.py +++ b/aiohttp_requests/__init__.py @@ -29,20 +29,7 @@ def session(self): def __getattr__(self, attr): if attr.upper() in aiohttp.hdrs.METH_ALL: - @functools.wraps(self.session._request) - def session_request(path, *args, **kwargs): - """ - This ensures `self.session` is always called where it can check the session/loop state so can't use - functools.partials as monkeypatch seems to do something weird where __getattr__ is only called once - for each attribute after patch is undone - """ - if isinstance(path, (list, tuple)): - return self._concurrent_request(self.session._request, attr.upper(), path, args, kwargs, - as_iterator=kwargs.pop('as_iterator', False)) - else: - return self.session._request(attr.upper(), path, *args, **kwargs) - - return session_request + return functools.partial(self.request, attr.upper()) else: return super().__getattribute__(attr) @@ -55,6 +42,23 @@ def __setattr__(self, attr, value): def _concurrent_request(self, request, verb, paths, args, kwargs, as_iterator=False): return self._worker.do([(request, verb, path, args, kwargs) for path in paths], as_iterator=as_iterator) + def request(self, verb, path, *args, **kwargs): + """ + This ensures `self.session` is always called where it can check the session/loop state so can't use + functools.partials as monkeypatch seems to do something weird where __getattr__ is only called once + for each attribute after patch is undone + + :param verb: HTTP verb + :param path: URL path + :param args: Additional arguments for aiohttp.ClientSession.request + :param kwargs: Additional keyword arguments for aiohttp.ClientSession.request + """ + if isinstance(path, (list, tuple)): + return self._concurrent_request(self.session._request, verb.upper(), path, args, kwargs, + as_iterator=kwargs.pop('as_iterator', False)) + else: + return self.session._request(verb.upper(), path, *args, **kwargs) + def close(self): """ Close aiohttp.ClientSession. diff --git a/tests/test_aiohttp_requests.py b/tests/test_aiohttp_requests.py index ac7a312..faefd09 100644 --- a/tests/test_aiohttp_requests.py +++ b/tests/test_aiohttp_requests.py @@ -24,6 +24,7 @@ async def test_aiohttp_requests_integration(): assert response.status == 200 assert len(content) > 10000 + assert 'Search the world' in content async def test_aiohttp_requests_integration_multiple(): @@ -35,6 +36,7 @@ async def test_aiohttp_requests_integration_multiple(): assert response.status == 200 assert len(content) > 10000 + assert 'Search the world' in content # Multiple requests as iterator responses = requests.get(['https://www.google.com'] * 2, as_iterator=True) @@ -44,6 +46,7 @@ async def test_aiohttp_requests_integration_multiple(): assert response.status == 200 assert len(content) > 10000 + assert 'Search the world' in content async def test_aiohttp_requests_after_close():