如何将不同查询参数的列表分别传递给请求体?



我有一个查询参数列表,我想传递给一个函数作为参数。这个函数调用一个api,所以参数列表中的每一项在每次调用时都被分别传递,并获取与该参数相关的结果,直到它到达该列表的末尾。

下面是参数列表(列表中的每一项都有不同的日期范围,有不同的结果集):

REQUEST_PARAMS = [
{
"period": "range",
"date": "2020-10-08,2020-10-31",
"format": "JSON",
"module": "API",
"method": "**",
"flat": "1",
"filter_limit": "-1",
},
{
"period": "range",
"date": "2020-11-01,2020-11-30",
"format": "JSON",
"module": "API",
"method": "**",
"flat": "1",
"filter_limit": "-1",
},
{
"period": "range",
"date": "2020-12-01,2020-12-31",
"format": "JSON",
"module": "API",
"method": "**",
"flat": "1",
"filter_limit": "-1",
}
]

下面是调用目标api的函数:

def get_the_results(max_retries=4):
"""
Fetch the response object from the api
:param max_retries: The max number of retries in case of error.
:return: list of dictionaries for the results
"""
return fetch_and_validate_response(URL, REQUEST_PARAMS, max_retries)

def fetch_and_validate_response(url, params, max_retries=0):
"""
Fetch a response object from provided URL.
:param url: The url for the request
:param params: The parameters for the request
:param max_retries: The max number of retries in case of error.
:return: json object if successful, None if retries due to network error continues to fail
"""
try:
response = get(url=url, params=params, verify=False)
response.raise_for_status()
response_decoded = response.json()
# Raise a RequestException if the API returns a result with an error.
try:
if response_decoded.get('result') == 'error':
log.info(f"Error in response: {response_decoded.get('result')} - {response_decoded.get('message')}")
raise RequestException
except AttributeError:
log.info("No error detected in response object.")
return response_decoded
except HTTPError as e:
log.exception(f'Network Error: HTTP error {response.status_code} occurred while getting query: {url}: {e}')
except ConnectionError as e:
log.exception(f'Network Error: A connection error occurred while getting query: {url} with {params}: {e}')
except Timeout as e:
log.exception(f'Network Error: The request timed out for query: {url} with {params}: {e}')
except RequestException as e:
log.exception(f'Network Error: An error occurred while getting {url}. Please double-check the url: {e}')
if max_retries <= 0:
return None
time.sleep(5)
return fetch_and_validate_response(url, params, max_retries - 1)

我想在这个函数中打印从这个调用中获取的结果:

def construct_results_dict():
data = get_the_results()
for item in data:
print(item)

非常感谢你的帮助-谢谢:)

你可以这样做。

遍历REQUEST_PARAMS列表并使用每个参数调用fetch_and_validate_response(),然后将fetch_and_validate_response()的返回值存储在列表中并返回该列表。

def get_the_results(max_retries=4):
"""
Fetch the response object from the api
:param max_retries: The max number of retries in case of error.
:return: list of dictionaries for the results
"""
# results list to store the results returned by the below function
results = []
for param in REQUEST_PARAMS:
results.append(fetch_and_validate_response(URL, param, max_retries)) 

# Finally return the results list
return results
现在,results列表将在下面的函数中返回到data
def construct_results_dict():
data = get_the_results()
for item in data:
print(item)

最新更新