Skip to content

refactor(data): extract shared guardedApiCall for API-backed repositories#31

Merged
netqo merged 1 commit into
devfrom
refactor/repo-api-call-guard
Jun 11, 2026
Merged

refactor(data): extract shared guardedApiCall for API-backed repositories#31
netqo merged 1 commit into
devfrom
refactor/repo-api-call-guard

Conversation

@netqo

@netqo netqo commented Jun 11, 2026

Copy link
Copy Markdown
Owner

Summary

NewsRepositoryImpl and GeminiAssistantRepository each repeated the same guard sequence around their remote call: short-circuit to a failed Result when the API key is blank (logging the reason), then wrap the call in runCatching and log any failure under the repo tag. This PR extracts that sequence into a single inline fun guardedApiCall(...) in data/ApiCall.kt and routes both repositories through it.

Rationale

The two repositories had drifted into the same boilerplate. Centralizing it removes the duplication (DRY) and gives any future API-backed repository the same fail-fast-on-blank-key plus logged-failure behavior without re-implementing it. The helper is inline so the block lambda can still call the repositories' own suspend functions.

Behavior is preserved: blank keys still fail without touching the network, and exceptions still collapse to Result.failure with a full stack trace in logcat. Only the blank-key log wording changed slightly.

Verification

  • ./gradlew ktlintCheck detekt testDebugUnitTest -> BUILD SUCCESSFUL
  • ./gradlew assembleDebug -> BUILD SUCCESSFUL

Test plan

  • ktlint + detekt clean
  • JVM unit tests green
  • Debug APK assembles

Checklist

  • Commit messages follow Conventional Commits (see CONTRIBUTING.md).
  • Branch name follows the naming convention (refactor/).
  • The change is scoped: no unrelated edits sneaked into this PR.
  • If user-visible behavior changed, CHANGELOG.md has been updated under [Unreleased]. (No user-visible change.)
  • If a new dependency was added, it is justified in the PR description. (No new dependency.)

…ries

NewsRepositoryImpl and GeminiAssistantRepository duplicated the same
guard sequence: short-circuit on a blank credential, then runCatching
the remote call and log failures under the repo tag. Pull it into one
inline `guardedApiCall` helper in `data/ApiCall.kt` so both repositories
share a single implementation and any future API-backed repository
inherits the same behavior.

Behavior is unchanged: blank keys still fail fast without a network call,
and exceptions still collapse to Result.failure with a logged stack
trace.
@netqo netqo merged commit 74105c6 into dev Jun 11, 2026
3 checks passed
@netqo netqo deleted the refactor/repo-api-call-guard branch June 11, 2026 04:16
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