Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bnorm/ktor-retrofit
Turns a Retrofit service interface into Ktor routing
https://github.com/bnorm/ktor-retrofit
kotlin ktor ktor-framework retrofit retrofit2
Last synced: 10 days ago
JSON representation
Turns a Retrofit service interface into Ktor routing
- Host: GitHub
- URL: https://github.com/bnorm/ktor-retrofit
- Owner: bnorm
- License: apache-2.0
- Created: 2018-09-28T23:10:01.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-04T02:24:54.000Z (over 4 years ago)
- Last Synced: 2024-10-14T17:25:56.722Z (23 days ago)
- Topics: kotlin, ktor, ktor-framework, retrofit, retrofit2
- Language: Kotlin
- Size: 104 KB
- Stars: 132
- Watchers: 7
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# ktor-retrofit
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.bnorm.ktor.retrofit/ktor-retrofit/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.bnorm.ktor.retrofit/ktor-retrofit)
Turns a Retrofit service interface into Ktor routing. Very early stage
prototype.You use Retrofit on the client side to access the end points of your server,
why not use that same interface to define the routings? The idea is to use an
implementation of the interface and add it to Ktor routing which will
dynamically construct sub-routings from the annotations and call the
implementation when the end point is accessed.## Example
```kotlin
interface Service {
@GET("string")
suspend fun getAll(): List@GET("string/{id}")
suspend fun getSingle(@Path("id") id: Long): String
}fun Application.module() {
install(ContentNegotiation) {
jackson { }
}val service = object : Service {
override suspend fun getAll(): List {
return listOf("first", "second")
}override suspend fun getSingle(id: Long): String {
return when (id) {
0L -> "first"
1L -> "second"
else -> throw IndexOutOfBoundsException("id=$id")
}
}
}
routing {
route("api") {
retrofitService(service = service)
}
}
}
```