3. Getting a case's status

Wait until the case has the status “Completed”.

Getting the list of possible states

The following snippet show how to get the list of all possible state a case can be in. It is not necessary for a production script, but it might be useful during development.

import requests

API_BASE_URL = "https://api.shapeshift3d.com"

def get_case_statuses(token):
    """Get the list of possible statuses

    Args:
        token (str): The authorisation token

    Returns:
        list(str): The list of statuses

    Example:
        print(get_case_statuses(token))
    """
    response = requests.get(f'{API_BASE_URL}/v1/orders-tracking/statuses',
                            headers=make_authorization_header(token))
    response.raise_for_status()
    return response.json()

Getting the case’s status and waiting for it to be completed

To get the cases status, simply make a HTTP request to /v1/orders-tracking/{caseId}/status.

A case will be done when its state is either Completed or one of the multiple states that ends with Error.

So, you can simply poll the state of the case until it has one of these states.

import requests
import logging

from sys import exit
from time import sleep

API_BASE_URL = "https://api.shapeshift3d.com"

def get_case_status(token, case_id):
    """Get a case's current status

    Args:
        token (str): The authorisation token.
        case_id (str): The caseId of the case to start.

    Returns:
        str: The status of the case as a string.
    """
    response = requests.get(f'{API_BASE_URL}/v1/orders-tracking/{case_id}/status',
                            headers=make_authorization_header(token))
    response.raise_for_status()
    return response.json()['status']

def wait_for_case_to_be_completed(token, case_id, interval=5):
    """Poll the API until the case is completed or failed.

    Args:
        token (str): The authorisation token
        case_id (str): The id of the case (order)
        interval (int, optional): Time to wait before each poll (in seconds). Defaults to 5 seconds.
    """
    status = get_case_status(token, case_id)
    logging.info(status)
    while not (status == 'Completed' or status.endswith('Error')):
        sleep(interval)  # wait a few seconds
        status = get_case_status(token, case_id)
        logging.info(status)
    if status.endswith('Error'):
        logging.error("An error occured while processing this case.")
        exit(1)