PublicAPI.ps1

function Get-LogetoEntity {
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true)] [string]$AccountName,
    [Parameter(Mandatory=$true)] [string]$AccessKey,
    [Parameter(Mandatory=$true)] [string]$Entity,
    $Params,
    [int]$ApiVersion = 1,
    [switch]$PostAccountNameInHeader,
    [switch]$SingleSegment

)
    if (!$Params)
    {
        $Params = @{}
    }

    $headers = @{'SecurityToken' = "$($AccountName):$($AccessKey)"}

    $accountName = $AccountName.ToLower()

    $domain = "https://$($accountName).logeto.com"

    if ($PostAccountNameInHeader)
    {
        if ($accountName.StartsWith("systemart") -or $accountName.StartsWith("betadb"))
        {
            $domain = "https://beta.logeto.com"    
        }
        elseif ($accountName.StartsWith("alfadb"))
        {
            $domain = "https://alfa.logeto.com"    
        }
        else
        {
            $domain = "https://app.logeto.com" 
        }
    }
    else
    {
        $domain = "https://$($accountName).logeto.com"
    }

    $uriBuilder = [System.UriBuilder]::new("$($domain)/api/v$($ApiVersion)/$($Entity)")

    $items = $null

    $progressId = 1

    do
    {
        Write-Progress -PercentComplete -1 -Status "Records downloaded $($items.Count)" -Activity "Retrieving records" -Id $progressId

        try
        {
            [System.Collections.ArrayList] $query = @()
            foreach ($param in $Params.GetEnumerator()) {
                $value =  $param.Value
                if ($value -is [DateTime])
                {
                    $value = $value.ToString("yyyy-MM-dd")
                }
                $key = [System.Net.WebUtility]::UrlEncode($param.Name)
                $value = [System.Net.WebUtility]::UrlEncode($value)
                $query.Add("${key}=${value}") | Out-Null
            }

            $uriBuilder.Query = ($query -join '&')
            $uri = $uriBuilder.ToString()

            $result = Invoke-RestMethod -Method Get -Uri $uri -ErrorVariable $errorResult -Headers $headers
        }
        catch
        {
            $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
            $result = $streamReader.ReadToEnd() | ConvertFrom-Json
            $streamReader.Close()
            return $result
        }

        if ($SingleSegment)
        {
            Write-Progress -Completed -Activity "Retrieving records" -Id $progressId
            return $result
        }

        if ($result.Items)
        {
            $items = $items + $result.Items
            Write-Progress -PercentComplete -1 -Status "Records downloaded $($items.Count)" -Activity "Retrieving records" -Id $progressId
        }

        if ($result.ContinuationToken)
        {
            $params["ContinuationToken"] = $result.ContinuationToken
        }
    } 
    while ($result -and $result.ContinuationToken)

    Write-Progress -Completed -Activity "Retrieving records" -Id $progressId

    return $items
}

function ConvertTo-LogetoIndexedTable
{
[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true, Position=0)] $Items,
    [Parameter(Mandatory=$true)] $LookupProperty
)
    $result = @{}
    foreach ($item in $Items)
    {
        $result[$item.$LookupProperty] = $item    
    }
    return $result
}