This is Deferred Extension to make an API request on Android UI with an exception message, build on kotlin coroutines and Retrofit.
Add the dependencies to your gradle file:
dependencies {
implementation 'com.hossamelsharkawy:req:0.0.1'
}
Init Server and add BaseUrl in your Application class
Server.init("https://jsonplaceholder.typicode.com/")
Create API interface
interface Api {
@GET("photos")
fun photos(): Deferred<ArrayList<Photo>>
}
Create API instance
val typicodeApi = Server.create(Api::class.java)
Request api on success response
typicodeApi.photos().req { photos -> //handel resonse }
Request api on success or failure response
typicodeApi.photos().req(
{ res -> //handel resonse }
,{ errorMsg -> //handel error })
Request api on success , failure or empty data
typicodeApi.photos().req(
onResponse ={res -> //handel resonse }
, onError = {errorMsg -> //handel error }
, onEmpty = { emptyMsg-> //handel empty })
Cancel request
val photosReq = typicodeApi.photos().req(
onResponse ={res -> //handel resonse }
,onError ={errorMsg -> //handel error }
)
photosReq.cancel()