Skip to content

Commit

Permalink
[PS] Add proxy support (OpenAPITools#7739)
Browse files Browse the repository at this point in the history
* add proxy support to powershell

* update api_client

* add proxy tests

* fix check

* add more check

* update samples

* fix proxy setting

* update docstring
  • Loading branch information
wing328 authored and biplavk committed Oct 19, 2020
1 parent ee1cbf6 commit 83c72c9
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,47 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {

{{/hasHttpSignatureMethods}}
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 @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ function Get-{{apiNamePrefix}}Configuration {
$Configuration["SkipCertificateCheck"] = $false
}

if (!$Configuration.containsKey("Proxy")) {
$Configuration["Proxy"] = $null
}

Return $Configuration

}
Expand Down Expand Up @@ -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

Expand All @@ -113,6 +123,7 @@ function Set-{{{apiNamePrefix}}}Configuration {
[string]$AccessToken,
[switch]$SkipCertificateCheck,
[hashtable]$DefaultHeaders,
[System.Object]$Proxy,
[switch]$PassThru
)
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function Get-PSConfiguration {
$Configuration["SkipCertificateCheck"] = $false
}

if (!$Configuration.containsKey("Proxy")) {
$Configuration["Proxy"] = $null
}

Return $Configuration

}
Expand Down Expand Up @@ -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
Expand All @@ -119,6 +129,7 @@ function Set-PSConfiguration {
[string]$AccessToken,
[switch]$SkipCertificateCheck,
[hashtable]$DefaultHeaders,
[System.Object]$Proxy,
[switch]$PassThru
)

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @{
Expand Down
17 changes: 17 additions & 0 deletions samples/client/petstore/powershell/tests/Petstore.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 83c72c9

Please sign in to comment.