Skip to content

kingg22/ktorgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

713 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ ktorgen β€” Kotlin + KSP + Ktor Client Code Generator

Kotlin Multiplatform Ktor Client

Maven Central Version Maven Central Last Update

GitHub License GitHub last commit (branch) Quality Gate Status Coverage

Ktorgen is a 100% compile-time code generator for creating HTTP clients using Ktor Client and interface annotations, inspired by Retrofit and Ktorfit.

πŸ“Œ Features

πŸ”Ή No runtime dependencies β€” you only need the annotations and the compiler in your build.

πŸ”Ή 100% compatible with Kotlin, Kotlin Multiplatform, KSP 2, and Ktor Client.

πŸ”Ή 100% Ktor configuration β€” no unnecessary overhead or wrappers added.

πŸ”Ή Annotation retention: SOURCE, BINARY (only for RequiresOptIn).

πŸ”Ή Generated code annotated with @Generated and @Suppress to avoid warnings and exclude of analysis tools.

πŸ”Ή Support suspend fun, Coroutines Flow and Result out of the box.

πŸ”Ή Optional type-safe headers, using Headers.ContentTypes or Headers.Companion.*

πŸ”Ή Optional inheritance between interfaces (use delegation to implement).

πŸ”Ή Experimental support for vararg parameters and Pair type.

πŸ“š See more in documentation page.

πŸ“¦ InstalaciΓ³n

Maven Central Version

Install KSP plugin

Install Ktor Client Core and an Engine

  • Version Catalog
[versions]
ktorgen = "<current-version>"

[libraries]
ktorgen-annotations = { group = "io.github.kingg22", name = "ktorgen-annotations", version.ref = "ktorgen" }
ktorgen-compiler = { group = "io.github.kingg22", name = "ktorgen-compiler", version.ref = "ktorgen" }
  • Gradle for Kotlin JVM (KMP projects see below)
dependencies {
  implementation("io.github.kingg22:ktorgen-annotations:<current-version>")
  ksp("io.github.kingg22:ktorgen-compiler:<current-version>")
}

πŸ”„ Comparison

Feature Retrofit Ktorfit ktorgen πŸš€
Based on Ktor Client ❌ βœ… βœ…
Runtime dependencies βœ… (Reflection, converters, adapters) βœ… (Converts) ❌
Retrofit-like annotations βœ… (Runtime retention) βœ… + ⚠️ more annotations βœ… with smart use that reduces unnecessary annotations
Type-safe headers ❌ ❌ βœ… with Headers.ContentTypes and Headers.Companion.*
Cookie support ⚠️ using the Header annotation ⚠️ Using the Header annotation βœ… with @Cookie
Using an @Part without @Multipart ❌ ❌ βœ… (smart use, your intended use is understood)
Customizing the generated code Limited Limited βœ… Using options in @KtorGen and @KtorGenFunction
Synchronous request βœ… βœ… ❌ Ktor Client don't offer synchronous request, only for Flow

BREAKING CHANGES:

  • Since PR #1, Header annotations have been different:

    Before this PR, the API of annotation is "identical" to Retrofit annotations.

    Before on functions @Headers("Content-Type: application/json") after @Header("Content-Type", "application/json")

    Before on parameters @Header("Content-Type") param: String after @HeaderParam("Content-Type") param: String

πŸ” Migrating from Ktorfit to KtorGen

Migrating is as simple as:

Changing annotation imports to io.github.kingg22.ktorgen.http.*

Comma-separated header annotations are now repeatable and type-safe.

Before:

@Headers("Content-Type: application/json", "Accept: application/json")
suspend fun request(@Header("Authentication") token: String): String

After:

@Header("Content-Type", "application/json")
@Header("Accept", "application/json")
suspend fun request(@HeaderParam("Authentication") token: String): String

Passing your own Ktor HttpClient to the implementations, like this: fun UserRoute(client) for interface UserRoute and generated class _UserRouteImpl.

Real-life migration example: deezer-client-kt

Roadmap πŸš€

  • Add a matrix compatibility test on CI (Kotlin versions, KSP versions, Ktor Client versions) to know the range of compatibility.
  • Improve support for the default behavior of KSP in KMP projects.
  • Add better sample projects.
  • Improve the internal state of the processor to avoid unnecessary state-sharing between rounds. Includes: logging, tracing, validation, deferred symbols, etc.
  • Add more options to customize the generated code. Includes: generated annotations, custom KDoc, etc.
  • Add more options to the behavior of the processor. Includes: fail-fast, no-processing, no-tracing, etc.
  • Add performance benchmarks.
  • Add test for Fragment annotation
  • Add test for Cookie annotation
  • Resolve knowable issues described in #27
  • Resolve issues related to unresolved references, multi-round processing, unexpected errors. EDIT: Most unresolved symbols are caused by google/ksp#2668 and platform-specific issues caused by Optional expectation of some annotations.

Documentation

See more in GitHub Page

Open to contributions πŸš€ First usage needs to open in samples folder, the IDE will sync and compile automatically all.

πŸ“œ Disclaimer

This repository is a fork of Ktorfit and Retrofit annotations, with my own changes and additions. It is not affiliated with Ktor, JetBrains, Kotlin, Ktorfit, or Retrofit. Credits to their respective authors.

License: Apache 2.0, same as Retrofit and Ktorfit.