Invoke-Api.ps1

function Invoke-Api
{
    param(
        [Parameter(Mandatory)]
        [Microsoft.PowerShell.Commands.WebRequestMethod] $Method,
        [Parameter(Mandatory)]
        [Uri] $RelativeUrl,
        [object] $JsonBody,
        [string] $FileBody
    )

    if ($JsonBody -and $FileBody)
    {
        throw [ArgumentException] "JsonBody and FileBody cannot be used together."
    }

    if (!$Script:AppAlias -or !$Script:Headers)
    {
        throw "Session token not set. Call Connect-Cloud to authenticate."
    }

    $uri = New-Object Uri $Script:CloudUrl, $RelativeUrl

    if ($JsonBody)
    {
        return Invoke-RestMethod -Method $Method -Uri $uri -Headers $Script:Headers -ContentType "application/json" -Body $($JsonBody | ConvertTo-Json -Depth 5)
    }

    if ($FileBody)
    {
        return Invoke-RestMethod -Method $Method -Uri $uri -Headers $Script:Headers -ContentType "multipart/form-data" -InFile $FileBody
    }

    return Invoke-RestMethod -Method $Method -Uri $uri -Headers $Script:Headers
}