add default headers support (#5746)

This commit is contained in:
William Cheng
2020-03-30 12:15:27 +08:00
committed by GitHub
parent 01f02f6c57
commit 861fcce578
6 changed files with 119 additions and 2 deletions
@@ -59,6 +59,12 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
$HeaderParameters['Content-Type'] = $ContentType
}
# add default headers if any
foreach ($header in $Configuration["DefaultHeaders"].GetEnumerator()) {
$HeaderParameters[$header.Name] = $header.Value
}
# constrcut URL query string
$HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Parameter in $QueryParameters.GetEnumerator()) {
@@ -33,6 +33,10 @@ function Get-{{apiNamePrefix}}Configuration {
$Configuration["Cookie"] = $null
}
if (!$Configuration["DefaultHeaders"]) {
$Configuration["DefaultHeaders"] = @{}
}
if (!$Configuration["ApiKey"]) {
$Configuration["ApiKey"] = @{}
}
@@ -82,6 +86,9 @@ Access token for authentication/authorization
.PARAMETER SkipCertificateCheck
Skip certificate verification
.PARAMETER DefaultHeaders
Default HTTP headers to be included in the HTTP request
.OUTPUTS
System.Collections.Hashtable
@@ -103,6 +110,7 @@ function Set-{{{apiNamePrefix}}}Configuration {
[string]$AccessToken,
[switch]$PassThru,
[bool]$SkipCertificateCheck,
[hashtable]$DefaultHeaders,
[switch]$Force
)
@@ -139,6 +147,10 @@ function Set-{{{apiNamePrefix}}}Configuration {
If ($SkipCertificateCheck) {
$Script:Configuration['SkipCertificateCheck'] = $SkipCertificateCheck
}
If ($DefaultHeaders) {
$Script:Configuration['DefaultHeaders'] = $DefaultHeaders
}
}
}
@@ -210,6 +222,41 @@ function Set-{{{apiNamePrefix}}}ConfigurationApiKeyPrefix {
}
}
<#
.SYNOPSIS
Set the default header.
.DESCRIPTION
Set the default header.
.PARAMETER Key
Key of the HTTP header
.PARAMETER Value
Value of the HTTP header
.OUTPUTS
None
#>
function Set-{{{apiNamePrefix}}}ConfigurationDefaultHeader {
[CmdletBinding()]
Param(
[string]$Key,
[AllowEmptyString()]
[string]$Value
)
Process {
if (!$Script:Configuration["DefaultHeaders"]) {
$Script:Configuration["DefaultHeaders"] = @{}
}
$Script:Configuration["DefaultHeaders"][$Key] = $Value
}
}
<#
.SYNOPSIS
@@ -39,6 +39,10 @@ function Get-PSConfiguration {
$Configuration["Cookie"] = $null
}
if (!$Configuration["DefaultHeaders"]) {
$Configuration["DefaultHeaders"] = @{}
}
if (!$Configuration["ApiKey"]) {
$Configuration["ApiKey"] = @{}
}
@@ -88,6 +92,9 @@ Access token for authentication/authorization
.PARAMETER SkipCertificateCheck
Skip certificate verification
.PARAMETER DefaultHeaders
Default HTTP headers to be included in the HTTP request
.OUTPUTS
System.Collections.Hashtable
@@ -109,6 +116,7 @@ function Set-PSConfiguration {
[string]$AccessToken,
[switch]$PassThru,
[bool]$SkipCertificateCheck,
[hashtable]$DefaultHeaders,
[switch]$Force
)
@@ -145,6 +153,10 @@ function Set-PSConfiguration {
If ($SkipCertificateCheck) {
$Script:Configuration['SkipCertificateCheck'] = $SkipCertificateCheck
}
If ($DefaultHeaders) {
$Script:Configuration['DefaultHeaders'] = $DefaultHeaders
}
}
}
@@ -216,6 +228,41 @@ function Set-PSConfigurationApiKeyPrefix {
}
}
<#
.SYNOPSIS
Set the default header.
.DESCRIPTION
Set the default header.
.PARAMETER Key
Key of the HTTP header
.PARAMETER Value
Value of the HTTP header
.OUTPUTS
None
#>
function Set-PSConfigurationDefaultHeader {
[CmdletBinding()]
Param(
[string]$Key,
[AllowEmptyString()]
[string]$Value
)
Process {
if (!$Script:Configuration["DefaultHeaders"]) {
$Script:Configuration["DefaultHeaders"] = @{}
}
$Script:Configuration["DefaultHeaders"][$Key] = $Value
}
}
<#
.SYNOPSIS
@@ -3,7 +3,7 @@
#
# Generated by: OpenAPI Generator Team
#
# Generated on: 3/29/20
# Generated on: 3/30/20
#
@{
@@ -80,7 +80,8 @@ FunctionsToExport = 'Add-PSPet', 'Remove-Pet', 'Find-PSPetsByStatus', 'Find-PSPe
'New-PSInlineObject', 'New-PSInlineObject1', 'New-PSOrder', 'New-PSPet',
'New-PSTag', 'New-PSUser', 'Get-PSConfiguration', 'Set-PSConfiguration',
'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix',
'Get-PSHostSettings', 'Get-PSUrlFromHostSettings'
'Set-PSConfigurationDefaultHeader', 'Get-PSHostSettings',
'Get-PSUrlFromHostSettings'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
@@ -65,6 +65,12 @@ function Invoke-PSApiClient {
$HeaderParameters['Content-Type'] = $ContentType
}
# add default headers if any
foreach ($header in $Configuration["DefaultHeaders"].GetEnumerator()) {
$HeaderParameters[$header.Name] = $header.Value
}
# constrcut URL query string
$HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Parameter in $QueryParameters.GetEnumerator()) {
@@ -84,5 +84,15 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
#Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080"
}
It "Default header tests" {
Set-PSConfigurationDefaultHeader -Key "TestKey" -Value "TestValue"
$Configuration = Get-PSConfiguration
$Configuration["DefaultHeaders"].Count | Should Be 1
$Configuration["DefaultHeaders"]["TestKey"] | Should Be "TestValue"
}
}
}