将 aiohttp 请求与响应相关联
Associating aiohttp requests with the responses
我只想将来自 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,以便稍后我可以 link 它使用 contacid 联系实体。这是我的 fetch() 函数,所以我想知道如何将此函数编辑为 return 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
您可以 return url(或任何标识您的请求的键)连同 输出 .
关于使用数据,我认为你应该直接阅读响应 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}
您没有 post get_responses
函数,但我猜这样的东西应该可以工作:
responses = self.get_responses(urls, self.get_office_token(office_name), chunk_size)
响应将是 {"url": url, data: "data"}
的列表(对于无效响应,数据可以是 None);但是,使用上面的代码,一个无效请求不会影响其他请求。
我只想将来自 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,以便稍后我可以 link 它使用 contacid 联系实体。这是我的 fetch() 函数,所以我想知道如何将此函数编辑为 return 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
您可以 return url(或任何标识您的请求的键)连同 输出 .
关于使用数据,我认为你应该直接阅读响应 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}
您没有 post get_responses
函数,但我猜这样的东西应该可以工作:
responses = self.get_responses(urls, self.get_office_token(office_name), chunk_size)
响应将是 {"url": url, data: "data"}
的列表(对于无效响应,数据可以是 None);但是,使用上面的代码,一个无效请求不会影响其他请求。