我有一個函式,它接受api_response和測驗以查看是否滿足條件if "meta" not in api_response:。如果滿足條件,我percent_complete從回應中提取密鑰/對值值并將其列印到控制臺。該值是一個百分比,在api_response.
我的問題是,當它列印到控制臺時,串列(應該包含 1x 值,例如,0.19)正在列印該值兩次。
例如,如果percent_complete== 0.19,則控制臺將列印Your data requested, associated with ID: 2219040 is (0.19, 0.19) complete!.
我的代碼有什么問題嗎,這可能是導致這種情況的原因嗎?
功能-
def api_call():
# Calling function that returns API authentication details for use in endpoint_initializer()
key, secret, url = ini_reader()
# Calling function that makes initial API POST call and returns endpoint_url to call, until data is returned.
endpoint_url = endpoint_initializer()
# saving current date in a variable, for use when printing user message
date = dt.datetime.today().strftime("%Y-%m-%d")
# Printing endpoint_url and current date.
print("-------------------------------------\n","API URL constructed for:", date, "\n-------------------------------------")
print("-------------------------------------------------------------\n","Endpoint:", endpoint_url, "\n-------------------------------------------------------------")
# Loop will continously call the end_point URL until data is returned. When data is not returned the `percent_complete' key value is extracted from api response.
# this will inform user of status of data aggregation.
while True:
response = requests.get(url = endpoint_url, auth = HTTPBasicAuth(key, secret), headers = {"Vendor-firm": "343"})
api_response = json.loads(response.text)
# Test condition to see if "meta" is in api_response. Meta only in response, when data is ready.
if "meta" not in api_response:
id_value = "id"
res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
id_value = "".join(res1)
percent_value = "percent_complete"
res2 = (tuple(api_response["data"]["attributes"].get("percent_complete", '') for key, val in api_response.items()))
print(f' Your data requested, associated with ID: {id_value} is {res2} complete!')
time.sleep(5)
# Condition to allow API response to be returned, if condition is not met.
elif "meta" in api_response:
return api_response
示例 API 回應-
{
"data": {
"id": "2219040",
"type": "jobs",
"attributes": {
"job_type": "PORTFOLIO_VIEW_RESULTS",
"started_at": "2021-12-18T17:40:17Z",
"parameters": {
"end_date": "2021-12-14",
"output_type": "json",
"view_id": 304078,
"portfolio_id": 1,
"portfolio_type": "firm",
"start_date": "2021-12-14"
},
"percent_complete": 0.19,
"status": "In Progress"
},
"relationships": {
"creator": {
"links": {
"self": "/v1/jobs/2219040/relationships/creator",
"related": "/v1/jobs/2219040/creator"
},
"data": {
"type": "users",
"id": "731221"
}
}
},
"links": {
"self": "/v1/jobs/2219040"
}
},
"included": []
}
uj5u.com熱心網友回復:
您回復中的字典包含兩個專案(“資料”和“包含”)。您創建 res2 的代碼迭代所有專案:
res2 = (tuple(api_response["data"]["attributes"].get("percent_complete", '') for key, val in api_response.items()))
所以你會得到兩次資訊。由于您只是從“資料”鍵中提取資料,因此迭代這些專案是愚蠢的。對?做就是了:
res2 = api_response["data"]["attributes"].get("percent_complete", '')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/388315.html
