0

我正在使用 Web API 来检索一些报告。首先,我必须发送一个请求以获取参考代码,并使用该代码发出第二个请求以获取报告。问题是,当报告太大时,需要一些时间来准备,因此服务器会返回警告/错误,指示我必须稍后检查。

因此,每当我从以前的平面图中收到警告或错误时,我都会尝试重复第二个请求,但它似乎不起作用。有任何想法吗?

/**
 * This method makes all the necessary tasks to import the operations from the XML report of IB.
 * It stores the new operations in IBRepository and returns the number of new operations to UI
 * using years MutableLiveData
 */
void checkForNewOperations()
{
    compositeDisposable.add(
            Task_RetrieveNewOperations.run()
                    .doOnNext(iIBNewTransactions -> IBRepository.getInstance().addTransactionsToNewOperationsList(iIBNewTransactions))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            iIBNewTransactions -> newOperations.setValue(Response.success(iIBNewTransactions.size())),
                            error -> newOperations.setValue(Response.error(error))
                    ));
}

/**
 * This method is used to retrieve the new operations from IB Report
 * To get the report, two calls are needed. First one to get reference code and second one to get the XML report as string
 * Every node of the XML report is emitted one by one to convert it to the appropriate object (Trade or CashTransaction)
 * As there might be some splitted or duplicated operations, I group them in a single one
 * Then, we check is its transaction ID is already in Firebase to exclude it from the final returned list
 * @return The list of new operations from IB report
 */
public static Observable<List<iIB>> run()
{
    //First retrieve reference code from IB
    return IBRepository.getInstance().getReferenceCode()

            //Once reference code is retrieved, make second request to get XML report as string
            .flatMap((Function<String, ObservableSource<Document>>) referenceCode -> IBRepository.getInstance().getXMLReport(referenceCode))
            .flatMapSingle((Function<Document, Single<List<iIB>>>) xmlReport ->

            ...
}

public Observable<Document> getXMLReport(String referenceCode)
{
    final String STATUS = "Status";
    final String FAIL = "Fail";
    final String WARN = "Warn";
    final String ERROR_MESSAGE = "ErrorMessage";

    String url = IBConstants.DOWNLOAD_REPORT_URL + referenceCode + "&t=" + IBConstants.IB_TOKEN + "&v=3";
    return _Volley.getInstance().postRxVolley(url)
            .flatMap((Function<Result, ObservableSource<String>>) Response::processResultResponse)
            .flatMap((Function<String, ObservableSource<Document>>) xmlString -> Observable.fromCallable(() -> XMLParser.convertStringToXMLDocument(xmlString)))
            .flatMap(xmlDocument ->
            {
                String status = XMLParser.getSingleValueFromTag(xmlDocument, STATUS, 0);

                return !status.contains(WARN) && !status.contains(FAIL) ?
                        Observable.just(xmlDocument) :
                        Observable.error(new Throwable(XMLParser.getSingleValueFromTag(xmlDocument, ERROR_MESSAGE, 0)));
            })
            .retryWhen(throwables -> throwables.delay(1, TimeUnit.SECONDS));
}
4

0 回答 0