SimpleCallApi is a library to assist with API calls. The idea is that it would be as simple as possible and could help from the beginner to the most senior developer. SimpleCallApi supports Coroutines and RxJava
implementation 'com.github.mattyoliveira:simple-call-api:2.0.0'
<dependency>
<groupId>com.github.mattyoliveira</groupId>
<artifactId>simple-call-api</artifactId>
<version>2.0.0</version>
<type>aar</type>
</dependency>
interface Api {
@GET("/api")
suspend fun getExemple(): yourType
}
createClientByService<Api>( baseUrl = "https://yourBase")
class ExempleRepository(val api: Api) {
suspend fun getExemple() = api.getExemple()
}
class ExempleUseCase(private val repositori: MainRepository) : SimpleUseCase<Post, None>(){
override suspend fun run(params: None) = repositori.getExemple()
}
class ExempleViewModel(private val useCase: ExempleUseCase) {
fun getExemple() {
viewModelScope.launch {
useCase(scope = this)
.onStatusChange {
when(it) {
AsyncStatus.DONE -> { //FinishOperation }
AsyncStatus.RUNNING -> { //RunningOperation }
AsyncStatus.ERROR -> { //ErrorOperation }
}
}
.onSuccess { it //return your type }
.onFailure { it //return ErrorEntity }
}
}
}
SimpleCallApi uses the Retrofit2, Coroutines, RxJava2, Moshi and Gson libraries.