def waitForActionToFinish(connection, replyObj, actionUrl): ''' This method waits for an action to finish executing. after a POST request is sent in order to start an action, The HTTP reply will contain, in the header, a 'location' field, that contains an URL. The action URL contains the status of the action. we perform a GET on that URL every 0.5 seconds until the action finishes with a success. If the action fails, we will throw an error and print the action's error message. Args: - connection is the connection object that manages the HTTP data transfers between the client and the REST API - replyObj the reply object holding the location - actionUrl - the url pointing to the operation ''' actionStatusSuccess = {"v0": lambda x: x.state == kActionStateFinished and x.status == kActionStatusSuccessful, "v1": lambda x: x.state == kActionStateSuccess} actionStatusError = {"v0": lambda x: x.status == kActionStatusError, "v1": lambda x: x.state == kActionStateError} messageError = {"v0": lambda x: x.error, "v1": lambda x: x.message} actionResultURL = replyObj.headers.get('location') apiVersion = getApiVersion(connection) if actionResultURL: actionResultURL = stripApiAndVersionFromURL(actionResultURL) actionFinished = False while not actionFinished: actionStatusObj = connection.httpGet(actionResultURL) if actionStatusSuccess[apiVersion](actionStatusObj): actionFinished = True elif actionStatusError[apiVersion](actionStatusObj): errorMsg = "Error while executing action '%s'." % actionResultURL errorMsg += messageError[apiVersion](actionStatusObj) raise Exception(errorMsg) else: time.sleep(0.1)