Pax8API/Private/Invoke-Pax8ApiOperation.ps1
|
function Invoke-Pax8ApiOperation { [CmdletBinding()] param ( [Parameter(Mandatory)] [string]$CommandName, [Parameter(Mandatory)] [hashtable]$Parameters ) if (-not $script:Pax8OperationMap.ContainsKey($CommandName)) { throw "Unknown Pax8 operation command '$CommandName'. Regenerate the module from the current OpenAPI specs." } $operation = $script:Pax8OperationMap[$CommandName] Assert-Pax8Session -Audience $operation.Audience $internalNames = @( 'All', 'Raw', 'Body', 'InputObject', 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable', 'WhatIf', 'Confirm' ) $effectiveParameters = @{} foreach ($entry in $Parameters.GetEnumerator()) { if ($entry.Key -notin $internalNames) { $effectiveParameters[$entry.Key] = $entry.Value } } $body = $null if ($Parameters.ContainsKey('Body') -and $null -ne $Parameters.Body) { $body = ConvertTo-Pax8WireValue -Value $Parameters.Body } foreach ($bodyParameter in @($operation.BodyParameters)) { $parameterName = [string]$bodyParameter.ParameterName if ($Parameters.ContainsKey($parameterName)) { if ($null -eq $body -or $body -isnot [System.Collections.IDictionary]) { $body = [ordered]@{} } $body[[string]$bodyParameter.WireName] = ConvertTo-Pax8WireValue -Value $Parameters[$parameterName] } } $method = [string]$operation.Method $raw = $Parameters.ContainsKey('Raw') -and [bool]$Parameters.Raw $useAll = $Parameters.ContainsKey('All') -and [bool]$Parameters.All $supportsPaging = @($operation.Parameters | Where-Object { $_.In -eq 'query' -and $_.WireName -eq 'page' }).Count -gt 0 $supportsPaging = $supportsPaging -and @($operation.Parameters | Where-Object { $_.In -eq 'query' -and $_.WireName -eq 'size' }).Count -gt 0 if ($useAll -and $method -eq 'GET' -and $supportsPaging) { $page = 0 $results = [System.Collections.Generic.List[object]]::new() do { $pageParameters = @{} foreach ($entry in $effectiveParameters.GetEnumerator()) { $pageParameters[$entry.Key] = $entry.Value } $pageParameters['Page'] = $page $pageParameters['Size'] = 200 $uri = New-Pax8RequestUri -Operation $operation -Parameters $pageParameters $response = Invoke-Pax8RestMethod -Uri $uri -Method $method $content = if ($response.PSObject.Properties['content']) { @($response.content) } else { @($response) } foreach ($item in $content) { $results.Add($item) } $totalPages = if ($response.PSObject.Properties['page'] -and $response.page.PSObject.Properties['totalPages']) { [int]$response.page.totalPages } else { $page + 1 } $page++ } while ($page -lt $totalPages) if ($raw) { return $results } return $results } $uri = New-Pax8RequestUri -Operation $operation -Parameters $effectiveParameters $response = Invoke-Pax8RestMethod -Uri $uri -Method $method -Body $body Get-Pax8ResponseObject -Response $response -Raw:$raw } |