Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly wait while `GET` returns the completion status equal to 0

I need to use API that performs a task in two steps:

  1. POST request: to submit a task and get back the results URL
  2. GET request: to check for a status of the results URL and get the result when the status is "completed"

Below I provide my implementation. However, I don’t know how to perform the waiting while GET returns the completion status equal to 0.

import requests
 
url = "https://xxx"
 
headers = {"Content-Type": "application/json", "Ocp-Apim-Subscription-Key": "xxx"}
 
body = {...}
 
response = requests.post(url, headers=headers, json=body)
 
status_code = response.status_code
url_result = response.headers['url-result']

# Step 2
s2_result = requests.get(url_result, headers=headers)
s2_result = s2_result.json()
s2_result_status = s2_result['completed']
if s2_result_status == 1:
# ...
else:
# wait 1 second and repeat

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to repeat the get in a loop. I suggest you use Session() to prvent connection errors

with requests.Session() as s:
    c = 0
    while c < 10: # add a limit
        s2_result = s.get(url_result, headers=headers)
        s2_result = s2_result.json()
        s2_result_status = s2_result['completed']
        if s2_result_status == 1:
            break
        else:
            time.sleep(1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading