Skip to content
Merged
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
13 changes: 13 additions & 0 deletions Private/CP/ConvertTo-CPJsonBody.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function ConvertTo-CPJsonBody {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$InputObject
)

if ($InputObject -is [string]) {
return $InputObject
}

return ($InputObject | ConvertTo-Json -Depth 20 -Compress)
}
17 changes: 17 additions & 0 deletions Private/CP/Get-CPBearerToken.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function Get-CPBearerToken {
<#
.SYNOPSIS
Returns the Catchpoint REST API v2 bearer token from the current auth state.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[PSCustomObject]$Auth
)

if (-not $Auth.BearerToken) {
throw 'Catchpoint auth state is missing a BearerToken. Use Connect-CPAccount and try again.'
}

return [System.Net.NetworkCredential]::new('', $Auth.BearerToken).Password
}
16 changes: 16 additions & 0 deletions Private/CP/Get-CPResponseData.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Get-CPResponseData {
[CmdletBinding()]
param(
$Response
)

if ($null -eq $Response) {
return $null
}

if ($Response -is [PSCustomObject] -and $null -ne $Response.PSObject.Properties['data']) {
return $Response.data
}

return $Response
}
45 changes: 45 additions & 0 deletions Private/CP/Get-CPResponseItems.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Get-CPResponseItems {
[CmdletBinding()]
param(
$Response,

[String]$ItemPropertyName
)

$result = [PSCustomObject]@{
Items = @()
HasMore = $false
Next = $null
}

if ($null -eq $Response) {
return $result
}

$payload = $Response
if ($Response -is [PSCustomObject] -and $null -ne $Response.PSObject.Properties['data'] -and $null -ne $Response.data) {
$payload = $Response.data
}

if ($payload -is [System.Array]) {
$result.Items = @($payload)
return $result
}

if ($ItemPropertyName -and $null -ne $payload.PSObject.Properties[$ItemPropertyName] -and $null -ne $payload.$ItemPropertyName) {
$result.Items = @($payload.$ItemPropertyName)
}
elseif ($null -ne $payload.PSObject.Properties['id'] -or $null -ne $payload.PSObject.Properties['name']) {
$result.Items = @($payload)
}

if ($null -ne $payload.PSObject.Properties['hasMore'] -and $payload.hasMore) {
$result.HasMore = [bool]$payload.hasMore
}

if ($null -ne $payload.PSObject.Properties['next'] -and -not [string]::IsNullOrWhiteSpace([string]$payload.next)) {
$result.Next = [string]$payload.next
}

return $result
}
77 changes: 77 additions & 0 deletions Private/CP/Invoke-CPApiRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function Invoke-CPApiRequest {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String]$ResourcePath,

[ValidateSet('GET', 'POST', 'PATCH', 'PUT', 'DELETE')]
[String]$Method = 'GET',

[Hashtable]$QueryParameters,

$Body,

[String]$ItemPropertyName,

[String]$TypeName,

[System.Management.Automation.PSCmdlet]$CallerPSCmdlet,

[System.Management.Automation.InvocationInfo]$Command
)

$headers = New-CPHeader -Auth $Script:CPAuth -Method $Method
$uri = Join-CPUri -PortalUrl $Script:CPAuth.PortalUrl -ResourcePath $ResourcePath -QueryString (New-CPQueryString -Parameters $QueryParameters)
$jsonBody = $null
if ($PSBoundParameters.ContainsKey('Body') -and $null -ne $Body) {
$jsonBody = ConvertTo-CPJsonBody -InputObject $Body
}

if ($Command) {
Resolve-CPDebugInfo -Url $uri -Headers $headers -Command $Command -Payload $jsonBody
}

$invokeParams = @{
Uri = $uri
Method = $Method
Headers = $headers
Auth = $Script:CPAuth
EnableDebugLogging = ($DebugPreference -ne 'SilentlyContinue')
CallerPSCmdlet = $CallerPSCmdlet
}

if ($jsonBody) {
$invokeParams.Body = $jsonBody
}

$response = Invoke-CPRestMethod @invokeParams

if ($ItemPropertyName) {
$page = Get-CPResponseItems -Response $response -ItemPropertyName $ItemPropertyName
if ($page.Items.Count -eq 0) {
Write-Output @() -NoEnumerate
return
}

if ($TypeName) {
if ($page.Items.Count -eq 1) {
return (Add-ObjectTypeInfo -InputObject $page.Items[0] -TypeName $TypeName)
}

return (Add-ObjectTypeInfo -InputObject $page.Items -TypeName $TypeName)
}

return $page.Items
}

$data = Get-CPResponseData -Response $response
if ($null -eq $data) {
return
}

if ($TypeName) {
return (Add-ObjectTypeInfo -InputObject $data -TypeName $TypeName)
}

return $data
}
70 changes: 70 additions & 0 deletions Private/CP/Invoke-CPPaginatedGet.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function Invoke-CPPaginatedGet {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String]$ResourcePath,

[Hashtable]$QueryParameters = @{},

[String]$ItemPropertyName,

[ValidateRange(1, 100)]
[Int]$PageSize = 100,

[Nullable[int]]$PageNumber,

[System.Management.Automation.PSCmdlet]$CallerPSCmdlet,

[System.Management.Automation.InvocationInfo]$Command
)

$headers = New-CPHeader -Auth $Script:CPAuth -Method GET
$enableDebugLogging = $DebugPreference -ne 'SilentlyContinue'
$results = New-Object System.Collections.Generic.List[object]
$currentPage = if ($PageNumber) { [int]$PageNumber } else { 1 }
$singlePage = $null -ne $PageNumber -and $PageNumber -gt 0
$nextUri = $null

do {
$query = @{} + $QueryParameters
$query['pageNumber'] = $currentPage
$query['pageSize'] = $PageSize

$uri = if ($nextUri) {
$nextUri
}
else {
Join-CPUri -PortalUrl $Script:CPAuth.PortalUrl -ResourcePath $ResourcePath -QueryString (New-CPQueryString -Parameters $query)
}

if ($Command) {
Resolve-CPDebugInfo -Url $uri -Headers $headers -Command $Command
}

$response = Invoke-CPRestMethod -Uri $uri -Method GET -Headers $headers -Auth $Script:CPAuth `
-CallerPSCmdlet $CallerPSCmdlet -EnableDebugLogging:$enableDebugLogging

$page = Get-CPResponseItems -Response $response -ItemPropertyName $ItemPropertyName
foreach ($item in $page.Items) {
$results.Add($item)
}

if ($singlePage) {
break
}

$nextUri = $null
if ($page.Next -and $page.Next -match '^https?://') {
$nextUri = $page.Next
}
elseif ($page.HasMore) {
$currentPage++
}
else {
break
}
}
while ($true)

return @($results.ToArray())
}
Loading
Loading