Skip to content

Commit 359180a

Browse files
committed
Pause and repeat request when receive throttling (error 429) #88
1 parent 8b327c5 commit 359180a

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

source/Private/Test-FabricApiResponse.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function Test-FabricApiResponse {
7676
#$responseHeader = $script:responseHeader
7777
#$statusCode = $script:statusCode
7878
$result = $null
79-
8079
Write-Message -Message "Testing API response for '$Operation' operation. StatusCode: $statusCode." -Level Debug
8180

8281
switch ($statusCode) {
@@ -122,6 +121,15 @@ function Test-FabricApiResponse {
122121
return $operationStatus
123122
}
124123
}
124+
429 {
125+
Write-Message -Message "API rate limit exceeded. Status Code: $statusCode ($($Response.errorCode))" -Level Warning
126+
Write-Message -Message "$($Response.message). Waiting $($responseHeader['Retry-After']) seconds..." -Level Warning
127+
$retryAfter = $responseHeader['Retry-After']
128+
$retryAfterStr = $retryAfter[0].ToString()
129+
$retryAfterInt = [int]$retryAfterStr
130+
Start-Sleep -Seconds $retryAfterInt
131+
return "Command:Repeat"
132+
}
125133
default {
126134
Write-Message -Message "Test-FabricApiResponse::default" -Level Debug
127135
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error

source/Public/Deployment Pipeline/New-FabricDeploymentPipeline.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ function New-FabricDeploymentPipeline {
108108
} catch {
109109
# Step 6: Error handling
110110
$errorDetails = $_.Exception.Message
111-
Write-Message -Message "Failed to create deployment pipeline. Error: $errorDetails" -Level Error
111+
Write-Error -Message "Failed to create deployment pipeline. Error: $errorDetails"
112112
}
113113
}

source/Public/Invoke-FabricRestMethod.ps1

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,46 @@ Function Invoke-FabricRestMethod {
105105
Write-Message -Message "No request body provided." -Level Debug
106106
}
107107

108-
$request = @{
109-
Headers = $FabricSession.HeaderParams
110-
Uri = $Uri
111-
Method = $Method
112-
Body = $Body
113-
ContentType = "application/json"
114-
ErrorAction = 'Stop'
115-
SkipHttpErrorCheck = $true
116-
ResponseHeadersVariable = "responseHeader"
117-
StatusCodeVariable = "statusCode"
118-
}
119-
$response = Invoke-RestMethod @request
108+
$repeat = $false
109+
do {
110+
111+
$request = @{
112+
Headers = $FabricSession.HeaderParams
113+
Uri = $Uri
114+
Method = $Method
115+
Body = $Body
116+
ContentType = "application/json"
117+
ErrorAction = 'Stop'
118+
SkipHttpErrorCheck = $true
119+
ResponseHeadersVariable = "responseHeader"
120+
StatusCodeVariable = "statusCode"
121+
}
122+
$response = Invoke-RestMethod @request
120123

121-
Write-Message -Message "Result response code: $statusCode" -Level Debug
122-
if ($response) {
123-
Write-Message -Message "Result return: $response" -Level Debug
124-
}
125-
# Needed for backward compatibility, example: Get-FabricWorkspace
126-
$script:statusCode = $statusCode
127-
$script:responseHeader = $responseHeader
128-
129-
if ($HandleResponse) {
130-
$params = @{
131-
Response = $response
132-
ResponseHeader = $responseHeader
133-
StatusCode = $statusCode
134-
Operation = (Get-PSCallStack)[1].Command.Split('-')[0]
135-
ObjectIdOrName = $ObjectIdOrName
136-
TypeName = $TypeName
137-
NoWait = $NoWait
138-
SuccessMessage = $SuccessMessage
124+
Write-Message -Message "Result response code: $statusCode" -Level Debug
125+
if ($response) {
126+
Write-Message -Message "Result return: $response" -Level Debug
139127
}
140-
$response = Test-FabricApiResponse @params
141-
}
128+
# Needed for backward compatibility, example: Get-FabricWorkspace
129+
$script:statusCode = $statusCode
130+
$script:responseHeader = $responseHeader
131+
132+
if ($HandleResponse) {
133+
$params = @{
134+
Response = $response
135+
ResponseHeader = $responseHeader
136+
StatusCode = $statusCode
137+
Operation = (Get-PSCallStack)[1].Command.Split('-')[0]
138+
ObjectIdOrName = $ObjectIdOrName
139+
TypeName = $TypeName
140+
NoWait = $NoWait
141+
SuccessMessage = $SuccessMessage
142+
}
143+
$response = Test-FabricApiResponse @params
144+
$repeat = $response -is [string] -and $response -eq "Command:Repeat"
145+
}
146+
147+
} while ($repeat)
142148

143149
Write-Message -Message "::End" -Level Debug
144150
$response

0 commit comments

Comments
 (0)