处理不同的API响应dtype
给定一个基本的API调用 -
response = requests.post(url, auth=HTTPBasicAuth(key, secret), headers=headers, data=d)
return(response.json())
如果DTYPE变化,您将如何处理响应
?例如,有时json
,有时.xlsx
[二进制]?
上下文:我想创建一个测试3X标准的函数:
- 响应是包含
persect> persion_complete
- 值用于添加到进度栏中。此apir_response
告诉我所请求的数据尚未准备就绪,并采用percen_complete
值以更新进度栏。 - 响应是包含
meta
的JSON对象 - 如果为true,则请求的数据已作为JSON对象返回,api_response
应返回,准备就绪用于另一个功能。 - 响应是
.xlsx
file(binary ??) - 如果为true,则请求的数据已在.xlsx
格式中返回,并且<代码>应返回api_response ,准备在另一个功能中使用。
欢迎任何建议吗?
Given a basic API call -
response = requests.post(url, auth=HTTPBasicAuth(key, secret), headers=headers, data=d)
return(response.json())
How would you handle the response
if the Dtype varied? E.g., sometimes json
, sometimes .xlsx
[binary]?
Context: I want to create a function that tests 3x criteria:
- Response is JSON object containing
percent_complete
- if TRUE, then thepercent_complete
value is used to add to a progress bar. Thisapir_response
tells me the requested data isn't ready yet and takespercent_complete
value to update a progress bar. - Response is JSON object containing
meta
- if TRUE, then the requested data has been returned as a JSON object, andapi_response
should be returned, ready to be used in another function. - Response is an
.xlsx
file (binary??) - if TRUE, then the requested data has been returned in.xlsx
format, andapi_response
should be returned, ready for use in another function.
Any suggestions would be welcome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
尝试
和除外
:You could use
try
andexcept
for that: