我想简单地将aiohttp异步HTTP请求的响应与一个标识符关联起来。我使用下面的代码来打API并提取contactproperty对象,该对象需要一个外部字段(contacid)才能调用其API。
def get_contact_properties(self, office_name, api_key, ids, chunk_size=100, **params):
properties_pages = []
batch = 0
while True:
chunk_ids = [ids[i] for i in range(batch * chunk_size + 1, chunk_size * (1 + batch) + 1)]
urls = ["{}/{}".format(self.__get_base_url(), "contacts/{}/properties?api_key={}".format(contactid, api_key))
for contactid in chunk_ids]
responses_raw = self.get_responses(urls, self.get_office_token(office_name), chunk_size)
try:
responses_json = [json.loads(response_raw) for response_raw in responses_raw]
except Exception as e:
print(e)
valid_responses = self.__get_valid_contact_properties_responses(responses_json)
properties_pages.append(valid_responses)
if len(valid_responses) < chunk_size: # this is how we know there are no more pages with data
break
else:
batch = batch + 1
ids是一个 list
的id。问题是我不知道哪个响应对应于哪个id,这样我就可以使用contacid将其链接到联系人实体。这是我的fetch()函数,所以我想知道如何编辑这个函数来返回contactid和输出。
async def __fetch(self, url, params, session):
async with session.get(url, params=params) as response:
output = await response.read()
return (output)
async def __bound_fetch(self, sem, url, params, session):
# Getter function with semaphore.
async with sem:
output = await self.__fetch(url, params, session)
return output
你可以返回 网址 (或任何标识您的请求的密钥),并附上 产出.
关于使用数据的问题,我认为你应该直接读取响应的JSON,尤其是aiohttp可以自动帮你做到这一点。
async def __fetch(self, url, params, session):
async with session.get(url, params=params) as response:
try:
data = await response.json()
except ValueError as exc:
print(exc)
return None
return data
async def __bound_fetch(self, sem, url, params, session):
# Getter function with semaphore.
async with sem:
output = await self.__fetch(url, params, session)
return {"url": url, "data": data}
你没有发布 get_responses
函数,但我猜想类似这样的东西应该可以用。
responses = self.get_responses(urls, self.get_office_token(office_name), chunk_size)
Responses将是一个列表 {"url": url, data: "data"}
(对于无效的响应,数据可以是None);但是,通过上面的代码,一个无效的请求不会影响其他请求。