Skip to content

Commit

Permalink
Add request() method
Browse files Browse the repository at this point in the history
  • Loading branch information
maxzheng committed May 11, 2024
1 parent 23ad520 commit 74b10ef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
32 changes: 18 additions & 14 deletions aiohttp_requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_aiohttp_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
Expand All @@ -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():
Expand Down

0 comments on commit 74b10ef

Please sign in to comment.