Skip to content

Commit

Permalink
Add some basic API stats and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-peterson committed Apr 27, 2022
1 parent dd9b35e commit 9151516
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/GitlabCli/Utilities.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ function ConvertTo-UrlEncoded {
[System.Net.WebUtility]::UrlEncode($Value)
}

$global:GitlabApiStats = [PSCustomObject]@{
FirstCall = $null
LastCall = $null
SuccessCount = 0
TotalCallCount = 0
Errors = @{}
}
function Invoke-GitlabApi {
param(
[Parameter(Position=0, Mandatory=$true)]
Expand Down Expand Up @@ -162,7 +169,25 @@ function Invoke-GitlabApi {
}
else {
Write-Debug "$HttpMethod $Uri"
$Result = Invoke-RestMethod -Method $HttpMethod -Uri $Uri -Header $Headers @RestMethodParams
$global:GitlabApiStats.FirstCall = [datetime]::Now
}
$global:GitlabApiStats.LastCall = [datetime]::Now
$global:GitlabApiStats.TotalCallCount++
try {
$Result = Invoke-RestMethod -Method $HttpMethod -Uri $Uri -Header $Headers @RestMethodParams
$global:GitlabApiStats.SuccessCount++
}
catch {
$global:GitlabApiStats.Errors["$HttpMethod $Uri"] = $_.Exception
if ([int] $_.Exception.Response.StatusCode -eq 429) {
$Interval = [Math]::Round($($global:GitlabApiStats.LastCall - $global:GitlabApiStats.FirstCall).TotalSeconds)
Write-Host "You've been rate limited`nCall statistics: $($global:GitlabApiStats.TotalCallCount) over a $($Interval) second period ($([Math]::Round($global:GitlabApiStats.TotalCallCount / $Interval, 1)) / sec)"
Read-Host "Press Enter to continue"
$Result = Invoke-RestMethod -Method $HttpMethod -Uri $Uri -Header $Headers @RestMethodParams
} else {
throw
}
}
if($MaxPages -gt 1) {
# Unwrap pagination container
$Result | ForEach-Object {
Expand Down

0 comments on commit 9151516

Please sign in to comment.