I need to make a request to a webserver and the server usually returns a reference code that i use in the stream. But sometimes the server is busy and returns an error indicating that it's busy so that i have to retry in a few moments.
public static Single<List<iIB>> run()
{
//First retrieve reference code from IB
return IBRepository.getInstance().getReferenceCode()
.doOnSuccess(s -> Log.d(AppConstants.AppTag, "Success: " + s))
.doOnError(s -> Log.d(AppConstants.AppTag, "Error: " + s))
//Once reference code is retrieved, make second request to get XML report as string
.flatMap((Function<String, SingleSource<String>>) referenceCode -> IBRepository.getInstance().getXMLReport(referenceCode))
...
}
/**
* This method makes the first request to IB to get the reference code for the flex query in case of success
* Sometimes server is busy and ask to retry the request in a few moments so we return an error indicating the cause
* @return an observable string containing the reference code to download the XMLParser report in IB
*/
public Single<String> getReferenceCode()
{
final String REF_CODE = "ReferenceCode";
final String STATUS = "Status";
final String FAIL = "Fail";
final String ERROR_MESSAGE = "ErrorMessage";
return Single.fromObservable(_Volley.getInstance().postRxVolley(IBConstants.QUERY_REQUEST_URL)
.flatMap((Function<Result, ObservableSource<String>>) Response::processResultResponse)
.flatMapSingle((Function<String, SingleSource<String>>) xmlResponse ->
{
Document xmlDocument = XMLParser.convertStringToXMLDocument(xmlResponse);
if(xmlDocument == null)
return Single.error(new Throwable("Error with DOM XML Library"));
else if(!XMLParser.getSingleValueFromTag(xmlDocument, STATUS, 0).contains(FAIL))
return Single.just(XMLParser.getSingleValueFromTag(xmlDocument, REF_CODE, 0));
else
return Single.error(new Throwable(XMLParser.getSingleValueFromTag(xmlDocument, ERROR_MESSAGE, 0)));
}));
}
What i'm trying to achieve is to retry the call to getReferenceCode() after some time if that method returns an error. Could this be achievable using RxJava operators?
Thanks