Repository navigation
refactor: route API calls through Invoke-PiHoleApiRequest and add -WhatIf/-Confirm - #154
Merged
Merged
Conversation
…uldProcess Move every authenticated public function onto the shared request helper (74 of 77; Get-PiHoleAuthStatus, Get-PiHoleInfoClient and Get-PiHoleInfoLogin call endpoints that work without logging in). This removes the login/logout boilerplate from each file and fixes a few behaviours along the way: - Functions that look something up first (New-PiHoleGroup, Get-PiHoleDomain, ...) no longer hold an extra session open while the lookup logs in with its own. - A failed login stops with one error, instead of also sending the request without a session and reporting a second 401. - All catch blocks use ConvertTo-PiHoleFriendlyErrorMessage, so Pi-hole's own error message and hint come through instead of just the HTTP status. The helper gains pass-through Body, Form and OutFile parameters so the teleporter upload/download and query-string Get bodies fit unchanged. Add SupportsShouldProcess to the 16 state-changing functions that lacked it, so all 27 now accept -WhatIf and -Confirm. The API call sits inside the ShouldProcess check, so -WhatIf no longer logs in (gravity used to). ConfirmImpact stays at the default Medium, so nothing prompts unless the caller asks. Add-PiHoleDnsRecord passes -Confirm:$false to Add-PiHoleConfigArrayItem, matching Remove-PiHoleDnsRecord. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Wrapping each function body in if ($PSCmdlet.ShouldProcess(...)) added a nesting level, pushing New-PiHoleClient, New-PiHoleDomain and Update-PiHoleGroup over SonarCloud's cognitive complexity limit of 15. Return early instead, which also restores the bodies' original indentation so unchanged lines stop showing up as new code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lpers SonarCloud's new-code duplication gate failed (19.8%, limit 3%) because several function families were already copies of each other, differing only in endpoint and property names. Move their shared bodies into private helpers: - Invoke-PiHoleAction: Invoke-PiHoleFlushLogs, Invoke-PiHoleFlushNetwork, Restart-PiHoleDnsService - Get-PiHoleLogContent: Get-PiHoleLogDnsmasq, Get-PiHoleLogFtl, Get-PiHoleLogWebserver - Get-PiHoleNetworkInfo: Get-PiHoleNetworkGateway, -Interface, -Route - Resolve-PiHoleGroupName: the group lookup in New-/Update-PiHoleClient, New-/Update-PiHoleDomain, Add-/Update-PiHoleList - ConvertTo-PiHoleClientObject, -DomainObject, -GroupObject: output of the client, domain and group New/Update functions Public names, parameters and output are unchanged; comparing develop and this branch against a fake API gave identical output and requests for all 20 scenarios tried. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The action wrappers have a single statement, so an if block reads better than an early return. It also keeps Invoke-PiHoleFlushNetwork and Restart-PiHoleDnsService under SonarCloud's duplicate-block size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Unit test with only Invoke-RestMethod mocked, so the real login and logout run. Checks that a call logs in, sends the request with that session and then logs out, in that order; that the session is still closed when the request fails; that a failed login sends nothing else; and that Body/Form are passed through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Items 2 and 3 from the 1.3 backlog.
Shared request helper
Every authenticated public function now calls the API through
Invoke-PiHoleApiRequest(74 of 77).Get-PiHoleAuthStatus,Get-PiHoleInfoClientandGet-PiHoleInfoLoginstay onInvoke-RestMethodbecause their endpoints work without logging in.New-PiHoleGroup,Get-PiHoleDomain,Add-PiHoleList, ...) no longer hold an extra session open while the lookup logs in with its own.ConvertTo-PiHoleFriendlyErrorMessage, so Pi-hole's own message and hint come through instead of just the HTTP status.Body,FormandOutFilethrough unchanged. Callers still serialize their own JSON, so the array-body workarounds and the query-string body inSearch-PiHoleListDomainbehave the same as before.-WhatIf / -Confirm
SupportsShouldProcessis added to the 16 state-changing functions that lacked it, so all 27 now support it. The API call is inside theShouldProcesscheck, so-WhatIfmakes no network calls;Update-PiHoleActionsGravityused to log in first.ConfirmImpactstays at the default (Medium), so nothing prompts unless the caller asks.Add-PiHoleDnsRecordpasses-Confirm:$falsetoAdd-PiHoleConfigArrayItemso the user is asked once, the same asRemove-PiHoleDnsRecord.CONTRIBUTING.mdgets a step describing this for new functions.Shared bodies for near-identical functions
SonarCloud's duplication gate went from 23.5% to 2.6%. The duplication wasn't new; several function families were already copies of each other and only differed in endpoint and property names. Their shared code now lives in private helpers:
Invoke-PiHoleActionInvoke-PiHoleFlushLogs,Invoke-PiHoleFlushNetwork,Restart-PiHoleDnsServiceGet-PiHoleLogContentGet-PiHoleLogDnsmasq,Get-PiHoleLogFtl,Get-PiHoleLogWebserverGet-PiHoleNetworkInfoGet-PiHoleNetworkGateway,Get-PiHoleNetworkInterface,Get-PiHoleNetworkRouteResolve-PiHoleGroupNameNew-/Update-PiHoleClient,New-/Update-PiHoleDomain,Add-/Update-PiHoleListConvertTo-PiHoleClientObject,-DomainObject,-GroupObjectShouldProcessuses a guard clause (if (-not ...) { return }) so function bodies keep their original indentation and complexity. This also cleared SonarCloud's three cognitive-complexity issues.No breaking changes. Public names, parameters and output are unchanged. I ran
developand this branch against a fake Pi-hole API in 20 scenarios: formatted and raw output,-NextID/-Detailed, a missing group, and every New/Update path. Output and API requests were identical in all 20.Testing
develop.PiHoleShell.Module.Tests.ps1: 6/6 passed.Get-PiHoleInfoVersionandGet-PiHoleQuery: 94/94 passed. These cover JSON bodies, batch deletes, form upload, file download, query strings, config writes, bad-password errors and-WhatIf.-WhatIfon 5 functions against an unreachable address: no network calls (35 ms in total).Private/changed, so CI runs the full suite (about an hour).🤖 Generated with Claude Code