Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/AndroidPoet/KtorBoost

🚀 Simplifying Ktor for Easier Development KMM/Compose Multiplatform
https://github.com/AndroidPoet/KtorBoost

android-library compose compose-multiplatform kmm kmm-library ktor ktor-client networking

Last synced: about 2 months ago
JSON representation

🚀 Simplifying Ktor for Easier Development KMM/Compose Multiplatform

Lists

README

        

KtorBoost




License
API
Build Status
Android Weekly
Profile



🚀 Simplifying Ktor for Easier Development.
Ktor Boost streamlines HTTP requests in Ktor by offering functions that neatly package results in
the Kotlin's Result class. It makes
handling successes and errors clearer, simplifying error control in Ktor apps




## Download

[![Maven Central](https://img.shields.io/maven-central/v/io.github.androidpoet/ktor-boost.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.androidpoet%22%20AND%20a:%22ktor-boost%22)

### Without this line, you will receive an error response in the 'success' field.

```kotlin
val client = HttpClient() {
expectSuccess = true
}

```

### Gradle

Add the dependency below to your **module**'s `build.gradle` file:

```gradle
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.androidpoet:ktor-boost:$version")

}
}
}
```

## Usage

```kotlin

// normal get,post,put...methods will be replaced by getResult,postResult,putResult...

val result = httpClient.getResult("sample_get_url")

val result = httpClient.postResult("sample_post_url")

val result = httpClient.putResult("sample_put_url")

val result = httpClient.deleteResult("sample_delete_url")

val result = httpClient.patchResult("sample_patch_url")

val result = httpClient.headResult("sample_head_url")

val result = httpClient.optionsResult("sample_options_url")

// for async and await methods use these

val result = httpClient.getResultAsync("sample_get_url")

val result = httpClient.postResultAsync("sample_post_url")

val result = httpClient.putResultAsync("sample_put_url")

val result = httpClient.deleteResultAsync("sample_delete_url")

val result = httpClient.patchResultAsync("sample_patch_url")

val result = httpClient.headResultAsync("sample_head_url")

val result = httpClient.optionsResultAsync("sample_options_url")

val deferredResult = httpClient.getResultAsync("sample_get_url")
val result = deferredResult.await()

```

## Without KtorBoost

Initially, when fetching data from APIs, the code directly gets the response's content, leading to
repetitive use of the 'body()' method:

```kotlin
// api service claas function
suspend fun getUserNames(): List {
return httpClient.get("trendingMovies").body()
}

//for async oprations

suspend fun getAsyncUserNames(): Deferred> {
return coroutineScope { async { httpClient.get("trendingMovies").body() } }
}

```

Handling errors involves enclosing each API call within separate try-catch blocks, causing code
duplication:

```kotlin
// viewModel

viewModelScope.launch {
try {
val data = apiService.getUserNames()
// handle data
} catch (e: Throwable) {
// handle error
}
}

// Async Await

viewModelScope.launch {
try {
val deferredData = apiService.getAsyncUserNames()
deferredResult.await()
// handle data
} catch (e: Throwable) {
// handle error
}
}

```

## With KtorBoost

With improvements, the API calls are now focused solely on returning values:

```kotlin
// api service claas function
suspend fun getUserNamesResult() = httpClient.getResult>("trendingMovies")

//for async oprations

suspend fun getAsyncUserNames(): = httpClient.getResultAsync>("trendingMovies")

```

This refined approach in error and response handling simplifies the code:

```kotlin
// viewModel

viewModelScope.launch {
val result = apiService.getUserNames()
result.onSuccess { data ->
// handle data
}.onFailure { error ->
// handle error
}
}

// Async Await

viewModelScope.launch {
val deferredResult = apiService.getAsyncUserNames()
val result = deferredResult.await()

result.onSuccess { data ->
// handle data
}.onFailure { error ->
// handle error
}

}

```

## Different variations and use cases

The solution offers multiple variations in response handling, enabling adaptability based on team
preferences and project needs:

```kotlin

//If you prefer executing the response function as a suspend function, useful for nested suspend
//function usage on success or error:

viewModelScope.launch {
result.onSuccessSuspend { data ->
// handle data
}.onFailureSuspend { error ->
// handle error
}
}

```

```kotlin
// For cases solely interested in success response, using getOrNull() function and handling errors manually:

viewModelScope.launch {
if (result.isSuccess) {
val data = result.getOrNull()
// handle data
} else {
// handle error
}
}

```

```kotlin

// Folding the response, a concise approach for clean and readable code:

viewModelScope.launch {
result.fold(onSuccess = { data ->
// handle data
}, onFailure = { error ->
// handle error
})
}

```

```kotlin

// Fold also supports suspend version:

viewModelScope.launch {
result.foldSuspend(
onSuccess = { data ->
// handle data
}, onFailure = { error ->
// handle error
})

}

```

These diverse approaches in response handling empower you to select the most suitable method,
whether for executing suspend functions,
specifically handling success responses, or streamlining code for clearer readability.

## Find this repository useful? :heart:

Support it by joining __[stargazers](https://github.com/androidpoet/KtorBoost/stargazers)__ for this
repository. :star:

Also, __[follow me](https://github.com/androidpoet)__ on GitHub for my next creations! 🤩

# License

```xml
Copyright 2023 AndroidPoet (Ranbir Singh)

Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
```