Private/Providers/ProviderAdapter.ps1
|
function New-ProviderCapabilities { [CmdletBinding()] param( [Parameter(Mandatory)] [pscustomobject]$Source, [Parameter()] [string[]]$SupportedInstrumentIds = @(), [Parameter()] [string[]]$EventTypes = @(), [Parameter()] [string[]]$Intervals = @(), [Parameter()] [string]$HistoryDepth ) foreach ($propertyName in @('sourceId', 'name', 'attribution', 'approvedUses', 'termsReviewStatus')) { if ($null -eq $Source.PSObject.Properties[$propertyName]) { throw "Source is missing required field '$propertyName'." } } [pscustomobject][ordered]@{ source = $Source supportedInstruments = @($SupportedInstrumentIds | Select-Object -Unique) eventTypes = @($EventTypes | Select-Object -Unique) intervals = @($Intervals | Select-Object -Unique) historyDepth = $HistoryDepth } } function Test-ProviderCapability { [CmdletBinding()] param( [Parameter(Mandatory)] [pscustomobject]$Capabilities, [Parameter()] [string]$InstrumentId, [Parameter()] [string]$EventType, [Parameter()] [string]$Interval, [Parameter(Mandatory)] [ValidateSet('display', 'cache', 'retention', 'commercialUse', 'redistribution')] [string]$Use ) if (-not [string]::IsNullOrWhiteSpace($InstrumentId) -and $InstrumentId -notin $Capabilities.supportedInstruments) { return $false } if (-not [string]::IsNullOrWhiteSpace($EventType) -and $EventType -notin $Capabilities.eventTypes) { return $false } if (-not [string]::IsNullOrWhiteSpace($Interval) -and $Interval -notin $Capabilities.intervals) { return $false } $decision = Get-SourcePolicyDecision -Sources @($Capabilities.source) ` -SourceId $Capabilities.source.sourceId -Use $Use return [bool]$decision.allowed } function New-ProviderResult { [CmdletBinding()] param( [Parameter(Mandatory)] [pscustomobject]$Capabilities, [Parameter(Mandatory)] [ValidateSet('display', 'cache', 'retention', 'commercialUse', 'redistribution')] [string]$RequestedUse, [Parameter(Mandatory)] [DateTimeOffset]$RetrievedAtUtc, [Parameter()] [Nullable[DateTimeOffset]]$SourceTimestampUtc, [Parameter(Mandatory)] [ValidateSet('live', 'delayed', 'cached', 'partial', 'unavailable')] [string]$Freshness, [Parameter(Mandatory)] [AllowEmptyCollection()] [object[]]$Records, [Parameter()] [string[]]$Warnings = @(), [Parameter()] [ValidateSet('no-data', 'unsupported', 'unavailable')] [string]$EmptyResultReason ) if (-not (Test-ProviderCapability -Capabilities $Capabilities -Use $RequestedUse)) { throw "Source '$($Capabilities.source.sourceId)' is not approved for '$RequestedUse'." } if ($Records.Count -eq 0 -and [string]::IsNullOrWhiteSpace($EmptyResultReason)) { throw 'An empty provider result requires an explicit no-data, unsupported, or unavailable reason.' } if ($Records.Count -gt 0 -and -not [string]::IsNullOrWhiteSpace($EmptyResultReason)) { throw 'An empty-result reason cannot accompany provider records.' } if ($Freshness -eq 'unavailable' -and $Records.Count -gt 0) { throw 'Unavailable provider results cannot contain records.' } foreach ($record in $Records) { if ($null -eq $record) { throw 'Provider results cannot contain null records.' } $instrumentProperty = $record.PSObject.Properties['instrumentId'] if ($null -ne $instrumentProperty -and -not (Test-ProviderCapability -Capabilities $Capabilities -InstrumentId $instrumentProperty.Value -Use $RequestedUse)) { throw "Source '$($Capabilities.source.sourceId)' returned an undeclared or ineligible instrument." } } foreach ($warning in $Warnings) { if ($warning -match '(?i)(api[_-]?key|access[_-]?token|password|secret|authorization)\s*[=:]') { throw 'Provider warnings must be sanitized and must not contain credentials.' } } [pscustomobject][ordered]@{ sourceId = $Capabilities.source.sourceId sourceName = $Capabilities.source.name attribution = $Capabilities.source.attribution retrievedAtUtc = $RetrievedAtUtc.ToUniversalTime() sourceTimestampUtc = if ($null -eq $SourceTimestampUtc) { $null } else { $SourceTimestampUtc.ToUniversalTime() } freshness = $Freshness records = @($Records) warnings = @($Warnings) emptyResultReason = $EmptyResultReason } } function Assert-ProviderRequestActive { [CmdletBinding()] param( [Parameter(Mandatory)] [System.Threading.CancellationToken]$CancellationToken ) if ($CancellationToken.IsCancellationRequested) { throw [System.OperationCanceledException]::new( 'Provider request was cancelled.', $CancellationToken ) } } function New-ProviderFailure { [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateSet('timeout', 'cancelled', 'rate-limit', 'authentication', 'invalid-payload', 'unsupported-instrument', 'unknown')] [string]$FailureType, [Parameter()] [switch]$HasEligibleCache ) $messages = @{ timeout = 'The provider request timed out.' cancelled = 'The provider request was cancelled.' 'rate-limit' = 'The provider rate limit was reached.' authentication = 'Provider authentication failed.' 'invalid-payload' = 'The provider returned data in an unsupported format.' 'unsupported-instrument' = 'The provider does not support this instrument.' unknown = 'The provider request failed.' } [pscustomobject][ordered]@{ failureType = $FailureType freshness = if ($HasEligibleCache.IsPresent) { 'cached' } else { 'unavailable' } retryable = $FailureType -in @('timeout', 'rate-limit', 'unknown') userMessage = $messages[$FailureType] } } |