network-layer-architect

Designs and refactors Android and KMP network layers using Retrofit, Ktor, and OkHttp.

Updated Jan 2, 2024
One-click install
npx skills add https://github.com/Mithrandir21/game-deals-app --skill network-layer-architect-mithrandir21
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: network-layer-architect
Source: https://github.com/Mithrandir21/game-deals-app/tree/main/.claude/skills/network-layer-architect
Command: npx skills add https://github.com/Mithrandir21/game-deals-app --skill network-layer-architect-mithrandir21

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building or untangling an Android/KMP networking layer involves many interlocking decisions — client choice, interceptor ordering, auth token refresh, retry policy, caching, and error mapping — and getting any of them wrong leaks HTTP details into ViewModels or causes production bugs like auth refresh deadlocks. ## Core Features & Use Cases - Client Selection & Setup: Chooses between Retrofit, Ktor, or OkHttp based on platform (Android-only vs KMP) and configures timeouts, converters, and plugins. - Interceptor & Auth Stack: Defines interceptor ordering, implements 401 token refresh with single-flight mutex protection, and adds logging and telemetry safely. - Retry, Caching & Error Handling: Implements exponential backoff with jitter, sealed result types for error mapping, and offline-first caching with Room as source of truth. - Use Case: When starting a greenfield Android module that calls a REST API, use this Skill to produce a complete layer design — HttpClient → ApiService → Repository → ViewModel — with MockWebServer-based tests. ## Quick Start Ask the assistant to design a network layer for your Android app using Retrofit with auth token refresh, retry logic, and offline-first caching.

Frequently Asked Questions about network-layer-architect

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up a Retrofit network layer on Android?▼

Configure an OkHttpClient with connect, read, and call timeouts plus auth and logging interceptors, then build Retrofit with a base URL and Moshi converter. Wrap calls in an ApiService consumed by a Repository that returns domain types, never raw Response objects.

Retrofit vs Ktor client: which should I use for Android?▼

Use Retrofit with OkHttp for Android-only apps with REST/JSON and a mature ecosystem. Choose Ktor Client for Kotlin Multiplatform projects sharing code with iOS, or when you need heavy WebSocket and streaming support.

How do I handle 401 errors and refresh auth tokens in OkHttp?▼

Implement an auth interceptor that adds the Authorization header, and on a 401 response closes it, refreshes the token, and retries the request once. Use a mutex for single-flight refresh so concurrent 401s do not trigger duplicate refreshes, and never refresh on the refresh endpoint itself.

Which HTTP errors should be retried with backoff?▼

Retry only network IOExceptions, 5xx server errors, and transient codes like 429 with Retry-After or 503. Use exponential backoff with jitter, such as 200ms doubling per attempt plus random jitter, and never retry 4xx client errors.

How do I test an Android network layer without a real server?▼

Use MockWebServer for OkHttp/Retrofit or MockEngine for Ktor to unit test the layer. Explicitly test auth refresh paths and error mapping, and reset MockWebServer in an @After method so tests do not pollute each other.

Why should DTOs not leak into ViewModels?▼

Leaking DTOs couples the UI to the API shape, so every API change breaks the UI. Repositories should translate HTTP responses into domain types or sealed result types, keeping Response, Call, and HttpException invisible to ViewModels.