4. Exporting the case

Download the case’s final output locally.

To get the information on a case’s output, you need to make a HTTP GET request to /v1/orders-tracking/{caseId}/download.

response = requests.get(f'{API_BASE_URL}/v1/orders-tracking/{case_id}/download',
                            headers=make_authorization_header(token))
response.raise_for_status()
file_info = response.json()

From that file_info, you’ll need the extension of the file and the download link.

Here is a snippet showing how to finally get the case’s output and save it to disk:

def download(url, filename):
    """Download the url and save it on disk

    Args:
        url (str): The url to download
        filename (str): The path where to save the downloaded file.
    """
    with requests.get(url, stream=True) as response:
        response.raise_for_status()
        total_size = int(response.headers['Content-Length'])
        number_of_bytes_downloaded = 0
        progress = 0
        with open(filename, 'wb') as file:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    file.write(chunk)
                    number_of_bytes_downloaded += len(chunk)
                    progress = floor(number_of_bytes_downloaded / total_size * 100)
                    log.info('Downloading... %s %%', progress)
    log.info('File downloaded as "%s".', filename)


def download_case_output(token, case_id):
    """Download a case's final output

    Args:
        token (str): The authorisation token
        case_id (str): The id of the case
    """
    response = requests.get(f'{API_BASE_URL}/v1/orders-tracking/{case_id}/download',
                            headers=make_authorization_header(token))
    response.raise_for_status()
    file_info = response.json()

    extension = file_info['file']['extension']
    filename = f'{case_id}.{extension}'
    link = file_info['link']
    download(link, filename)

You will be billed when downloading the case’s output for the first time, but you can re-download the same case’s output as many times as necessary aftewards, without further fees.