forked from loafle/openapi-generator-original
[PS] Add proxy support (#7739)
* add proxy support to powershell * update api_client * add proxy tests * fix check * add more check * update samples * fix proxy setting * update docstring
This commit is contained in:
@@ -55,6 +55,10 @@ function Get-PSConfiguration {
|
||||
$Configuration["SkipCertificateCheck"] = $false
|
||||
}
|
||||
|
||||
if (!$Configuration.containsKey("Proxy")) {
|
||||
$Configuration["Proxy"] = $null
|
||||
}
|
||||
|
||||
Return $Configuration
|
||||
|
||||
}
|
||||
@@ -95,6 +99,12 @@ Skip certificate verification
|
||||
.PARAMETER DefaultHeaders
|
||||
Default HTTP headers to be included in the HTTP request
|
||||
|
||||
.PARAMETER Proxy
|
||||
Proxy setting in the HTTP request, e.g.
|
||||
|
||||
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
|
||||
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
|
||||
|
||||
.PARAMETER PassThru
|
||||
Return an object of the Configuration
|
||||
|
||||
@@ -119,6 +129,7 @@ function Set-PSConfiguration {
|
||||
[string]$AccessToken,
|
||||
[switch]$SkipCertificateCheck,
|
||||
[hashtable]$DefaultHeaders,
|
||||
[System.Object]$Proxy,
|
||||
[switch]$PassThru
|
||||
)
|
||||
|
||||
@@ -167,6 +178,15 @@ function Set-PSConfiguration {
|
||||
$Script:Configuration['DefaultHeaders'] = $DefaultHeaders
|
||||
}
|
||||
|
||||
If ($Proxy -ne $null) {
|
||||
If ($Proxy.GetType().FullName -ne "System.Net.SystemWebProxy" -and $Proxy.GetType().FullName -ne "System.Net.WebRequest+WebProxyWrapperOpaque") {
|
||||
throw "Incorrect Proxy type '$($Proxy.GetType().FullName)'. Must be System.Net.SystemWebProxy or System.Net.WebRequest+WebProxyWrapperOpaque."
|
||||
}
|
||||
$Script:Configuration['Proxy'] = $Proxy
|
||||
} else {
|
||||
$Script:Configuration['Proxy'] = $null
|
||||
}
|
||||
|
||||
If ($PassThru.IsPresent) {
|
||||
$Script:Configuration
|
||||
}
|
||||
|
||||
@@ -101,21 +101,47 @@ function Invoke-PSApiClient {
|
||||
}
|
||||
|
||||
if ($SkipCertificateCheck -eq $true) {
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
-Body $RequestBody `
|
||||
-ErrorAction Stop `
|
||||
-UseBasicParsing `
|
||||
-SkipCertificateCheck
|
||||
|
||||
if ($Configuration["Proxy"] -eq $null) {
|
||||
# skip certification check, no proxy
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
-Body $RequestBody `
|
||||
-ErrorAction Stop `
|
||||
-UseBasicParsing `
|
||||
-SkipCertificateCheck
|
||||
} else {
|
||||
# skip certification check, use proxy
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
-Body $RequestBody `
|
||||
-ErrorAction Stop `
|
||||
-UseBasicParsing `
|
||||
-SkipCertificateCheck `
|
||||
-Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) `
|
||||
-ProxyUseDefaultCredentials
|
||||
}
|
||||
} else {
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
-Body $RequestBody `
|
||||
-ErrorAction Stop `
|
||||
-UseBasicParsing
|
||||
if ($Configuration["Proxy"] -eq $null) {
|
||||
# perform certification check, no proxy
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
-Body $RequestBody `
|
||||
-ErrorAction Stop `
|
||||
-UseBasicParsing
|
||||
} else {
|
||||
# perform certification check, use proxy
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
-Body $RequestBody `
|
||||
-ErrorAction Stop `
|
||||
-UseBasicParsing `
|
||||
-Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) `
|
||||
-ProxyUseDefaultCredentials
|
||||
}
|
||||
}
|
||||
|
||||
return @{
|
||||
|
||||
Reference in New Issue
Block a user