From d52cb33ec90f0761552e1412511d24bd26e9ad38 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Sun, 25 Oct 2026 14:50:50 -0500 Subject: [PATCH] refactor: share request/session boilerplate via Invoke-PiHoleApiRequest SonarCloud's develop quality gate failed on 5.1% duplicated new code: the login / build request / call / logout block in Get-PiHoleApiEndpoint and Get-PiHoleAuthApp is copied from about 17 other functions. Move it into a private Invoke-PiHoleApiRequest helper and use it from both new functions. Errors still propagate to each caller's own catch, so messages are unchanged apart from the friendly-message conversion now applying to the final 401. Co-Authored-By: Claude Opus 5.5 --- .../Private/Invoke-PiHoleApiRequest.ps1 | 38 +++++++++++++++++++ .../Authentication/Get-PiHoleAuthApp.ps1 | 18 +-------- .../FTLInformation/Get-PiHoleApiEndpoint.ps1 | 18 +-------- 3 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 PiHoleShell/Private/Invoke-PiHoleApiRequest.ps1 diff --git a/PiHoleShell/Private/Invoke-PiHoleApiRequest.ps1 b/PiHoleShell/Private/Invoke-PiHoleApiRequest.ps1 new file mode 100644 index 0000000..48e6b17 --- /dev/null +++ b/PiHoleShell/Private/Invoke-PiHoleApiRequest.ps1 @@ -0,0 +1,38 @@ +function Invoke-PiHoleApiRequest { + #INTERNAL FUNCTION + # + # Logs in, calls one API endpoint and always logs out again - the request/session boilerplate + # every public function otherwise repeats. Errors are left to propagate, so callers keep their + # own catch block (and its ConvertTo-PiHoleFriendlyErrorMessage call) unchanged. + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] + param ( + [Parameter(Mandatory = $true)] + [System.URI]$PiHoleServer, + [Parameter(Mandatory = $true)] + [string]$Password, + [Parameter(Mandatory = $true)] + [string]$Endpoint, + [string]$Method = "Get", + [bool]$IgnoreSsl = $false + ) + + try { + $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + + $Params = @{ + Headers = @{sid = $($Sid) } + Uri = "$($PiHoleServer.OriginalString)/api/$($Endpoint.TrimStart('/'))" + Method = $Method + SkipCertificateCheck = $IgnoreSsl + ContentType = "application/json" + } + + Invoke-RestMethod @Params -ErrorAction Stop + } + + finally { + if ($Sid) { + Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl + } + } +} diff --git a/PiHoleShell/Public/Authentication/Get-PiHoleAuthApp.ps1 b/PiHoleShell/Public/Authentication/Get-PiHoleAuthApp.ps1 index f931700..68fbc22 100644 --- a/PiHoleShell/Public/Authentication/Get-PiHoleAuthApp.ps1 +++ b/PiHoleShell/Public/Authentication/Get-PiHoleAuthApp.ps1 @@ -44,17 +44,7 @@ Generates a new app password and makes it the active one. From then on, use $App [bool]$RawOutput = $false ) try { - $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl - - $Params = @{ - Headers = @{sid = $($Sid) } - Uri = "$($PiHoleServer.OriginalString)/api/auth/app" - Method = "Get" - SkipCertificateCheck = $IgnoreSsl - ContentType = "application/json" - } - - $Response = Invoke-RestMethod @Params + $Response = Invoke-PiHoleApiRequest -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl -Endpoint 'auth/app' if ($RawOutput) { Write-Output $Response @@ -71,10 +61,4 @@ Generates a new app password and makes it the active one. From then on, use $App catch { Write-Error -Message (ConvertTo-PiHoleFriendlyErrorMessage -ErrorRecord $_) } - - finally { - if ($Sid) { - Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl - } - } } diff --git a/PiHoleShell/Public/FTLInformation/Get-PiHoleApiEndpoint.ps1 b/PiHoleShell/Public/FTLInformation/Get-PiHoleApiEndpoint.ps1 index c71c78d..d7f62e4 100644 --- a/PiHoleShell/Public/FTLInformation/Get-PiHoleApiEndpoint.ps1 +++ b/PiHoleShell/Public/FTLInformation/Get-PiHoleApiEndpoint.ps1 @@ -39,17 +39,7 @@ Lists only the endpoints that delete something. ) try { - $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl - - $Params = @{ - Headers = @{sid = $($Sid) } - Uri = "$($PiHoleServer.OriginalString)/api/endpoints" - Method = "Get" - SkipCertificateCheck = $IgnoreSsl - ContentType = "application/json" - } - - $Response = Invoke-RestMethod @Params + $Response = Invoke-PiHoleApiRequest -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl -Endpoint 'endpoints' if ($RawOutput) { Write-Output $Response @@ -71,10 +61,4 @@ Lists only the endpoints that delete something. catch { Write-Error -Message (ConvertTo-PiHoleFriendlyErrorMessage -ErrorRecord $_) } - - finally { - if ($Sid) { - Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl - } - } }