https://github.com/artemmey/kontract
https://github.com/artemmey/kontract
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/artemmey/kontract
- Owner: ArTemmey
- Created: 2021-09-08T19:57:58.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-21T16:40:41.000Z (almost 3 years ago)
- Last Synced: 2025-10-09T06:07:36.289Z (10 days ago)
- Language: Kotlin
- Size: 86.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Framework for interaction between Ktor server and Ktor client.
### Usage example
Common code (API contract):
```kotlin
sealed class Api : ApiContract() {var accessToken by header(HttpHeaders.Authorization)
class Sessions : Api() {
override val path = "/sessions"fun post(body: SessionsPostIn) = post(body)
}class Users : Api() {
override val path = "/users"fun post(body: domain.entity.User) = post(body)
fun put(body: domain.entity.User) = put(body)
}class User : Api() {
override val path = "/users/current"fun get() = get()
}
}
```Server code:
```kotlin
method(Api.Users::post) { postUser(it) }method(Api.Users::put) { putUser(it) }
method(Api.Sessions::post) { postSession(it) }
```Client code:
```kotlin
Api.Sessions().post(SessionsPostIn(email, pass)).execute()
.onSuccess { saveSession(it) }
```