在我的Play Scala应用中,我通过jar文件使用SOAP api库。在此界面中,无法通过调用来获取所有域项目。该文档建议开发人员使用带有分页参数的fetchDeltaSince。
如果要获取在startTime和endTime之间创建的任何域,则必须使用这样的方法:
Account::fetchDeltaSince(startTime : Calendar, page : Int, pageSize : Int, endTime : Calendar) : Array[Account]
pageSize最大允许为100,并且大约有30K个帐户。我将不得不使用某种while循环来获取此数据,该while循环监视返回的项目数和递增的页面参数(也许是?)。我希望结果全部为一种Array[Account]
数据类型。
在Scala中做到这一点的最佳方法是什么,有没有一种异步的方法?
假设您的抓取或多或少具有此签名:
def fetchDeltaSince(startTime: Calendar, page: Int, pageSize: Int, endTime: Calendar): Array[Account]
您可能有一个异步版本,例如Future
:
def fetchDeltaSinceAsync(startTime: Calendar, page: Int, pageSize: Int, endTime: Calendar): Future[Array[Account]]
然后您可以将其合并,例如
def fetchAll(startTime: Calendar,
pageSize: Int,
endTime: Calendar): Future[List[Account]] = {
def fetchUntilAll(page: Int, listBuffer: ListBuffer[Account]): Future[List[Account]]
fetchDeltaSinceAsync(startTime, page, pageSize, endTime).flatMap { newBatch =>
if (newBatch.isEmpty) Future.successful(listBuffer.toList)
else fetchUntilAll(page + 1, listBuffer ++= newBatch)
}
fetchUntilAll(0, new ListBuffer[fetchUntilAll]())
}
我使用了ListBuffer
,它避免了一次又一次地重新分配List
。在您的代码中,您将不得不进行调整,例如停止条件(也许不是空数组而是异常,还是其他?)。您可能还希望使用更多功能,例如用Monix的Future
替换Task
,用Cats的ListBuffer
替换Chain
。