Skip to content

Commit

Permalink
Merge pull request #47 from craysiii/custom-headers
Browse files Browse the repository at this point in the history
Add new methods to allow custom headers in each Hudu API Request
  • Loading branch information
lwhitelock authored Aug 19, 2023
2 parents 1d48caf + b501229 commit 77ad50c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions HuduAPI/HuduAPI.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
'New-HuduAssetLayout',
'New-HuduBaseURL',
'New-HuduCompany',
'New-HuduCustomHeaders',
'New-HuduFolder',
'New-HuduPassword',
'New-HuduPublicPhoto',
Expand All @@ -112,6 +113,7 @@
'Remove-HuduAsset',
'Remove-HuduBaseURL',
'Remove-HuduCompany',
'Remove-HuduCustomHeaders',
'Remove-HuduMagicDash',
'Remove-HuduPassword',
'Remove-HuduPassword',
Expand Down
7 changes: 7 additions & 0 deletions HuduAPI/Private/Invoke-HuduRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ function Invoke-HuduRequest {
'x-api-key' = (New-Object PSCredential 'user', $HuduAPIKey).GetNetworkCredential().Password;
}

if (($Script:Int_HuduCustomHeaders | Measure-Object).count -gt 0){

foreach($Entry in $Int_HuduCustomHeaders.GetEnumerator()) {
$Headers[$Entry.Name] = $Entry.Value
}
}

$ContentType = 'application/json; charset=utf-8'

$Uri = '{0}{1}' -f $HuduBaseURL, $Resource
Expand Down
34 changes: 34 additions & 0 deletions HuduAPI/Public/New-HuduCustomHeaders.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function New-HuduCustomHeaders {
<#
.SYNOPSIS
Set Hudu custom headers to be injected into each request
.DESCRIPTION
There may be times when one might need to use custom headers e.g. Service Tokens for Cloudflare Zero Trust
.PARAMETER Headers
Hashtable with the Custom Headers that need to be injected into each request
.EXAMPLE
New-HuduCustomHeaders -Headers @{"CF-Access-Client-Id" = "x"; "CF-Access-Client-Secret" = "y"}
.NOTES
General notes
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Scope = 'Function')]
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[hashtable]
$Headers
)
process {
if ($Headers.Count -eq 0) {
Write-Host "Empty Custom Header hashtable was provided, no Custom Headers will be set"
return 0
}

Set-Variable -Name 'Int_HuduCustomHeaders' -Value $Headers -Visibility Private -Scope script -Force
}
}
18 changes: 18 additions & 0 deletions HuduAPI/Public/Remove-HuduCustomHeaders.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function Remove-HuduCustomHeaders {
<#
.SYNOPSIS
Remove Custom Headers that are injected into each request
.DESCRIPTION
Unsets the Hudu Custom Header variable
.EXAMPLE
Remove-HuduCustomHeaders
#>
[CmdletBinding(SupportsShouldProcess)]
Param()
if ($PSCmdlet.ShouldProcess('Custom Headers')) {
Remove-Variable -Name 'Int_HuduCustomHeaders' -Scope script -Force
}
}

0 comments on commit 77ad50c

Please sign in to comment.