Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Private/Invoke-Pax8Request.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,45 @@ 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) {
$Complete = $true
}
$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 $($_) "
}
}
Expand Down
15 changes: 7 additions & 8 deletions Public/Connect-Pax8.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}