[powershell] add file upload support (#10735)

* add file upload support

* comment out c# tests

* Revert "comment out c# tests"

This reverts commit 8f500908fb6366be548e08f437693ec0aa5199d3.
This commit is contained in:
William Cheng 2021-10-30 14:42:11 +08:00 committed by GitHub
parent 912d993955
commit 4e21b61800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 6 deletions

View File

@ -56,9 +56,15 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
$HeaderParameters['Accept'] = $Accept $HeaderParameters['Accept'] = $Accept
} }
[string]$MultiPartBoundary = $null
$ContentType= SelectHeaders -Headers $ContentTypes $ContentType= SelectHeaders -Headers $ContentTypes
if ($ContentType) { if ($ContentType) {
$HeaderParameters['Content-Type'] = $ContentType $HeaderParameters['Content-Type'] = $ContentType
if ($ContentType -eq 'multipart/form-data') {
[string]$MultiPartBoundary = [System.Guid]::NewGuid()
$MultiPartBoundary = "---------------------------$MultiPartBoundary"
$HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary"
}
} }
# add default headers if any # add default headers if any
@ -66,7 +72,6 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
$HeaderParameters[$header.Name] = $header.Value $HeaderParameters[$header.Name] = $header.Value
} }
# construct URL query string # construct URL query string
$HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) $HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Parameter in $QueryParameters.GetEnumerator()) { foreach ($Parameter in $QueryParameters.GetEnumerator()) {
@ -84,8 +89,33 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
# include form parameters in the request body # include form parameters in the request body
if ($FormParameters -and $FormParameters.Count -gt 0) { if ($FormParameters -and $FormParameters.Count -gt 0) {
if (![string]::IsNullOrEmpty($MultiPartBoundary)) {
$RequestBody = ""
$LF = "`r`n"
$FormParameters.Keys | ForEach-Object {
$value = $FormParameters[$_]
$isFile = $value.GetType().FullName -eq "System.IO.FileInfo"
$RequestBody += "--$MultiPartBoundary$LF"
$RequestBody += "Content-Disposition: form-data; name=`"$_`""
if ($isFile) {
$fileName = $value.Name
$RequestBody += "; filename=`"$fileName`"$LF"
$RequestBody += "Content-Type: application/octet-stream$LF$LF"
$RequestBody += Get-Content -Path $value.FullName
} else {
$RequestBody += "$LF$LF"
$RequestBody += ([string]$value)
}
$RequestBody += "$LF--$MultiPartBoundary"
}
$RequestBody += "--"
} else {
$RequestBody = $FormParameters $RequestBody = $FormParameters
} }
}
if ($Body -or $IsBodyNullable) { if ($Body -or $IsBodyNullable) {
$RequestBody = $Body $RequestBody = $Body

View File

@ -3,7 +3,7 @@
# #
# Generated by: OpenAPI Generator Team # Generated by: OpenAPI Generator Team
# #
# Generated on: 30-11-2020 # Generated on: 10/30/2021
# #
@{ @{
@ -45,7 +45,7 @@ PowerShellVersion = '5.0'
# DotNetFrameworkVersion = '' # DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = '' # ClrVersion = ''
# Processor architecture (None, X86, Amd64) required by this module # Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = '' # ProcessorArchitecture = ''
@ -128,6 +128,15 @@ PrivateData = @{
# ReleaseNotes of this module # ReleaseNotes of this module
ReleaseNotes = 'This is a sample project' ReleaseNotes = 'This is a sample project'
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable } # End of PSData hashtable
} # End of PrivateData hashtable } # End of PrivateData hashtable

View File

@ -62,9 +62,15 @@ function Invoke-PSApiClient {
$HeaderParameters['Accept'] = $Accept $HeaderParameters['Accept'] = $Accept
} }
[string]$MultiPartBoundary = $null
$ContentType= SelectHeaders -Headers $ContentTypes $ContentType= SelectHeaders -Headers $ContentTypes
if ($ContentType) { if ($ContentType) {
$HeaderParameters['Content-Type'] = $ContentType $HeaderParameters['Content-Type'] = $ContentType
if ($ContentType -eq 'multipart/form-data') {
[string]$MultiPartBoundary = [System.Guid]::NewGuid()
$MultiPartBoundary = "---------------------------$MultiPartBoundary"
$HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary"
}
} }
# add default headers if any # add default headers if any
@ -72,7 +78,6 @@ function Invoke-PSApiClient {
$HeaderParameters[$header.Name] = $header.Value $HeaderParameters[$header.Name] = $header.Value
} }
# construct URL query string # construct URL query string
$HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) $HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Parameter in $QueryParameters.GetEnumerator()) { foreach ($Parameter in $QueryParameters.GetEnumerator()) {
@ -90,8 +95,33 @@ function Invoke-PSApiClient {
# include form parameters in the request body # include form parameters in the request body
if ($FormParameters -and $FormParameters.Count -gt 0) { if ($FormParameters -and $FormParameters.Count -gt 0) {
if (![string]::IsNullOrEmpty($MultiPartBoundary)) {
$RequestBody = ""
$LF = "`r`n"
$FormParameters.Keys | ForEach-Object {
$value = $FormParameters[$_]
$isFile = $value.GetType().FullName -eq "System.IO.FileInfo"
$RequestBody += "--$MultiPartBoundary$LF"
$RequestBody += "Content-Disposition: form-data; name=`"$_`""
if ($isFile) {
$fileName = $value.Name
$RequestBody += "; filename=`"$fileName`"$LF"
$RequestBody += "Content-Type: application/octet-stream$LF$LF"
$RequestBody += Get-Content -Path $value.FullName
} else {
$RequestBody += "$LF$LF"
$RequestBody += ([string]$value)
}
$RequestBody += "$LF--$MultiPartBoundary"
}
$RequestBody += "--"
} else {
$RequestBody = $FormParameters $RequestBody = $FormParameters
} }
}
if ($Body -or $IsBodyNullable) { if ($Body -or $IsBodyNullable) {
$RequestBody = $Body $RequestBody = $Body