[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:
William Cheng
2020-10-19 09:30:23 +08:00
committed by GitHub
parent ee1cbf6f4b
commit 30fad9defa
5 changed files with 137 additions and 28 deletions

View File

@@ -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 @{

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 @{

View File

@@ -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" {