|
| 1 | +function Get-Video { |
| 2 | + [CmdletBinding(DefaultParameterSetName = 'List')] |
| 3 | + [OutputType([pscustomobject])] |
| 4 | + param ( |
| 5 | + [Parameter(ParameterSetName = 'Get', Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] |
| 6 | + [ValidateNotNullOrEmpty()] |
| 7 | + [Alias('video_id')] |
| 8 | + [Alias('Id')] |
| 9 | + [string][UrlEncodeTransformation()]$VideoId, |
| 10 | + |
| 11 | + [Parameter(ParameterSetName = 'List')] |
| 12 | + [ValidateRange(0, 100)] |
| 13 | + [int]$Limit = 20, |
| 14 | + |
| 15 | + [Parameter(ParameterSetName = 'List')] |
| 16 | + [ValidateSet('asc', 'desc')] |
| 17 | + [string][LowerCaseTransformation()]$Order = 'asc', |
| 18 | + |
| 19 | + [Parameter(ParameterSetName = 'List')] |
| 20 | + [switch]$All, |
| 21 | + |
| 22 | + [Parameter(ParameterSetName = 'List', DontShow)] |
| 23 | + [string]$After, |
| 24 | + |
| 25 | + [Parameter()] |
| 26 | + [int]$TimeoutSec = 0, |
| 27 | + |
| 28 | + [Parameter()] |
| 29 | + [ValidateRange(0, 100)] |
| 30 | + [int]$MaxRetryCount = 0, |
| 31 | + |
| 32 | + [Parameter()] |
| 33 | + [OpenAIApiType]$ApiType = [OpenAIApiType]::OpenAI, |
| 34 | + |
| 35 | + [Parameter()] |
| 36 | + [System.Uri]$ApiBase, |
| 37 | + |
| 38 | + [Parameter(DontShow)] |
| 39 | + [string]$ApiVersion, |
| 40 | + |
| 41 | + [Parameter()] |
| 42 | + [ValidateSet('openai', 'azure', 'azure_ad')] |
| 43 | + [string]$AuthType = 'openai', |
| 44 | + |
| 45 | + [Parameter()] |
| 46 | + [securestring][SecureStringTransformation()]$ApiKey, |
| 47 | + |
| 48 | + [Parameter()] |
| 49 | + [Alias('OrgId')] |
| 50 | + [string]$Organization, |
| 51 | + |
| 52 | + [Parameter()] |
| 53 | + [System.Collections.IDictionary]$AdditionalQuery, |
| 54 | + |
| 55 | + [Parameter()] |
| 56 | + [System.Collections.IDictionary]$AdditionalHeaders, |
| 57 | + |
| 58 | + [Parameter()] |
| 59 | + [object]$AdditionalBody |
| 60 | + ) |
| 61 | + |
| 62 | + begin { |
| 63 | + # Get API context |
| 64 | + $OpenAIParameter = Get-OpenAIAPIParameter -EndpointName 'Videos' -Parameters $PSBoundParameters -ErrorAction Stop |
| 65 | + |
| 66 | + # Iterator flag |
| 67 | + [bool]$HasMore = $true |
| 68 | + } |
| 69 | + |
| 70 | + process { |
| 71 | + # Create cancellation token for timeout |
| 72 | + $Cancellation = [System.Threading.CancellationTokenSource]::new() |
| 73 | + if ($TimeoutSec -gt 0) { |
| 74 | + $Cancellation.CancelAfter([timespan]::FromSeconds($TimeoutSec)) |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + #region Pagenation Loop |
| 79 | + while ($HasMore) { |
| 80 | + #region Construct Query URI |
| 81 | + $UriBuilder = [System.UriBuilder]::new($OpenAIParameter.Uri) |
| 82 | + if ($PSCmdlet.ParameterSetName -eq 'Get') { |
| 83 | + $UriBuilder.Path += "/$VideoId" |
| 84 | + $QueryUri = $UriBuilder.Uri |
| 85 | + } |
| 86 | + else { |
| 87 | + $QueryParam = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query) |
| 88 | + if ($All) { |
| 89 | + $Limit = 100 |
| 90 | + } |
| 91 | + $QueryParam.Add('limit', $Limit) |
| 92 | + $QueryParam.Add('order', $Order) |
| 93 | + if ($After) { |
| 94 | + $QueryParam.Add('after', $After) |
| 95 | + } |
| 96 | + $UriBuilder.Query = $QueryParam.ToString() |
| 97 | + $QueryUri = $UriBuilder.Uri |
| 98 | + } |
| 99 | + #endregion |
| 100 | + |
| 101 | + #region Send API Request |
| 102 | + $params = @{ |
| 103 | + Method = 'Get' |
| 104 | + Uri = $QueryUri |
| 105 | + TimeoutSec = $OpenAIParameter.TimeoutSec |
| 106 | + MaxRetryCount = $OpenAIParameter.MaxRetryCount |
| 107 | + ApiKey = $OpenAIParameter.ApiKey |
| 108 | + AuthType = $OpenAIParameter.AuthType |
| 109 | + Organization = $OpenAIParameter.Organization |
| 110 | + AdditionalQuery = $AdditionalQuery |
| 111 | + AdditionalHeaders = $AdditionalHeaders |
| 112 | + AdditionalBody = $AdditionalBody |
| 113 | + } |
| 114 | + $Response = Invoke-OpenAIAPIRequest @params |
| 115 | + |
| 116 | + # error check |
| 117 | + if ($null -eq $Response) { |
| 118 | + return |
| 119 | + } |
| 120 | + #endregion |
| 121 | + |
| 122 | + #region Parse response object |
| 123 | + try { |
| 124 | + $Response = $Response | ConvertFrom-Json -ErrorAction Stop |
| 125 | + } |
| 126 | + catch { |
| 127 | + Write-Error -Exception $_.Exception |
| 128 | + return |
| 129 | + } |
| 130 | + #endregion |
| 131 | + |
| 132 | + $Cancellation.Token.ThrowIfCancellationRequested() |
| 133 | + |
| 134 | + # Update iterator flag |
| 135 | + if ($HasMore = [bool]$Response.has_more) { |
| 136 | + if ($All -and $Response.last_id) { |
| 137 | + $After = $Response.last_id |
| 138 | + } |
| 139 | + else { |
| 140 | + $HasMore = $false |
| 141 | + if (-not $PSBoundParameters.ContainsKey('Limit')) { |
| 142 | + Write-Warning 'There is more data that has not been retrieved.' |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + #region Output |
| 148 | + if ($Response.object -eq 'list' -and ($null -ne $Response.data)) { |
| 149 | + $Responses = @($Response.data) |
| 150 | + } |
| 151 | + else { |
| 152 | + $Responses = @($Response) |
| 153 | + } |
| 154 | + foreach ($res in $Responses) { |
| 155 | + ParseVideoJobObject $res |
| 156 | + } |
| 157 | + #endregion |
| 158 | + } |
| 159 | + } |
| 160 | + catch [OperationCanceledException] { |
| 161 | + Write-TimeoutError |
| 162 | + return |
| 163 | + } |
| 164 | + catch { |
| 165 | + Write-Error -Exception $_.Exception |
| 166 | + return |
| 167 | + } |
| 168 | + finally { |
| 169 | + if ($null -ne $Cancellation) { |
| 170 | + $Cancellation.Dispose() |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + end { |
| 176 | + |
| 177 | + } |
| 178 | +} |
0 commit comments