Home > Mobile >  How to test an API response for binary
How to test an API response for binary

Time:04-17

Question:
What is the easiest way to test an API response for a binary response?

Context:
I have a function that makes an API Call for some data. The response (api_response) of that API call will either be JSON or binary. If JSON, and if it contains percent_complete then the data is not yet ready, and my function uses the percent_complete key:pair value to update a progress bar for the user.

If the response is JSON and contains meta, then my data is ready and has returned as a JSON object.

If the response is binary, then my data is also ready, however has been returned as .xlsx [binary]. This is the response when the data is not ready and you will see percent_complete is utilized for a progress bar -

{
    "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": []

Current function:
The following function continues to call the API every 5-seconds (using the 7-digit code in self, per the above API object) until meta is returned in a JSON object (therefore my data is returned as a JSON object) and returns the JSON object as api_response.

Otherwise, the API call continues every 5-seconds and simply uses percent_complete to update a status bar (using enlighten library)

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

How would I expand this function to test if the response (api_response) contains binary and if so, then return api_response?

CodePudding user response:

A normal http-server should return an appropriate content-type. Please check:

response.headers['content-type']

CodePudding user response:

Per Markus response, by accessing the Content-Type response.headers, I am able to trinagulate whether the response is binary (Content-Type: application/binary) or JSON (Content-Type: application/vnd.api json).

  • Related