Functions/Invoke-IAQuery.ps1

Function Invoke-IAQuery {
    <#
        .SYNOPSIS
            Is used to invoke web requests towards the Insight Analytics API.
        .DESCRIPTION
            This function is used in this module for internal operations. It is used to invoke web requests towards the IA API.
        .OUTPUTS
            The response from the web request.
        .EXAMPLE
            Invoke-IAQuery -QueryUrl 'Groups' -Method Get
    #>

    Param(
        [Parameter(Mandatory = $true)]
        $QueryUrl,
        [Parameter(Mandatory = $true)]
        $Method,
        [Parameter(Mandatory = $false)]
        $Body,
        $ContentType = "application/json",
        $baseEndpoint = "api"
    )
    $Uri = "https://$($script:apiBaseUrl)/$baseEndpoint/$($QueryUrl)"
    write-Verbose  $Uri 
    $response = $null; 
    $response = Invoke-WebRequest -Uri $Uri -Headers $script:Headers -Method $Method -Body $Body -ContentType $ContentType -UseBasicParsing

    if ($null -ne $response.Content -and 
        $response.content -is [String] -and
        $response.content.substring(0, 1) -eq "{") {

        (ConvertFrom-Json $response.Content)
    }
    else {
        return $response
    }
}