CCRestMethod.ps1


#
# Citrix API Rest Call (Support Export/Prepare/GetJob)
#


Function BuildUrl([string]$deployment, [string]$serviceRoute, [bool]$async) {
    if ($deployment) {
        $baseUrl = "https://$deployment/"
    } else {
        $baseUrl = "https://api.layering.cloud.com/"
    }
    if ($async) {
        $async_str = "true"
    } else {
        $async_str = "false"
    }
    [string]$url = $baseUrl + $serviceRoute + "?async=$async_str"

    return $url
}

Function GetAuthHeaders([string]$customerId, [string]$secureClientId, [string]$secureSecret) {
    if ([string]::IsNullOrEmpty($GLOBAL:XDAuthToken)) {
        try {
            $parameters = AuthToCitrixCloud $CustomerId $SecureClientId $SecureSecret
        }
        catch {
            throw "Failed to get bearer token: $_"
        }
    }
    $headers = @{
        'Authorization'     = $GLOBAL:XDAuthToken
        'Citrix-CustomerId' = $customerId
        'Accept'            = 'application/json'
        'Content-Type'      = 'application/json;charset=utf-8'
    }
    return $headers
}

Function Invoke-CCRestMethod(
    [string]$method,
    [string]$deployment,
    [string]$serviceRoute,
    [string]$customerId,
    [string]$secureClientId,
    [string]$secureSecret,
    [bool]$async = $false,
    [psobject]$json = $null
    ) {
    $url = BuildUrl $deployment $serviceRoute $async
    $headers = GetAuthHeaders $customerId $secureClientId $secureSecret
    $moduleInfo = Get-InstalledModule "Citrix.Workloads.Portability"
    $parameters = @{
        Headers = $headers
        Method = $method
        UserAgent = "Citrix.Workloads.Portability/$($moduleInfo.Version) Powershell/$($PSVersionTable.PSVersion)"
        Verbose = $Global:LogVerbose
    }
    if ($json) {
        $parameters['Body'] = $json
    }
    try {
        return Invoke-RestMethod $url @parameters
    }
    catch {
        throw "$method REST method failed: $_"
    }
}