functions/github/Invoke-GitHubRestMethod.ps1

function Invoke-GitHubRestMethod
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$True)]
        [uri] $Uri,

        [Parameter()]
        [string] $Verb = 'GET',

        [Parameter()]
        [hashtable] $Body,

        [Parameter()]
        [string] $Token = $env:GITHUB_TOKEN,

        [Parameter()]
        [hashtable] $Headers = @{},

        [Parameter()]
        [int[]] $HttpErrorStatusCodesToIgnore = @(),

        [Parameter()]
        [switch] $AllPages
    )

    if ($Headers.Keys -notcontains 'Accept') {
        $Headers += @{ Accept = 'application/vnd.github.machine-man-preview+json' }
        # Accept = 'application/vnd.github.v3+json'
        # Accept = 'application/vnd.github.luke-cage-preview+json'
    }
    if ($Headers.Keys -notcontains 'Authorization') {
        $Headers += @{ Authorization = "Token $Token" }
    }

    try {
        $resp = Invoke-RestMethod -Headers $Headers  `
                                -Method $Verb `
                                -Uri $Uri `
                                -Body ($Body|ConvertTo-Json -Depth 100 -Compress) `
                                -FollowRelLink:$AllPages
    }
    catch {
        if ($_.Exception.Response.StatusCode -notin $HttpErrorStatusCodesToIgnore) {
            throw $_
        }
    }

    # Flatten the result set in the event we traversed multiple pages
    $resp | ForEach-Object { $_ }
}