def get_applications(artifact_dir: str, endpoint: str, auth_token: str,
                     extra_data: bool):
    params = {"IncludeModules": extra_data, "IncludeEnvStatus": extra_data}
    # Sends the request
    response = send_get_request(endpoint, auth_token, APPLICATIONS_ENDPOINT,
                                params)
    status_code = int(response["http_status"])
    # Process the response based on the status code returned from the server
    if status_code == APPLICATIONS_SUCCESS_CODE:
        # Stores the result
        store_data(artifact_dir, APPLICATIONS_FILE, response["response"])
        return response["response"]
    elif status_code == APPLICATIONS_EMPTY_CODE:
        raise NoAppsAvailableError(
            "No applications available in the infrastructure. Details {}".
            format(response["response"]))
    elif status_code == APPLICATIONS_FLAG_FAILED_CODE:
        raise InvalidParametersError(
            "There was an error with the 'extra_data' flag or the request was invalid when listing all applications. The params used were: {}. Details: {}"
            .format(params, response["response"]))
    elif status_code == APPLICATIONS_FAILED_CODE:
        raise ServerError("Failed to list the applications. Details {}".format(
            response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def get_deployments(artifact_dir: str, endpoint: str, auth_token: str,
                    date: str):
    # Builds the parameters for the api call
    params = {"MinDate": date}
    # Sends the request
    response = send_get_request(endpoint, auth_token, DEPLOYMENTS_ENDPOINT,
                                params)
    status_code = int(response["http_status"])
    if status_code == DEPLOYMENTS_SUCCESS_CODE:
        # Stores the result
        store_data(artifact_dir, DEPLOYMENTS_FILE, response["response"])
        return response["response"]
    elif status_code == DEPLOYMENTS_EMPTY_CODE:
        raise NoDeploymentsError(
            "There are no deployments starting on {} until now. Details: {}".
            format(date, response["response"]))
    elif status_code == DEPLOYMENTS_INVALID_CODE:
        raise InvalidParametersError(
            "Invalid request starting on {} until now. Parameters: {}. Details: {}"
            .format(date, params, response["response"]))
    elif status_code == DEPLOYMENTS_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to see the deployment list. Details: {}"
            .format(response["response"]))
    elif status_code == DEPLOYMENTS_FAILED_CODE:
        raise ServerError("Failed to list the deployments. Details {}".format(
            response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def get_deployment_status(artifact_dir: str, endpoint: str, auth_token: str,
                          deployment_key: str):
    # Builds the API call
    query = "{}/{}/{}".format(DEPLOYMENTS_ENDPOINT, deployment_key,
                              DEPLOYMENT_STATUS_ENDPOINT)
    # Sends the request
    response = send_get_request(endpoint, auth_token, query, None)
    status_code = int(response["http_status"])
    if status_code == DEPLOYMENT_STATUS_SUCCESS_CODE:
        # Stores the result
        filename = "{}{}".format(deployment_key, DEPLOYMENT_STATUS_FILE)
        filename = os.path.join(DEPLOYMENT_FOLDER, filename)
        store_data(artifact_dir, filename, response["response"])
        return response["response"]
    elif status_code == DEPLOYMENT_STATUS_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to see the details of that deployment. Details: {}"
            .format(response["response"]))
    elif status_code == DEPLOYMENT_STATUS_NO_DEPLOYMENT_CODE:
        raise NoDeploymentsError(
            "There are no deployments with the key {}. Details: {}".format(
                deployment_key, response["response"]))
    elif status_code == DEPLOYMENT_STATUS_FAILED_CODE:
        raise ServerError(
            "Failed to get the status of deployment with key {}. Details: {}".
            format(deployment_key, response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def send_deployment(artifact_dir: str, endpoint: str, auth_token: str,
                    lt_api_version: int, app_keys: list, dep_note: str,
                    source_env: str, dest_env: str):
    # builds the deployment plan
    deployment_request = _create_deployment_plan(artifact_dir, endpoint,
                                                 lt_api_version, auth_token,
                                                 app_keys, dep_note,
                                                 source_env, dest_env)
    # Sends the request
    response = send_post_request(endpoint, auth_token, DEPLOYMENTS_ENDPOINT,
                                 deployment_request)
    status_code = int(response["http_status"])
    if status_code == DEPLOYMENT_SUCCESS_CODE:
        return response["response"]
    elif status_code == DEPLOYMENT_INVALID_CODE:
        raise InvalidParametersError(
            "The request is invalid. Check the body of the request for errors. Body: {}. Details: {}."
            .format(deployment_request, response["response"]))
    elif status_code == DEPLOYMENT_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to create the deployment. Details: {}"
            .format(response["response"]))
    elif status_code == DEPLOYMENT_NO_ENVIRONMENT_CODE:
        raise EnvironmentNotFoundError(
            "Can't find the source or target environment. Details: {}.".format(
                response["response"]))
    elif status_code == DEPLOYMENT_FAILED_CODE:
        raise ServerError(
            "Failed to create the deployment. Details: {}".format(
                response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def continue_deployment(endpoint: str, auth_token: str, deployment_key: str):
    # Builds the API call
    query = "{}/{}/{}".format(DEPLOYMENTS_ENDPOINT, deployment_key,
                              DEPLOYMENT_CONTINUE_ENDPOINT)
    # Sends the request
    response = send_post_request(endpoint, auth_token, query, None)
    status_code = int(response["http_status"])
    if status_code == DEPLOYMENT_ACTION_SUCCESS_CODE:
        return response["response"]
    elif status_code == DEPLOYMENT_ACTION_IMPOSSIBLE_CODE:
        raise ImpossibleApplyActionDeploymentError(
            "You can't continue the deployment with key {}. Details: {}".
            format(deployment_key, response["response"]))
    elif status_code == DEPLOYMENT_ACTION_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to continue the deployment. Details: {}"
            .format(response["response"]))
    elif status_code == DEPLOYMENT_ACTION_NO_DEPLOYMENT_CODE:
        raise NoDeploymentsError(
            "There are no deployments with the key {}. Details: {}".format(
                deployment_key, response["response"]))
    elif status_code == DEPLOYMENT_ACTION_FAILED_CODE:
        raise ServerError(
            "Failed to continue the deployment with key {}. Details: {}".
            format(deployment_key, response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def get_environments(artifact_dir: str, endpoint: str, auth_token: str):
    # Sends the request
    response = send_get_request(endpoint, auth_token, ENVIRONMENTS_ENDPOINT,
                                None)
    status_code = int(response["http_status"])
    if status_code == ENVIRONMENTS_SUCCESS_CODE:
        # Stores the result
        store_data(artifact_dir, ENVIRONMENTS_FILE, response["response"])
        return response["response"]
    elif status_code == ENVIRONMENTS_NOT_FOUND_CODE:
        raise EnvironmentNotFoundError(
            "No environments found. Details {}".format(response["response"]))
    elif status_code == ENVIRONMENTS_FAILED_CODE:
        raise ServerError(
            "Failed to list the environments. Details: {}".format(
                response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def get_environment_app_version(artifact_dir: str, endpoint: str,
                                auth_token: str, extra_data: bool, **kwargs):
    # Tuple with (AppName, AppKey): app_tuple[0] = AppName; app_tuple[1] = AppKey
    app_tuple = _get_application_info(artifact_dir, endpoint, auth_token,
                                      **kwargs)
    # Tuple with (EnvName, EnvKey): env_tuple[0] = EnvName; env_tuple[1] = EnvKey
    env_tuple = _get_environment_info(artifact_dir, endpoint, auth_token,
                                      **kwargs)
    # Builds the query and arguments for the call to the API
    query = "{}/{}/{}/{}".format(ENVIRONMENTS_ENDPOINT, env_tuple[1],
                                 ENVIRONMENT_APPLICATIONS_ENDPOINT,
                                 app_tuple[1])
    arguments = {"IncludeEnvStatus": extra_data, "IncludeModules": extra_data}
    # Sends the request
    response = send_get_request(endpoint, auth_token, query, arguments)
    status_code = int(response["http_status"])
    if status_code == ENVIRONMENT_APP_SUCCESS_CODE:
        # Stores the result
        filename = "{}.{}{}".format(env_tuple[0], app_tuple[0],
                                    ENVIRONMENT_APPLICATION_FILE)
        filename = os.path.join(ENVIRONMENT_FOLDER, filename)
        store_data(artifact_dir, filename, response["response"])
        return response["response"]
    elif status_code == ENVIRONMENT_APP_NOT_STATUS_CODE:
        raise InvalidParametersError(
            "Error in the request parameters. Params: {}. Details {}".format(
                arguments, response["response"]))
    elif status_code == ENVIRONMENT_APP_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to see the application in that environment. Details: {}"
            .format(response["response"]))
    elif status_code == ENVIRONMENT_APP_NOT_FOUND:
        raise AppDoesNotExistError(
            "The application does not exist in the environment. Details: {}".
            format(response["response"]))
    elif status_code == ENVIRONMENT_APP_FAILED_CODE:
        raise ServerError(
            "Failed to access the running version of an application. Details: {}"
            .format(response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def export_app_oap(file_path: str, endpoint: str, auth_token: str,
                   env_key: str, app_key: str, app_version_key: str):
    query = "{}/{}/{}/{}/{}".format(APPLICATIONS_ENDPOINT, app_key,
                                    APPLICATION_VERSIONS_ENDPOINT,
                                    app_version_key,
                                    APPLICATION_VERSIONS_CONTENT)
    # Sends the request
    response = send_get_request(endpoint, auth_token, query, None)

    status_code = int(response["http_status"])
    if status_code == APPLICATIONS_SUCCESS_CODE:
        # Stores the result
        url_string = response["response"]
        url_string = url_string["url"]
        download_oap(file_path, auth_token, url_string)
        return
    elif status_code == APPLICATION_VERSION_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to see the details of that application. Details: {}"
            .format(response["response"]))
    elif status_code == APPLICATION_VERSION_INVALID_CODE:
        raise AppVersionsError(
            "The request is invalid for the given keys. Details: {}".format(
                response["response"]))
    elif status_code == APPLICATION_VERSIONS_EMPTY_CODE:
        raise AppDoesNotExistError(
            "No binary available for given keys. Details: {}".format(
                response["response"]))
    elif status_code == APPLICATION_VERSION_FAILED_CODE:
        raise EnvironmentNotFoundError(
            "Failed to retrieve the application. Details: {}".format(
                response["response"]))
    elif status_code == APPLICATION_VERSION_FAILED_LIST_CODE:
        raise ServerError(
            "Failed to download the oap of the application version. Details: {}"
            .format(response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def set_application_version(endpoint: str, auth_token: str, env_key: str,
                            app_key: str, change_log: str, app_version: str):
    query = "{}/{}/{}/{}/{}".format(ENVIRONMENTS_ENDPOINT, env_key,
                                    ENVIRONMENT_APPLICATIONS_ENDPOINT, app_key,
                                    APPLICATION_VERSIONS_ENDPOINT)

    version_request = {
        "ChangeLog": change_log,
        "Version": app_version,
        "MobileVersions": None
    }

    response = send_post_request(endpoint, auth_token, query,
                                 json.dumps(version_request))

    status_code = int(response["http_status"])
    if status_code == APPLICATION_VERSION_CREATE_SUCCESS_CODE:
        return response["response"]
    elif status_code == APPLICATION_VERSION_CREATE_INVALID_CODE:
        raise InvalidParametersError(
            "The request is invalid. Check the body of the request for errors. Body: {}. Details: {}."
            .format(version_request, response["response"]))
    elif status_code == APPLICATION_VERSION_CREATE_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to create the version. Details: {}"
            .format(response["response"]))
    elif status_code == APPLICATION_VERSION_CREATE_NO_ENVIRONMENT_CODE:
        raise EnvironmentNotFoundError(
            "Can't find the application or target environment. Details: {}.".
            format(response["response"]))
    elif status_code == APPLICATION_VERSION_CREATE_FAILED_CODE:
        raise ServerError(
            "Failed to tag an application, or Failed to create a new version. Details: {}"
            .format(response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))
def start_deployment(endpoint: str, auth_token: str, deployment_key: str,
                     **kwargs):
    redeploy = False
    if "redeploy_outdated" not in kwargs:
        redeploy = True
    # Builds the API call
    query = "{}/{}/{}".format(DEPLOYMENTS_ENDPOINT, deployment_key,
                              DEPLOYMENT_START_ENDPOINT)
    # If the parameter to redeploy outdated has a value, that must be included in the call
    if not redeploy:
        query = "{}?RedeployOutdated={}".format(query,
                                                kwargs["redeploy_outdated"])
    # Sends the request
    response = send_post_request(endpoint, auth_token, query, None)
    status_code = int(response["http_status"])
    if status_code == DEPLOYMENT_ACTION_SUCCESS_CODE:
        return response["response"]
    elif status_code == DEPLOYMENT_ACTION_IMPOSSIBLE_CODE:
        raise ImpossibleApplyActionDeploymentError(
            "You can't start the deployment with key {}. Details: {}".format(
                deployment_key, response["response"]))
    elif status_code == DEPLOYMENT_ACTION_NO_PERMISSION_CODE:
        raise NotEnoughPermissionsError(
            "You don't have enough permissions to start the deployment. Details: {}"
            .format(response["response"]))
    elif status_code == DEPLOYMENT_ACTION_NO_DEPLOYMENT_CODE:
        raise NoDeploymentsError(
            "There are no deployments with the key {}. Details: {}".format(
                deployment_key, response["response"]))
    elif status_code == DEPLOYMENT_ACTION_FAILED_CODE:
        raise ServerError(
            "Failed to start the deployment with key {}. Details: {}".format(
                deployment_key, response["response"]))
    else:
        raise NotImplementedError(
            "There was an error. Response from server: {}".format(response))