如何测试二进制的 API 响应

How to test an API response for binary

问题:
测试二进制响应的 API 响应的最简单方法是什么?

上下文:
我有一个函数可以 API 调用一些数据。 API 调用的响应 (api_response) 将是 JSON 或二进制。如果 JSON,并且它包含 percent_complete,则数据尚未准备好,我的函数使用 percent_complete key:pair 值来为用户更新进度条。

如果响应为 JSON 并包含 meta,那么我的数据已准备就绪并已作为 JSON 对象返回。

如果响应是二进制的,那么我的数据也已准备就绪,但已返回为 .xlsx [二进制]。这是数据未准备好时的响应,您会看到 percent_complete 用于进度条 -

{
    "data": {
        "id": "2768510",
        "type": "jobs",
        "attributes": {
            "job_type": "PORTFOLIO_VIEW_RESULTS",
            "started_at": "2022-04-14T16:19:21Z",
            "parameters": {
                "end_date": "2022-04-14",
                "output_type": "json",
                "view_id": 304078,
                "portfolio_id": 1,
                "portfolio_type": "firm",
                "start_date": "2022-04-14"
            },
            "percent_complete": 0.0,
            "status": "In Progress"
        },
        "relationships": {
            "creator": {
                "links": {
                    "self": "/v1/jobs/2768510/relationships/creator",
                    "related": "/v1/jobs/2768510/creator"
                },
                "data": {
                    "type": "users",
                    "id": "731221"
                }
            }
        },
        "links": {
            "self": "/v1/jobs/2768510"
        }
    },
    "included": []

当前函数:
以下函数继续每 5 秒调用一次 API(使用 [=19 中的 7 位代码=],根据上述 API 对象)直到 meta 在 JSON 对象中返回(因此我的数据作为 JSON 对象返回)和 returns JSON 对象作为 api_response.

否则,API 调用每 5 秒继续一次,仅使用 percent_complete 更新状态栏(使用 enlighten 库)

def api_call():
#   Calling function containing the JOBS API endpoint for calling, until its RESPONSE == data requested.
    endpoint_url = endpoint_initializer()

#   Calling function containing API credentials
    key, secret, url, group_url = ini_reader()
    
#   Setting variable for use in progress bar, used to reflect API 'percent_complete' key pair value.
    BAR_FORMAT = u'{id_value} {percentage:3.0f}%|{bar}| ' u'[{elapsed}<{eta}, {rate:.2f} %/s]'
    manager = enlighten.get_manager()
    
    date = dt.datetime.today().strftime("%Y-%m-%d")
    print("------------------------------------\n","API URL constructed for:", date, "\n------------------------------------")
    print("----------------------------------------------------------------------\n","Addepar Endpoint:", endpoint_url, "\n----------------------------------------------------------------------") 
    
#   Setting variable with counter for progress bar.
    pbar = manager.counter(total=100, bar_format=BAR_FORMAT)

    while True:
        response = requests.get(url = endpoint_url, auth = HTTPBasicAuth(key, secret), headers = {"Vendor-firm": "665"})
        api_response = json.loads(response.text) 
        
        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 = api_response["data"]["attributes"].get("percent_complete", '')*100
            pbar.count = res2
            pbar.update(incr=0, id_value=id_value)
            time.sleep(5)
        
        elif "meta" in api_response:
            pbar.count = 100
            pbar.update(incr=0, id_value=id_value)
            pbar.close()
            return api_response

我如何扩展此函数以测试响应 (api_response) 是否包含二进制文件,如果是,则 return api_response?

一个正常的 http-server 应该 return 一个适当的 content-type。请检查:

response.headers['content-type']

根据 Markus 的响应,通过访问 Content-Type response.headers,我能够三元化响应是二进制的 (Content-Type: application/binary) 还是 JSON (Content-Type: application/vnd.api+json).