SimpleCallApi
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
Version
Gradle
implementation 'com.github.mattyoliveira:simple-call-api:2.0.0'
Maven
<dependency>
<groupId>com.github.mattyoliveira</groupId>
<artifactId>simple-call-api</artifactId>
<version>2.0.0</version>
<type>aar</type>
</dependency>
Example
ApiService
interface Api {
@GET("/api")
suspend fun getExemple(): yourType
}
NetworkConfig
createClientByService<Api>( baseUrl = "https://yourBase")
SimpleRespository
class ExempleRepository(val api: Api) {
suspend fun getExemple() = api.getExemple()
}
SimpleUseCase
class ExempleUseCase(private val repositori: MainRepository) : SimpleUseCase<Post, None>(){
override suspend fun run(params: None) = repositori.getExemple()
}
SimpleViewModel
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 }
}
}
}
Technology
SimpleCallApi uses the Retrofit2, Coroutines, RxJava2, Moshi and Gson libraries.