All Projects → Subito-it → Esito

Subito-it / Esito

Licence: other
Esito ambition is to be your return type for suspending functions.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Esito

Relax
☘☘Relax 基于Kotlin语言编写的一套组件化框架,不紧整体组件化、内部也高度组件化🎋你可配置MVP、MVVM的开发模式、也可以配置所需要的业务组件🍁🍁
Stars: ✭ 253 (+336.21%)
Mutual labels:  retrofit
RxjavaSamples
This repo is a container for some samples using RXJAVA2 samples
Stars: ✭ 12 (-79.31%)
Mutual labels:  retrofit
StatefulLiveData
StatefulLiveData is a lean, yet powerful tool that harnesses the capabilities of LiveData and enhances them, enabling the observer to distinguish between different states the data can be in, such as Success, Loading and Error.
Stars: ✭ 18 (-68.97%)
Mutual labels:  retrofit
RestaurantsExplorer
Android application build with MVVM Pattern, using Zomato API to enable search cities arround the world and display the city restaurants on a map.
Stars: ✭ 32 (-44.83%)
Mutual labels:  retrofit
Library-Spring
The library web application where you can borrow books. It's Spring MVC and Hibernate project.
Stars: ✭ 73 (+25.86%)
Mutual labels:  retrofit
restio
HTTP Client for Dart inspired by OkHttp
Stars: ✭ 46 (-20.69%)
Mutual labels:  retrofit
Networkresponseadapter
A Kotlin Coroutines based Retrofit call adapter that handles errors as a part of state
Stars: ✭ 246 (+324.14%)
Mutual labels:  retrofit
OLA Play Music App
Music Streaming App
Stars: ✭ 27 (-53.45%)
Mutual labels:  retrofit
litho-kotlin
A simple project showing how to set up an Android project to work with Kotlin and Facebook's Litho.
Stars: ✭ 37 (-36.21%)
Mutual labels:  retrofit
either option
A small typed and safe library for error handling with functionnal programming concept in Dart and flutter project
Stars: ✭ 34 (-41.38%)
Mutual labels:  result
common
A minimal library that defines primitive building blocks of PHP code.
Stars: ✭ 28 (-51.72%)
Mutual labels:  result
tiinvo
Functions for tacit programming and functional types for TypeScript and JavaScript.
Stars: ✭ 36 (-37.93%)
Mutual labels:  result
android
🌦 Vädret
Stars: ✭ 17 (-70.69%)
Mutual labels:  retrofit
RxHttp
Retrofit 与 协程结合 断点下载 上传,网络缓存
Stars: ✭ 15 (-74.14%)
Mutual labels:  retrofit
KotlinEverywhere
This application created for Kotlin Everywhere series as a codelab. It will show step by step Kotlin and Android Jetpack Components fundamentals. 🚀🚀
Stars: ✭ 52 (-10.34%)
Mutual labels:  retrofit
Retrosheet
Turn Google Spreadsheet to JSON endpoint (for Android and JVM) for FREE (100%)
Stars: ✭ 245 (+322.41%)
Mutual labels:  retrofit
RxHttp
对RxJava2+Retrofit2+OkHttp3的封装
Stars: ✭ 65 (+12.07%)
Mutual labels:  retrofit
Endpoint2mock
Android library which simplifies mocking of Retrofit endpoints
Stars: ✭ 16 (-72.41%)
Mutual labels:  retrofit
Retrofit-with-EventBus
Retrofit With EventBus
Stars: ✭ 21 (-63.79%)
Mutual labels:  retrofit
Photos
No description or website provided.
Stars: ✭ 74 (+27.59%)
Mutual labels:  retrofit

Esito

Coroutines are great for Asynchronous and non-blocking programming, but exceptions could be hard to handle, especially in a coroutine. [1, 2, 3]

Exceptions in a coroutine could cancel their parents, and each canceled parent cancels all its children, and this is not always the desired behavior.

While this could be changed using a SupervisorJob, it's still easy to shoot yourself in the foots throwing exceptions in Coroutines, so Esito is proposing an alternative approach making explicit the computations that could fail.

Installation

repositories {
	maven { url 'https://jitpack.io' }
}

dependencies {
    implementation("com.github.Subito-it.Esito:core:(insert latest version)")
}

Getting started

The main class is the sealed class Result, with two subtypes:

  • Success: it contains the result of a successful computation
  • Failure: it contains the cause of an unsuccessful computation

The goal when using Esito is to avoid throwing exceptions and use Result as the return type of a function that can fail:

sealed class ConversionError
object EmptyInput : ConversionError()
object NotNumericInput : ConversionError()

fun fromStringToInt(input: String): Result<Int, ConversionError> = when {
    input.isBlank() -> Result.failure(EmptyInput)
    else -> runCatching { input.toInt() }.mapError { NotNumericInput }
}

In this example we are defining all the possible failures in ConversionError, then we are applying some logic to build our result.

If the input is not blank we are using the runCatching method to wrap a method throwing an exception and mapping the eventual error in our desired type.

Operators

Esito result has several operators, such as map and flatmap, for examples see Recipes.

Retrofit Integration

Esito ships an integration with retrofit, after a one-line setup you can start to use Result return type in your Retrofit interfaces:

interface GitHubService {
    
    @GET("users/{user}/repos")
    suspend fun listRepos(@Path("user") user: String?): Result<List<Repo>, Throwable>
}

For additional info and to learn how to use your own Error instead of Throwable have a look at this documentation.

Async Utilities

Esito offers some utilities for suspending methods returning Result. For example, suppose we have the following functions:

suspend fun getUserFullName(userId: Int): Result<String, FeatureError>

suspend fun getUserStatus(userId: Int): Result<UserStatus, FeatureError>

And we have to return an instance of DataForUI running in parallel getUserFullName and getUserStatus.

data class DataForUI(
	val userFullName: String,
	val userStatus: UserStatus
)

Esito exposes a zip method to compute two independent execution in parallel:

suspend fun fetchIfo(userId: Int): Result<DataForUI, FeatureError> =
	zip(
		{ getUserFullName(userId) },
		{ getUserStatus(userId) },
		::DataForUI //syntactic sugar for constructor
		).invoke()

For additional info have a look at this documentation.

Testing

Esito is providing two extension methods to facilitate testing: assertIsSuccess and assertIsFailure, here is an example of usage:

val success = Result.success<Int, Throwable>(42)
success.assertIsSuccess {
    assertEquals(42, value)
}

val failure = Result.failure<Int, RuntimeException>(RuntimeException("Ops"))
failure.assertIsFailure {
    assertEquals("Ops", error.message)
}

For additional info have a look at this documentation.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].