I have the following code that includes a simple class
AioRequest. I would like to be able to call an instance of this class directly with a single url or a list of urls and return the results.
request = AioRequest() single_response = request('get', 'http://httpbin.org/get')
url_list = ['http://httpbin.org/get', 'http://google/some_json'] list_of_responses = request('get', url_list)
I’m wondering what the best practice is for two strategies I am using;
Determining if the argument is a list of urls or a string with a single url. Shoud it be two separate arguments like
__call__or should I type check like
I would like to use the
__call__method as the main api for the
AioRequestinstance, but is returning two different types based on the arguments given a good idea? Is is safe to rely on the user to determine the return type ex. give a single string get a single response, give a list of strings, get a list of responses?
import asyncio from aiohttp import ClientSession from collections import namedtuple Response = namedtuple( "Response", ["url", "status_code", "json"] ) class AioRequest: def __init__(self, session=None): self._loop = asyncio.get_event_loop() self._session = session or ClientSession(loop=self._loop) def __call__(self, method, url=None, url_list=None, **kwargs): urls = url_list or  urls.append(url) requests = [self._request(method, url, **kwargs) for url in urls if url is not None] return self._run(*requests) def __call__2(self, method, url_or_list_of, **kwargs): if type(url_or_list_of) is str: return self._run(method, url_or_list_of, **kwargs) else: requests = [self._request(method, url, **kwargs) for url in url_or_list_of] return self._run(*requests) async def _request(self, method, url, **kwargs): response = await self._session.request(method, url, **kwargs) return Response(url, response.status, await response.json()) def _run(self, *tasks): task_group = asyncio.gather(*tasks) results = self._loop.run_until_complete(task_group) # results = list of responses if len(results) == 1: return results return results def __del__(self): self._session.close()
Thank you in advance for all your help.
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!