|
1 | 1 | function Add-ContainerFile { |
2 | | - [CmdletBinding(DefaultParameterSetName = 'ContainerId_FileId')] |
| 2 | + [CmdletBinding()] |
3 | 3 | [OutputType([pscustomobject])] |
4 | 4 | param ( |
5 | | - [Parameter(ParameterSetName = 'Container_FileId', Mandatory, Position = 0, ValueFromPipeline)] |
6 | | - [Parameter(ParameterSetName = 'Container_File', Mandatory, Position = 0, ValueFromPipeline)] |
7 | | - [PSTypeName('PSOpenAI.Container')]$Container, |
8 | | - |
9 | | - [Parameter(ParameterSetName = 'ContainerId_FileId', Mandatory, Position = 0)] |
10 | | - [Parameter(ParameterSetName = 'ContainerId_File', Mandatory, Position = 0)] |
| 5 | + [Parameter(Mandatory, Position = 0, ValueFromPipeline)] |
11 | 6 | [ValidateNotNullOrEmpty()] |
| 7 | + [Alias('Container')] |
12 | 8 | [Alias('container_id')] |
13 | 9 | [string][UrlEncodeTransformation()]$ContainerId, |
14 | 10 |
|
15 | | - [Parameter(ParameterSetName = 'Container_FileId', Mandatory, Position = 1)] |
16 | | - [Parameter(ParameterSetName = 'ContainerId_FileId', Mandatory, Position = 1)] |
17 | | - [ValidateNotNullOrEmpty()] |
18 | | - [string][UrlEncodeTransformation()]$FileId, |
19 | | - |
20 | | - [Parameter(ParameterSetName = 'Container_File', Mandatory, Position = 1, ValueFromPipeline)] |
21 | | - [Parameter(ParameterSetName = 'ContainerId_File', Mandatory, Position = 1, ValueFromPipeline)] |
22 | | - [string]$File, |
| 11 | + [Parameter(Mandatory, Position = 1)] |
| 12 | + [Alias('FileId')] |
| 13 | + [object[]]$File, |
23 | 14 |
|
24 | 15 | [Parameter()] |
25 | 16 | [int]$TimeoutSec = 0, |
@@ -64,68 +55,87 @@ function Add-ContainerFile { |
64 | 55 | } |
65 | 56 |
|
66 | 57 | process { |
67 | | - # Get ids |
68 | | - if ($PSCmdlet.ParameterSetName -like 'Container_*') { |
69 | | - $ContainerId = $Container.id |
70 | | - } |
71 | | - if (-not $ContainerId) { |
72 | | - Write-Error -Exception ([System.ArgumentException]::new('Could not retrieve container id.')) |
73 | | - return |
74 | | - } |
75 | | - |
76 | | - #region Construct parameters for API request |
77 | 58 | $QueryUri = $OpenAIParameter.Uri.ToString() -f $ContainerId |
78 | | - $PostBody = [System.Collections.Specialized.OrderedDictionary]::new() |
79 | | - |
80 | | - if ($PSCmdlet.ParameterSetName -like '_FileId') { |
81 | | - $PostBody.file_id = $FileId |
82 | | - $OpenAIParameter.ContentType = 'application/json' |
83 | | - } |
84 | | - elseif (Test-Path -LiteralPath $File -PathType Leaf) { |
85 | | - $PostBody.file = Resolve-FileInfo $File |
86 | | - $OpenAIParameter.ContentType = 'multipart/form-data' |
87 | | - } |
88 | | - else { |
89 | | - $PostBody.file_id = [string]$File |
90 | | - $OpenAIParameter.ContentType = 'application/json' |
91 | | - } |
92 | | - #endregion |
93 | | - |
94 | | - #region Send API Request |
95 | | - $params = @{ |
96 | | - Method = 'Post' |
97 | | - Uri = $QueryUri |
98 | | - ContentType = $OpenAIParameter.ContentType |
99 | | - TimeoutSec = $OpenAIParameter.TimeoutSec |
100 | | - MaxRetryCount = $OpenAIParameter.MaxRetryCount |
101 | | - ApiKey = $OpenAIParameter.ApiKey |
102 | | - AuthType = $OpenAIParameter.AuthType |
103 | | - Organization = $OpenAIParameter.Organization |
104 | | - Body = $PostBody |
105 | | - AdditionalQuery = $AdditionalQuery |
106 | | - AdditionalHeaders = $AdditionalHeaders |
107 | | - AdditionalBody = $AdditionalBody |
108 | | - } |
109 | | - $Response = Invoke-OpenAIAPIRequest @params |
110 | 59 |
|
111 | | - # error check |
112 | | - if ($null -eq $Response) { |
113 | | - return |
| 60 | + # Create cancellation token for timeout |
| 61 | + $Cancellation = [System.Threading.CancellationTokenSource]::new() |
| 62 | + if ($TimeoutSec -gt 0) { |
| 63 | + $Cancellation.CancelAfter([timespan]::FromSeconds($TimeoutSec)) |
114 | 64 | } |
115 | | - #endregion |
116 | 65 |
|
117 | | - #region Parse response object |
| 66 | + # Loop through each file to be added |
118 | 67 | try { |
119 | | - $Response = $Response | ConvertFrom-Json -ErrorAction Stop |
| 68 | + foreach ($_file in $File) { |
| 69 | + #region Construct parameters for API request |
| 70 | + $PostBody = [System.Collections.Specialized.OrderedDictionary]::new() |
| 71 | + |
| 72 | + if ($_file -is [System.IO.FileInfo]) { |
| 73 | + $PostBody.file = $_file |
| 74 | + $OpenAIParameter.ContentType = 'multipart/form-data' |
| 75 | + } |
| 76 | + elseif (Test-Path -LiteralPath $_file -PathType Leaf) { |
| 77 | + $PostBody.file = Resolve-FileInfo $_file |
| 78 | + $OpenAIParameter.ContentType = 'multipart/form-data' |
| 79 | + } |
| 80 | + else { |
| 81 | + $PostBody.file_id = [string]$_file |
| 82 | + $OpenAIParameter.ContentType = 'application/json' |
| 83 | + } |
| 84 | + #endregion |
| 85 | + |
| 86 | + #region Send API Request |
| 87 | + $params = @{ |
| 88 | + Method = 'Post' |
| 89 | + Uri = $QueryUri |
| 90 | + ContentType = $OpenAIParameter.ContentType |
| 91 | + TimeoutSec = $OpenAIParameter.TimeoutSec |
| 92 | + MaxRetryCount = $OpenAIParameter.MaxRetryCount |
| 93 | + ApiKey = $OpenAIParameter.ApiKey |
| 94 | + AuthType = $OpenAIParameter.AuthType |
| 95 | + Organization = $OpenAIParameter.Organization |
| 96 | + Body = $PostBody |
| 97 | + AdditionalQuery = $AdditionalQuery |
| 98 | + AdditionalHeaders = $AdditionalHeaders |
| 99 | + AdditionalBody = $AdditionalBody |
| 100 | + } |
| 101 | + $Response = Invoke-OpenAIAPIRequest @params |
| 102 | + |
| 103 | + # error check |
| 104 | + if ($null -eq $Response) { |
| 105 | + return |
| 106 | + } |
| 107 | + #endregion |
| 108 | + |
| 109 | + #region Parse response object |
| 110 | + try { |
| 111 | + $Response = $Response | ConvertFrom-Json -ErrorAction Stop |
| 112 | + } |
| 113 | + catch { |
| 114 | + Write-Error -Exception $_.Exception |
| 115 | + } |
| 116 | + #endregion |
| 117 | + |
| 118 | + #region Output |
| 119 | + ParseContainerFileObject -InputObject $Response |
| 120 | + #endregion |
| 121 | + |
| 122 | + # Check cancellation |
| 123 | + $Cancellation.Token.ThrowIfCancellationRequested() |
| 124 | + } |
| 125 | + } |
| 126 | + catch [OperationCanceledException] { |
| 127 | + Write-TimeoutError |
| 128 | + return |
120 | 129 | } |
121 | 130 | catch { |
122 | 131 | Write-Error -Exception $_.Exception |
| 132 | + return |
| 133 | + } |
| 134 | + finally { |
| 135 | + if ($null -ne $Cancellation) { |
| 136 | + $Cancellation.Dispose() |
| 137 | + } |
123 | 138 | } |
124 | | - #endregion |
125 | | - |
126 | | - #region Output |
127 | | - ParseContainerFileObject -InputObject $Response |
128 | | - #endregion |
129 | 139 | } |
130 | 140 |
|
131 | 141 | end { |
|
0 commit comments