I am new to Kotlin and I am making a method that makes a call to an interface of Endpoints and uses one of the methods present there. I am using Observable<> instead of Call<> into the response. I wanted to know how to obtain the response body() in the "result" above. This is my method
private fun refreshUser(userLogin: String) {
executor.execute {
// Check if user was fetched recently
val userExists = userDao.hasUser(userLogin, getMaxRefreshTime(Date())) != null
// If user have to be updated
if (!userExists) {
disposable = endpoints.getUser(userLogin)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result -> /*Get the response body() HERE*/},
{ error -> Log.e("ERROR", error.message) }
)
}
}
}
It all depends on how you have defined the Retrofit interface. In order to get the Response you need to return something from the interface that looks like:
fun getUsers() : Observable<Response<User>>
Then inside { result -> /*Get the response body() HERE*/}, you will get something of the form Response<User>, which has the response's body.
Also to note, you do not need to enclosing executor if you leverage Room for the dao interactions; it has RxJava support. You can use RxJava operators to combine the dao lookup with the server call.
See this tutorial
https://medium.freecodecamp.org/rxandroid-and-kotlin-part-1-f0382dc26ed8
//Kotlin
Observable.just("Hello World")
.subscribeOn(Schedulers.newThread())
//each subscription is going to be on a new thread.
.observeOn(AndroidSchedulers.mainThread()))
//observation on the main thread
//Now our subscriber!
.subscribe(object:Subscriber<String>(){
override fun onCompleted() {
//Completed
}
override fun onError(e: Throwable?) {
//TODO : Handle error here
}
override fun onNext(t: String?) {
Log.e("Output",t);
}
})
if you wanna use retrofit 2 and rxjava 2
https://medium.com/#elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890
interface WikiApiService {
#GET("api.php")
fun hitCountCheck(#Query("action") action: String,
#Query("format") format: String,
#Query("list") list: String,
#Query("srsearch") srsearch: String):
Observable<Model.Result>
}
Observable is the class response.
private fun beginSearch(srsearch: String) {
disposable =
wikiApiServe.hitCountCheck("query", "json", "search", srsearch)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result -> showResult(result.query.searchinfo.totalhits) },
{ error -> showError(error.message) }
)
}
If, as you mentioned to #Emmanuel, the return type of your getUser() method is Observable<Response<User>> then calling result.body() will yield the resulting User.
{ result ->
val user: User = result.body()
}
If however, you are looking for the the raw response, you can instead call result.raw().body(); which will return an okhttp3.ResponseBody type.
{ result ->
val body: ResponseBody = result.raw().body()
val text: String = body.string()
}
Related
In my projet I have a method taht subscribe each on my observable the same way. I'm trying to enhanced it by puttin the retryWhen option on it.
To avoid a big retrywhen to handle different error I have design this logique
A RetryFunction class that is generic
abstract class RxStreamLimitedRetryFunction(private val nbOfAttempts: Int, val streamId: String) : Function<Observable<Throwable>, Observable<*>> {
override fun apply(t: Observable<Throwable>): Observable<*> {
return t.flatMap {
if (shouldRetry(it)) Observable.just(it)
else Observable.empty()
}.zipWith(Observable.range(0, nbOfAttempts + 1), BiFunction<Throwable, Int, Int> { throwable, attempts ->
if (attempts == nbOfAttempts) {
throw RetryMaxAttemptsException(nbOfAttempts)
} else {
Log.d("Retry nb ${attempts + 1} out of $nbOfAttempts for stream with id : $streamId with error ${throwable.message} ")
attempts
}
}).flatMap { onRetry(it) }
}
abstract fun onRetry(attempsNb: Int): Observable<*>
abstract fun shouldRetry(throwable: Throwable): Boolean
}
two child class each with different retry attemps following the error
class RxStream404Retry(streamId: String) : RxStreamLimitedRetryFunction(4, streamId) {
override fun onRetry(attempsNb: Int): Observable<*> {
return Observable.timer(500, TimeUnit.MILLISECONDS)
}
override fun shouldRetry(throwable: Throwable): Boolean {
return true
} }
class RxStream500Retry(streamId: String) : RxStreamLimitedRetryFunction(2, streamId) {
override fun onRetry(attempsNb: Int): Observable<*> {
return Observable.timer(500, TimeUnit.MILLISECONDS)
}
override fun shouldRetry(throwable: Throwable): Boolean {
return false
}}
The shouldRetry method is simplified in this exemple
All of this retry function find they way in a list of retryfunction that is set using an ObservableTransformer to the observable via a retryWhen per function
class RetryComposer : ObservableTransformer<RxStreamSuccess, RxStreamSuccess> {
val retryFunctionList = arrayListOf(RxStream404Retry("Test1"),
RxStream500Retry("Test2")
)
override fun apply(upstream: Observable<RxStreamSuccess>): ObservableSource<RxStreamSuccess> {
retryFunctionList.forEach {
upstream.retryWhen(it)
}
return upstream
}}
My subscribing chain looks like this :
streamCache[stremId] = observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { listener.onLoading() }
.compose(RetryComposer())
.doOnComplete {
Log.d(" Retry onComplete")
streamCache.remove(stremId) }
.subscribe(
{ result -> listener.onSuccess(result) },
{ throwable ->
streamCache.remove(stremId)
}
)
When I test with an observable that goes into error nothing happen my RxStream404Retry is not trigger. Can you not put more thant one retryWhen per observable ?
Thank a lot
I think the issue comes from:
retryFunctionList.forEach {
upstream.retryWhen(it) <- this returns a new Observable that is not attached to any subscriber
}
This code is equivalent to:
Observable obs1 = upstream.retryWhen(RxStream404Retry("Test1"))
Observable obs2 = upstream.retryWhen(RxStream500Retry("Test2"))
return upstream
So, these observables are not subscribed by the subscriber of the main Rx chain.
You may have look at the amb() operators for that (http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html#amb-java.lang.Iterable-)
You may try something like:
return upstream.retryWhen(amb(retryFunctionList)) // pseudo code
That would be the rough idea.
Pasted below is a piece of code from Google's Plaid app which wraps suspend functions and safely executes network operations. What changes need to be made to make it work with RxJava functions rather than Coroutines and wait for the network result, any help is appreciated.
/**
* Wrap a suspending API [call] in try/catch. In case an exception is thrown, a [Result.Error] is
* created based on the [errorMessage].
*/
suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> {
return try {
call()
} catch (e: Exception) {
// An exception was thrown when calling the API so we're converting this to an IOException
Result.Error(IOException(errorMessage, e))
}
}
Usage example in ProductHuntRemoteDataSource.kt:
class ProductHuntRemoteDataSource #Inject constructor(private val service: ProductHuntService) {
/**
* Load Product Hunt data for a specific page.
*/
suspend fun loadData(page: Int) = safeApiCall(
call = { requestData(page) },
errorMessage = "Error loading ProductHunt data"
)
private suspend fun requestData(page: Int): Result<GetPostsResponse> {
val response = service.getPostsAsync(page)
if (response.isSuccessful) {
val body = response.body()
if (body != null) {
return Result.Success(body)
}
}
return Result.Error(IOException("Error loading ProductHunt data " +
"${response.code()} ${response.message()}"))
}
}
You can easily make this work with RxJava by converting your observables to suspending functions using the awaitFirst() or await() (for Single) extension functions. You need to use the RxJava extensions.
Edit:
you can write an extension function on your observables to do something similar, but not identical.
Here's a possible solution
fun <T> Observable<Result<T>>.safeCall(errorMessage: String, block: (Result<T>) -> Unit): Disposable =
subscribe({ response: Result<T> ->
if (response.isSuccess && response.body != null) {
block(Result.Success(response.body))
} else {
block(Result.Error(IOException(errorMessage)))
}
}, { throwable ->
block(Result.Error(IOException(errorMessage, throwable)))
})
you would call this on an observable like this
val disposable = myObservable.safeCall("Error message") { result ->
// TODO: handle result
}
I am building an app based off of the Android Clean Architecture Kotlin version (https://github.com/android10/Android-CleanArchitecture-Kotlin).
Using this architecture, each time you want to invoke a use case, a Kotlin coroutine is launched and the result is posted in the main thread. This is achieved by this code:
abstract class UseCase<out Type, in Params> where Type : Any {
abstract suspend fun run(params: Params): Either<Failure, Type>
fun execute(onResult: (Either<Failure, Type>) -> Unit, params: Params) {
val job = async(CommonPool) { run(params) }
launch(UI) { onResult.invoke(job.await()) }
}
In his example architecture, Mr. Android10 uses Retrofit to make a synchronous api call inside the kotlin couroutine. For example:
override fun movies(): Either<Failure, List<Movie>> {
return when (networkHandler.isConnected) {
true -> request(service.movies(), { it.map { it.toMovie() } }, emptyList())
false, null -> Left(NetworkConnection())
}
}
private fun <T, R> request(call: Call<T>, transform: (T) -> R, default: T): Either<Failure, R> {
return try {
val response = call.execute()
when (response.isSuccessful) {
true -> Right(transform((response.body() ?: default)))
false -> Left(ServerError())
}
} catch (exception: Throwable) {
Left(ServerError())
}
}
'Either' represents a disjoint type, meaning the result will either be a Failure or the object of type T you want.
His service.movies() method is implemented like so (using retrofit)
#GET(MOVIES) fun movies(): Call<List<MovieEntity>>
Now here is my question. I am replacing retrofit with Google Cloud Firestore. I know that currently, Firebase/Firestore is an all async library. I want to know if anyone knows of a method more elegant way of making a synchronous API call to Firebase.
I implemented my own version of Call:
interface Call<T: Any> {
fun execute(): Response<T>
data class Response<T>(var isSuccessful: Boolean, var body: T?, var failure: Failure?)
}
and my API call is implemented here
override fun movieList(): Call<List<MovieEntity>> = object : Call<List<MovieEntity>> {
override fun execute(): Call.Response<List<MovieEntity>> {
return movieListResponse()
}
}
private fun movieListResponse(): Call.Response<List<MovieEntity>> {
var response: Call.Response<List<MovieEntity>>? = null
FirebaseFirestore.getInstance().collection(DataConfig.databasePath + MOVIES_PATH).get().addOnCompleteListener { task ->
response = when {
!task.isSuccessful -> Call.Response(false, null, Failure.ServerError())
task.result.isEmpty -> Call.Response(false, null, MovieFailure.ListNotAvailable())
else -> Call.Response(true, task.result.mapTo(ArrayList()) { MovieEntity.fromSnapshot(it) }, null)
}
}
while (response == null)
Thread.sleep(50)
return response as Call.Response<List<MovieEntity>>
}
Of course, the while loop at the end bothers me. Is there any other, more elegant ways, to wait for the response to be assigned before returning from the movieListResponse method?
I tried calling await() on the Task that is returned from the Firebase get() method, but the movieListResponse method would return immediately anyway. Thanks for the help!
So I found what I was looking for in the Google Tasks API: "If your program is already executing in a background thread you can block a task to get the result synchronously and avoid callbacks" https://developers.google.com/android/guides/tasks#blocking
So my previous problematic code becomes:
private fun movieListResponse(): Call.Response<List<MovieEntity>> {
return try {
val taskResult = Tasks.await(FirebaseFirestore.getInstance().
collection(DataConfig.databasePath + MOVIES_PATH).get(), 2, TimeUnit.SECONDS)
Call.Response(true, taskResult.mapTo(ArrayList()) { MovieEntity.fromSnapshot(it) }, null)
} catch (e: ExecutionException) {
Call.Response(false, null, Failure.ServerError())
} catch (e: InterruptedException) {
Call.Response(false, null, Failure.InterruptedError())
} catch (e: TimeoutException) {
Call.Response(false, null, Failure.TimeoutError())
}
}
Note I no longer need my Thread.sleep while loop.
This code should only be run in a background thread/kotlin coroutine.
This is overengineered, there are several layers trying to do the same thing. I suggest you go back a few steps, undo the abstractions and get into the mood of using coroutines directly. Implement a suspend fun according to this template. You don't need the crutches of Either, handle exceptions in the most natural way: a try-catch around a suspend fun call.
You should end up with a signature as follows:
suspend fun movieList(): List<MovieEntity>
Call site:
launch(UI) {
try {
val list = movieList()
...
} catch (e: FireException) {
// handle
}
}
That's is not the way how firebase works. Firebase is based on callback.
I recommend architecture component's livedata.
Please check the following example.
here is a link: https://android.jlelse.eu/android-architecture-components-with-firebase-907b7699f6a0
I was looking other questions but this one is using a different method.
I’m using MVVM in my Android app.
Actually this is the way that I’m getting data from my server:
Inject dataManager into viewModel.
viewModel calls dataManager->fetchUsers
fetchUsers make request to server and return an Observable of Several which in this case should be Several but it’s generic.
viewModel subscribe to this request and expect a Several.
At this point everything works besides Several doesn’t have a list of User. This several have a list of LinkedTreeMap
I tried to change my dataManager to return a string then map the response in my viewModel but the thing with this is that I will have to do that in every request.
Also I tried to map the request in my dataManager but I got the same link tree map array.
The thing with TypeToken approach is that I have to map in my viewModel.
UPDATED
UsersViewModel
private fun fetchUsers() {
isLoading.set(false)
compositeDisposable += dataManager.GET<User>(classJava = User::class)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( { response ->
isLoading.set(false)
// Here the response should be a several with a list of users.
}, { err ->
isLoading.set(false)
})
}
GET function:
override fun <T> GET(classJava: KClass<*>): Single<Several<T>> {
return Rx2AndroidNetworking.get("EndPoint")
.addHeaders("Authorization", apiHeader.protectedAPIHeader.accessToken!!)
.setOkHttpClient(APIClient.getUnsafeClient())
.build()
// Also I tested with
// .getObjectSingle(Several::class.java) // The thing is that I can't assign the type <T> with this approach.
.stringSingle
.map {
val fromJSON = fromJson<Several<T>>(it)
fromJSON
}
}
fromJson function:
inline fun <reified T> fromJson(json: String): T = Gson().fromJson(json, object: TypeToken<T>() {}.type)
Several.kt
data class Several<T>(val items: MutableList<T>)
If I change my GET function to return a String then in UsersViewModel I add a .map like this
private fun fetchUsers() {
isLoading.set(false)
compositeDisposable += dataManager.GET<User>(classJava = User::class)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(
val severalUsers = fromJson<Several<User>>(it)
severalUsers
)
.subscribe( { response ->
isLoading.set(false)
// usersLiveData.value = response
}, { err ->
Log.e("tag", "Ocurred some error")
isLoading.set(false)
})
}
Then I would have a list of Users as expected but What I dont want to do is map the response in UsersViewModel because I would have to do in every single request that expect a list of items.
Is there any way that I could get a several object without map in my viewmodel?
I'm trying to create generic architecture to consume complex json structure as follows:
Json Format
{
"type": "success",
"code": "s-groups-0006",
"description": "Index List successfully",
"result": {
"asOnDate": 1505457095278,
"indexList": [
{
"change": "22.35",
"changePercent": "0.27",
"isDefault": true,
"isEditable": false
}
]
}
}
Dagger Format
#Singleton
fun provideGson(): Gson =
GsonBuilder()
.setLenient()
// .registerTypeAdapter(BaseResponse::class.java, RestDeserializer<BaseResponse<T>>())
.create()
Rest Deseralizers
class RestDeserializer<T> : JsonDeserializer<T> {
#Throws(JsonParseException::class)
override fun deserialize(je: JsonElement, type: Type, jdc: JsonDeserializationContext): T? {
val content = je.asJsonObject
// Deserialize it. You use a new instance of Gson to avoid infinite recursion
// to this deserializer
return Gson().fromJson<T>(content, type)
}
}
Error callback
abstract class ErrorCallBack<T : BaseResponse<T>> : DisposableObserver<T>() {
protected abstract fun onSuccess(t: T)
override fun onNext(t: T) {
//You can return StatusCodes of different cases from your API and handle it here. I usually include these cases on BaseResponse and iherit it from every Response
onSuccess(t)
}
override fun onError(e: Throwable) {
when (e) {
is HttpException -> {
val responseBody = (e).response().errorBody()
responseBody?.let {
L.e("Error in call htttp exception")
}
}
is SocketTimeoutException -> {
// todo
L.e("Error in Socket time out")
}
is IOException -> {
// todo
L.e("Error in IO Exception")
}
else -> {
e.message?.let {
// todo
}
}
}
}
override fun onComplete() {
}
private fun getErrorMessage(responseBody: ResponseBody): String {
return try {
val jsonObject = JSONObject(responseBody.string())
jsonObject.getString("message")
} catch (e: Exception) {
e.message!!
}
}
}
Repository
override fun getValidateUser(validateUser: ValidateUser): LiveData<ValidateUserResponse> {
val mutableLiveData = MutableLiveData<ValidateUserResponse>()
remoteServices.requestValidateUser(validateUser)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : ErrorCallBack<BaseResponse<ValidateUserResponse>>() {
override fun onSuccess(t: BaseResponse<ValidateUserResponse>) {
if (t.type == CommonContents.SUCCESS) {
L.d("Success in validate user")
mutableLiveData.value = transform(t)
} else {
L.e("Validate User Error")
}
}
})
return mutableLiveData
}
Data Class
data class BaseResponse<out T>(
#SerializedName(CommonContents.TYPE) val type: String,
#SerializedName(CommonContents.CODE) val Code: String,
#SerializedName(CommonContents.DESCRIPTION) val Description: String,
#SerializedName(CommonContents.RESULT)val result: T? = null)
These are my structures and I try to make a generic structure but am facing an issue when invoking the Error callback.
Please guide me how to achieve this. Can i call generic method inside generic Response?
.subscribeWith(object : ErrorCallBack<BaseResponse<ValidateUserResponse>>() {
Guide To Achieving Working Code
This is my guide to making some code work. It is based on the principles of Test Driven Development.
Set up your unit test environment in your IDE. Personally, I have been using JUnit 4 with Eclipse, but you may prefer JUnit 5 with JetBrains' IDE.
Write a unit test for your ErrorCallback class constructor. Make it pass. Next, write unit tests for each of the methods, to see that it behaves the way that you expect.
Write unit tests where your test fixture is a PublishSubject<BaseResponse<Integer>> for some number of different cases: normal data, sequence of normal data, normal data followed by error, normal data followed by completion.
From there, add some more tests so that you can test the entire observable chain.