Private/PartnerCenter/Invoke-PartnerCenterRequest.ps1

function Invoke-PartnerCenterRequest {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Uri,
        
        [Parameter()]
        [string]$Method = 'GET',
        
        [Parameter()]
        [object]$Body,
        
        [Parameter()]
        [string]$ContentType = 'application/json'
    )

    try {
        # Get Partner Center token
        $token = Get-PartnerAccessToken -TenantId $script:Config.PartnerTenantId `
            -Scopes "https://api.partnercenter.microsoft.com/user_impersonation"

        $headers = @{
            'Authorization' = "Bearer $($token.access_token)"
            'Accept'        = 'application/json'
        }

        $params = @{
            Uri     = $Uri
            Method  = $Method
            Headers = $headers
        }

        if ($Body) {
            $params.Body = if ($Body -is [string]) { $Body } else { $Body | ConvertTo-Json -Depth 100 }
            $params.ContentType = $ContentType
        }

        $response = Invoke-RestMethod @params -SkipHttpErrorCheck -StatusCodeVariable StatusCode
        # Add status code
        $response | Add-Member -NotePropertyName 'StatusCode' -NotePropertyValue $StatusCode
        return $response
    }
    catch {
        Write-ModuleLog -Message "Failed to execute Partner Center request" -Level Error -Component 'PartnerCenter' `
            -ErrorRecord $_ -ThrowError -ErrorOperation 'PartnerCenterRequest' -ErrorMessage 'Failed to execute Partner Center request'
    }
}