From ba08e49ff99b868cf3076b16f9181e4d9bc69250 Mon Sep 17 00:00:00 2001 From: Chris Taylor Date: Fri, 27 Jan 2023 13:30:18 -0700 Subject: [PATCH] UseBasicParsing --- Private/Invoke-Pax8Request.ps1 | 23 ++++++++++++++--------- Public/Connect-Pax8.ps1 | 15 +++++++-------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Private/Invoke-Pax8Request.ps1 b/Private/Invoke-Pax8Request.ps1 index 4bb5a11..44a30ce 100644 --- a/Private/Invoke-Pax8Request.ps1 +++ b/Private/Invoke-Pax8Request.ps1 @@ -9,21 +9,23 @@ function Invoke-Pax8Request { if (!$script:Pax8Token) { Write-Host "Please run 'Connect-Pax8' first" -ForegroundColor Red - } else { + } + else { $headers = @{ Authorization = "Bearer $($script:Pax8Token)" } try { - if (($Method -eq "put") -or ($Method -eq "post") -or ($Method -eq "delete")) { - $Response = Invoke-WebRequest -method $method -uri ($Script:Pax8BaseURL + $Resource) -ContentType 'application/json' -body $Body -Headers $headers -ea stop + if (($Method -eq 'put') -or ($Method -eq 'post') -or ($Method -eq 'delete')) { + $Response = Invoke-WebRequest -Method $method -Uri ($Script:Pax8BaseURL + $Resource) -ContentType 'application/json' -Body $Body -Headers $headers -UseBasicParsing -ea stop $Result = $Response | ConvertFrom-Json - } else { + } + else { $Complete = $false $PageNo = 0 $Result = do { - $Response = Invoke-WebRequest -method $method -uri ($Script:Pax8BaseURL + $Resource + "?page=$PageNo&size=200" + $ResourceFilter) -ContentType 'application/json' -Headers $headers -ea stop + $Response = Invoke-WebRequest -Method $method -Uri ($Script:Pax8BaseURL + $Resource + "?page=$PageNo&size=200" + $ResourceFilter) -ContentType 'application/json' -Headers $headers -UseBasicParsing -ea stop $JSON = $Response | ConvertFrom-Json if ($JSON.Page) { if (($JSON.Page.totalPages - 1) -eq $PageNo -or $JSON.Page.totalPages -eq 0) { @@ -31,18 +33,21 @@ function Invoke-Pax8Request { } $PageNo = $PageNo + 1 $JSON.content - } else { + } + else { $Complete = $true $JSON } } while ($Complete -eq $false) } - } catch { + } + catch { if ($_.Response.StatusCode -eq 429) { - Write-Warning "Rate limit exceeded. Waiting to try again." + Write-Warning 'Rate limit exceeded. Waiting to try again.' Start-Sleep 8 $Result = Invoke-Pax8Request -Method $Method -Resource $Resource -ResourceFilter $ResourceFilter -Body $Body - } else { + } + else { Write-Error "An Error Occured $($_) " } } diff --git a/Public/Connect-Pax8.ps1 b/Public/Connect-Pax8.ps1 index 29c3d03..c36a5ec 100644 --- a/Public/Connect-Pax8.ps1 +++ b/Public/Connect-Pax8.ps1 @@ -11,19 +11,18 @@ function Connect-Pax8 { $auth = @{ client_id = $ClientID client_secret = $ClientSecret - audience = "api://p8p.client" - grant_type = "client_credentials" + audience = 'api://p8p.client' + grant_type = 'client_credentials' } - $json = $auth | ConvertTo-json -Depth 2 + $json = $auth | ConvertTo-Json -Depth 2 try { - $Response = Invoke-WebRequest -Method POST -Uri 'https://login.pax8.com/oauth/token' -ContentType 'application/json' -Body $json + $Response = Invoke-WebRequest -Method POST -Uri 'https://login.pax8.com/oauth/token' -ContentType 'application/json' -Body $json -UseBasicParsing $script:Pax8Token = ($Response | ConvertFrom-Json).access_token - } catch { - Write-Host $_ -ForegroundColor Red } - - + catch { + Write-Host $_ -ForegroundColor Red + } } \ No newline at end of file