改造 RxAndroid 为 api 调用制作包装器 [英] Retrofit RxAndroid make wrapper for api calls
问题描述
我想在改造中为 api 调用创建一个包装器,以便我可以在公共位置显示 ProgressDialog &处理常见响应.
I wanted to create a wrapper for api calls in retrofit so I can display ProgressDialog at common place & handle common response.
我通过创建这样的包装器来实现这一点
I achieved this by creating wrapper like this
public static <T> Observable<T> callApiWrapper(final Context context,
final boolean shouldShowProgress,
final String message,
final Observable<T> source) {
final ProgressDialog progressDialog = new ProgressDialog(context);
if (shouldShowProgress) {
if (!TextUtils.isEmpty(message))
progressDialog.setMessage(message);
else
progressDialog.setMessage(context.getString(R.string.please_wait));
}
return source.lift(new Observable.Operator<T, T>() {
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
return new Subscriber<T>() {
@Override
public void onStart() {
super.onStart();
if (shouldShowProgress) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
progressDialog.show();
}
});
}
child.onStart();
}
@Override
public void onCompleted() {
if (shouldShowProgress && progressDialog.isShowing())
progressDialog.dismiss();
child.onCompleted();
}
@Override
public void onError(Throwable e) {
if (shouldShowProgress && progressDialog.isShowing())
progressDialog.dismiss();
child.onError(e);
}
@Override
public void onNext(T t) {
/*
Handle Invalid API response
*/
if (((BaseResponse) t).getStatus() == RestParams.Codes.INVALID_API_KEY) {
mCommonDataModel.setApiKey("");
getCommonApiService().getApiKey()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String response = responseBody.string();
JSONObject jsonObject = new JSONObject(response);
String key = jsonObject.optString("KEY");
if (!TextUtils.isEmpty(key))
mCommonDataModel.setApiKey(key);
callApiWrapper(context, shouldShowProgress,
message, source)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} else {
if (shouldShowProgress && progressDialog.isShowing())
progressDialog.dismiss();
child.onNext(t);
}
}
};
}
});
}
在上面的代码中,我检查是否获得了 Invalid API KEY 这样的特定状态代码,然后我调用 API 来获取新的 API 密钥,而不是直接将状态提供给原始订阅者.
In the above code, I check that if I get specific status code like Invalid API KEY, then I am calling an API to get the new API key instead of giving the status directly to original subscriber.
一旦我成功获得新的 API 密钥,我就会递归调用包装器 &尝试向原始订阅者提供响应.但问题是原始订阅者没有得到 onNext
回调
Once I get the new API key successfully, I call the wrapper recursively & try to give the response to original subscriber. But the problem is Original Subscriber is not getting onNext
callback
我在这里错过了什么?有没有其他方法可以实现我想要做的事情?
What am I missing here? Is there any other way of achieving what I am trying to do?
推荐答案
最后,我设法创建了一个包装器来为我处理常见的 Progressbar &密钥 API 响应无效时的重试逻辑.如果在许多情况下,这种包装器可能很有用.感谢@JohnWowUs 的回答,这帮助我理解了事情 &实现这个包装器.
Finally I managed to create a wrapper which handles for me the common Progressbar & retry logic in case of Invalid Key API Response. This kind of wrapper might be useful if in many cases. Thanks to @JohnWowUs for his answer which helped me to understand things & implement this wrapper.
这是工作代码
private static final int MAX_RETRIES = 2;
private static int sCurrentRetryAttempt = 0;
/**
* Common Wrapper for calling API.
*
* @param context context for showing progress dialog
* @param shouldShowProgress boolean which indicated if progress dialog should be shown or not
* @param message message to be shown in progress dialog. if null passed, then "Please wait..." will be shown
* @param source original observable
* @return observable to which observer can subscribe
*/
public static <T> Observable<T> callApiWrapper(final Context context,
final boolean shouldShowProgress,
final String message,
final Observable<T> source) {
// Progress Dialog
final ProgressDialog progressDialog = setupProgressDialog(context, shouldShowProgress, message);
if (progressDialog != null) progressDialog.show();
return source
.flatMap(new Func1<T, Observable<T>>() {
@Override
public Observable<T> call(T t) {
/*
* Check if the response contains invalid key status code.
*/
if (t instanceof BaseResponse) {
if (((BaseResponse) t).getStatus() == RestParams.Codes.INVALID_API_KEY) {
return Observable.error(new InvalidKeyException("Invalid key"));
}
}
/*
* We are here, that means, there wasn't invalid key status code.
* So we wouldn't like to handle it so just return to original subscriber
*/
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
return Observable.just(t);
}
}).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Throwable> observable) {
return observable.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(final Throwable throwable) {
if (throwable instanceof InvalidKeyException) {
/*
* Check for retry limit. if we already have retried enough, then
* we should tell the original subscriber about the error as it
* doesn't seems recoverable.
*/
if (sCurrentRetryAttempt >= MAX_RETRIES) {
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
//Resetting the attempts to 0
sCurrentRetryAttempt = 0;
return Observable.error(throwable);
}
//Increase the attempt counter
sCurrentRetryAttempt += 1;
return getCommonApiService().getApiKey()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Func1<ResponseBody, Observable<?>>() {
@Override
public Observable<?> call(ResponseBody responseBody) {
try {
/*
* Check if we succeed in our attempt to handle
* invalid key
*/
if (processApiKey(responseBody)) {
/*
* We succeeded in our attempts to handle
* invalid api key, so we will return the
* original subscriber what it wanted.
*/
return callApiWrapper(context,
shouldShowProgress, message, source);
} else
return Observable.just(throwable);
} catch (Exception e) {
/*
* We are here that means something went wrong,
* so we will retry silently.
*/
return Observable.just(throwable);
}
}
});
} else {
/*
* For any other error, we are not going to handle right now,
* so just return
*/
return Observable.error(throwable);
}
}
});
}
});
}
&使用这个和正常一样:
& using this is same as normal like:
RestClient.callApiWrapper(mContext, true, null,
RestClient.getAuthenticationApiService().socialLogIn(name, email, singInVia, mobile, "android", deviceToken))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<BaseResponse<RegistrationResponse>>() {
//...
}
这篇关于改造 RxAndroid 为 api 调用制作包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!