Skip to content

refactor: route API calls through Invoke-PiHoleApiRequest and add -WhatIf/-Confirm - #154

Merged
mikemadeja merged 5 commits into
developfrom
refactor/api-helper-and-shouldprocess
Nov 7, 2026
Merged

mikemadeja merged 5 commits into
developfrom
refactor/api-helper-and-shouldprocess

Conversation

@mikemadeja

@mikemadeja mikemadeja commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

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-PiHoleInfoClient and Get-PiHoleInfoLogin stay on Invoke-RestMethod because their endpoints work without logging in.

  • Removes the login/request/logout block from each file (about 1,000 lines).
  • Functions that look something up first (New-PiHoleGroup, Get-PiHoleDomain, Add-PiHoleList, ...) no longer hold an extra session open while the lookup logs in with its own.
  • A failed login (wrong password, unreachable server) now gives one error. Before, the request was still sent without a session, which added a second 401.
  • Every catch block uses ConvertTo-PiHoleFriendlyErrorMessage, so Pi-hole's own message and hint come through instead of just the HTTP status.
  • The helper passes Body, Form and OutFile through unchanged. Callers still serialize their own JSON, so the array-body workarounds and the query-string body in Search-PiHoleListDomain behave the same as before.

-WhatIf / -Confirm

SupportsShouldProcess is added to the 16 state-changing functions that lacked it, so all 27 now support it. The API call is inside the ShouldProcess check, so -WhatIf makes no network calls; Update-PiHoleActionsGravity used to log in first. ConfirmImpact stays at the default (Medium), so nothing prompts unless the caller asks. Add-PiHoleDnsRecord passes -Confirm:$false to Add-PiHoleConfigArrayItem so the user is asked once, the same as Remove-PiHoleDnsRecord.

CONTRIBUTING.md gets 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:

Helper Used by
Invoke-PiHoleAction Invoke-PiHoleFlushLogs, Invoke-PiHoleFlushNetwork, Restart-PiHoleDnsService
Get-PiHoleLogContent Get-PiHoleLogDnsmasq, Get-PiHoleLogFtl, Get-PiHoleLogWebserver
Get-PiHoleNetworkInfo Get-PiHoleNetworkGateway, Get-PiHoleNetworkInterface, Get-PiHoleNetworkRoute
Resolve-PiHoleGroupName 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

ShouldProcess uses 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 develop and 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

  • Every file parses. PSScriptAnalyzer is clean file by file. A recursive run crashes locally with a NullReferenceException, and it does the same on develop.
  • PiHoleShell.Module.Tests.ps1: 6/6 passed.
  • Live integration tests against the dev server for GroupManagement, Teleporter, Config, DnsControl and ListManagement, plus Get-PiHoleInfoVersion and Get-PiHoleQuery: 94/94 passed. These cover JSON bodies, batch deletes, form upload, file download, query strings, config writes, bad-password errors and -WhatIf.
  • Ran -WhatIf on 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

mikemadeja and others added 5 commits November 7, 2026 07:01
…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>
@sonarqubecloud

sonarqubecloud Bot commented Nov 7, 2026

Copy link
Copy Markdown

@mikemadeja
mikemadeja merged commit aa7f81a into develop Nov 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant