Public/Invoke-PegasusRequest.ps1

<#
.SYNOPSIS
Sends a generic request to the Pegasus API

.EXAMPLE
Invoke-PegasusRequest -InputObject $Person -Method POST -Endpoint "/admin/persons"

#>

function Invoke-PegasusRequest {
    [CmdletBinding()]

    Param
    (
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        $InputObject,

        [Parameter(Mandatory = $false)]
        [ValidateSet("GET", "POST", "PATCH", "DELETE")]
        [string] $Method = "GET",

        [Parameter(Mandatory = $true)]
        [string] $Endpoint
    )
    
    Process {
        $url = "$($Script:APIRoot)$($Endpoint)"
        if ($Method -eq "GET") {
            if ($InputObject) {
                Write-Error "GET method does not support input objects"
                return
            }

            Invoke-RestMethod -Uri $url -Headers (Get-PegasusHeader) -Verbose:$false
        }
        elseif ($Method -eq "POST") {
            Invoke-RestMethod -Uri $url -Headers (Get-PegasusHeader) -Method POST -Verbose:$false -Body ($InputObject | ConvertTo-Json -Depth 10) -ContentType "application/json"
        }
        elseif ($Method -eq "PATCH") {
            Invoke-RestMethod -Uri $url -Headers (Get-PegasusHeader) -Method PATCH -Verbose:$false -Body ($InputObject | ConvertTo-Json -Depth 10) -ContentType "application/json"
        }
        elseif ($Method -eq "DELETE") {
            if ($InputObject) {
                Write-Error "DELETE method does not support input objects"
                return
            }

            Invoke-RestMethod -Uri $url -Headers (Get-PegasusHeader) -Method DELETE -Verbose:$false
        }
    }
}