public/Invoke-RetryRequest.ps1

<#
.SYNOPSIS
  This function invokes a request with retry logic for rate limiting (HTTP 429 responses).

.DESCRIPTION
  The Invoke-RetryRequest function sends a request using the provided parameters.
  If the API returns a 429 response (Too Many Requests), the function will wait and retry the request.
  The wait time increases exponentially with each subsequent 429 response.

.PARAMETER method
  Optional. The HTTP method to use for the request. Defaults to 'get'.

.PARAMETER uri
  Mandatory. The URI of the endpoint to which the request will be sent.

.PARAMETER headers
  Optional. A hashtable of headers to include in the request.

.PARAMETER body
  Optional. The body of the request, if applicable, if method is get, used as query parameter.

.PARAMETER maxRetrySeconds
  Optional. The maximum number of seconds to continue retrying the request. Defaults to 2000.

.EXAMPLE
  Invoke-RetryRequest -uri "https://graph.microsoft.com/beta/users" -method "get"
#>


function Invoke-RetryRequest {
  [CmdletBinding()]
  param (
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $method = 'get',
    [Parameter(Mandatory,
      ValueFromPipelineByPropertyName)]
    [string]
    $uri,
    [Parameter(Mandatory = $false)]
    [hashtable]
    $headers = $null,
    [Parameter(Mandatory = $false)]
    [object]
    $body = $null,
    [Parameter(Mandatory = $false)]
    [int]
    $maxRetrySeconds = 2000
  )
  Write-Verbose "Invoking retry request with uri: $uri"
  $sleepDuration = 0
  $retry = $false
  do {
    try {
      $retry = $false
      Invoke-RestMethod -Method $method -Headers $headers -ContentType 'application/json' -Uri $uri -Body $body -ErrorAction Stop -Verbose:$false
    }
    catch {
      if ($_.Exception.Response.StatusCode.value__ -ne 429) { $retry = $false; throw $_ }
      $sleepDuration = $sleepDuration -eq 0 ? 4 : $sleepDuration * 2
      $retry = $true
      Write-Verbose "API returned 429, retrying in $sleepDuration seconds"
      Start-Sleep -Seconds $sleepDuration
    }
  } until (
    -not $retry -or ($sleepDuration -ge $maxRetrySeconds)
  )
}

# SIG # Begin signature block
# MIIVrwYJKoZIhvcNAQcCoIIVoDCCFZwCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEo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