Private/Get-FloRecruitNextPage.ps1
|
function Get-FloRecruitNextPage { <# .SYNOPSIS Extracts pagination information from HTTP response headers. .DESCRIPTION Parses response headers to determine if there are additional pages of data and extracts the cursor or link for the next page. .PARAMETER Headers The headers hashtable from the HTTP response. .OUTPUTS PSCustomObject with pagination information. #> [CmdletBinding()] param( [Parameter(Mandatory)] [hashtable]$Headers ) $result = [PSCustomObject]@{ PSTypeName = 'FloRecruit.PaginationInfo' NextCursor = $null NextUrl = $null TotalCount = $null HasNextPage = $false } # Check for Link header (RFC 5988) if ($Headers.ContainsKey('Link')) { $linkHeader = $Headers['Link'] # Parse Link header for rel="next" if ($linkHeader -match '<([^>]+)>;\s*rel="next"') { $result.NextUrl = $Matches[1] $result.HasNextPage = $true } } # Check for custom pagination headers if ($Headers.ContainsKey('X-Next-Cursor')) { $result.NextCursor = $Headers['X-Next-Cursor'] $result.HasNextPage = $true } if ($Headers.ContainsKey('X-Total-Count')) { $result.TotalCount = [int]$Headers['X-Total-Count'] } # Alternative header names if ($Headers.ContainsKey('Next-Cursor')) { $result.NextCursor = $Headers['Next-Cursor'] $result.HasNextPage = $true } if ($Headers.ContainsKey('Total-Count')) { $result.TotalCount = [int]$Headers['Total-Count'] } return $result } function Get-FloRecruitAllPages { <# .SYNOPSIS Automatically retrieves all pages of data from a paginated endpoint. .DESCRIPTION Makes multiple API requests to retrieve all pages of data, handling pagination automatically. .PARAMETER Endpoint The API endpoint to query. .PARAMETER QueryParameters Initial query parameters for the request. .PARAMETER Method The HTTP method to use (default: GET). #> [CmdletBinding()] param( [Parameter(Mandatory)] [string]$Endpoint, [Parameter()] [hashtable]$QueryParameters = @{}, [Parameter()] [ValidateSet('GET', 'POST')] [string]$Method = 'GET' ) $allResults = @() $currentParams = $QueryParameters.Clone() do { # Make request with ReturnHeaders to get pagination info $response = Invoke-FloRecruitRequest -Endpoint $Endpoint -Method $Method -QueryParameters $currentParams -ReturnHeaders # Add results to collection if ($response.Data) { $allResults += $response.Data } # Check for next page $paginationInfo = Get-FloRecruitNextPage -Headers $response.Headers if ($paginationInfo.HasNextPage -and $paginationInfo.NextCursor) { # Update cursor for next request $currentParams['cursor'] = $paginationInfo.NextCursor } else { break } } while ($paginationInfo.HasNextPage) return $allResults } |