Skip to content

kmpbits/KtorAdvancedDemo

Repository files navigation

Ktor Advanced Demo: The Pit Crew

A Kotlin Multiplatform (KMP) showcase focusing on industrial-grade HttpClient configuration. This project demonstrates how to offload common networking concerns—authentication, retries, logging, and serialization—to Ktor's powerful plugin architecture.

"One client, configured once, shared as a singleton — the rest of the code never has to think about auth headers, retry logic, or JSON parsing. That's the pit crew doing its job."


🛠️ The "Pit Crew" (Key Features)

This demo centralizes network logic in HttpClientFactory.kt, implementing a robust pipeline of plugins:

  1. 📋 Dynamic Logging: Uses LogLevel.ALL in debug for full visibility into request/response bodies, and LogLevel.NONE in production for zero overhead.
  2. 🔄 Content Negotiation: Powered by kotlinx.serialization with lenient JSON parsing to survive minor backend inconsistencies and future-proof against new fields.
  3. 🔐 Transparent Auth (Bearer):
    • Auto-loading: Attaches tokens automatically to outgoing requests.
    • Auto-refresh: Intercepts 401 Unauthorized responses, refreshes tokens via TokenStorage, and retries the original request without any manual intervention in repositories.
    • Exclusion logic: Smartly skips authentication for specific endpoints (e.g., login/register).
  4. 🌐 Centralized Defaults: Sets the base URL (JSONPlaceholder) and default headers (Content-Type, Accept) globally.
  5. 🔁 Intelligent Retries:
    • Automatically retries safe (idempotent) methods (GET, PUT, DELETE).
    • Implements Exponential Back-off (2s → 4s → 8s) to handle transient server errors (5xx).
    • Intentionally excludes POST requests to prevent accidental duplicate resource creation.

🏗️ Project Structure

  • shared/commonMain:
    • network/HttpClientFactory.kt: The heart of the networking layer.
    • network/TokenStorage.kt: Contract for token management.
    • network/PostsRepository.kt: Clean, logic-free data fetching.
    • App: Compose Multiplatform UI demonstrating the network state.

🚀 Tech Stack

  • Kotlin Multiplatform (KMP)
  • Ktor 3.1.3: Core networking engine.
  • Compose Multiplatform: Shared UI layer.
  • Kotlinx Serialization: JSON processing.
  • Material 3: Modern UI components.

🏁 Getting Started

Prerequisites

  • Android Studio Ladybug (or newer)
  • Kotlin 2.1.0+
  • macOS (if running the iOS target)

Running the App

  1. Android: Select the androidApp configuration and hit Run.
  2. iOS: Open the iosApp folder in Xcode or run from Android Studio using the KMP plugin.

Experimenting

  • Toggle Debug Mode in the UI to see how Logging and prettyPrint behavior changes in the Logcat.
  • Check HttpClientFactory.kt to modify retry counts or base URLs.

This demo is part of an article series on advanced Kotlin Multiplatform networking. For a deep dive into the implementation details, check out the full post.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors