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:
@@ -114,6 +114,8 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
|
||||
|
||||
{{/hasHttpSignatureMethods}}
|
||||
if ($SkipCertificateCheck -eq $true) {
|
||||
if ($Configuration["Proxy"] -eq $null) {
|
||||
# skip certification check, no proxy
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
@@ -121,14 +123,38 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
|
||||
-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 {
|
||||
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 @{
|
||||
|
||||
@@ -49,6 +49,10 @@ function Get-{{apiNamePrefix}}Configuration {
|
||||
$Configuration["SkipCertificateCheck"] = $false
|
||||
}
|
||||
|
||||
if (!$Configuration.containsKey("Proxy")) {
|
||||
$Configuration["Proxy"] = $null
|
||||
}
|
||||
|
||||
Return $Configuration
|
||||
|
||||
}
|
||||
@@ -89,6 +93,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
|
||||
|
||||
@@ -113,6 +123,7 @@ function Set-{{{apiNamePrefix}}}Configuration {
|
||||
[string]$AccessToken,
|
||||
[switch]$SkipCertificateCheck,
|
||||
[hashtable]$DefaultHeaders,
|
||||
[System.Object]$Proxy,
|
||||
[switch]$PassThru
|
||||
)
|
||||
|
||||
@@ -161,6 +172,15 @@ function Set-{{{apiNamePrefix}}}Configuration {
|
||||
$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
|
||||
}
|
||||
|
||||
@@ -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,6 +101,8 @@ function Invoke-PSApiClient {
|
||||
}
|
||||
|
||||
if ($SkipCertificateCheck -eq $true) {
|
||||
if ($Configuration["Proxy"] -eq $null) {
|
||||
# skip certification check, no proxy
|
||||
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
|
||||
-Method $Method `
|
||||
-Headers $HeaderParameters `
|
||||
@@ -108,14 +110,38 @@ function Invoke-PSApiClient {
|
||||
-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 {
|
||||
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 @{
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
|
||||
Context 'Pet' {
|
||||
It 'CRUD tests' {
|
||||
# proxy test - use system default proxy setting
|
||||
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
|
||||
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
|
||||
Set-PSConfiguration -Proxy $proxy
|
||||
|
||||
$Id = 38369
|
||||
|
||||
# Add pet
|
||||
@@ -39,6 +44,9 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
|
||||
$Result."name" | Should -Be "PowerShell Update"
|
||||
$Result."status" | Should -Be "Pending"
|
||||
|
||||
# clear proxy setting
|
||||
Set-PSConfiguration -Proxy $null
|
||||
|
||||
# Update (put)
|
||||
$NewPet = Initialize-PSPet -Id $Id -Name 'PowerShell2' -Category (
|
||||
Initialize-PSCategory -Id $Id -Name 'PSCategory2'
|
||||
@@ -167,6 +175,15 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
|
||||
$Conf = Set-PSConfiguration -PassThru -SkipCertificateCheck
|
||||
$Conf["SkipCertificateCheck"] | Should -Be $true
|
||||
$Conf = Set-PSConfiguration -PassThru # reset SkipCertificateCheck
|
||||
|
||||
# proxy test
|
||||
$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
|
||||
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
|
||||
Set-PSConfiguration -Proxy $proxy
|
||||
|
||||
Set-PSConfiguration -Proxy $null
|
||||
$Conf2 = Get-PSConfiguration
|
||||
$Conf2["Proxy"] | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Base URL tests" {
|
||||
|
||||
Reference in New Issue
Block a user