玩WS递归Get请求分页

Play WS Recursive Get Requests for Pagination

我正在尝试使用 Play WS 编写一个递归函数来执行多个获取请求以遍历给定的所有页面。下一页(包含所有查询参数)在 header "Link".

中给出

这个函数只 return 给我 1 个值,然而我期望它 return 几个,所以我做错了什么,但对未来了解不够递归以了解发生了什么。

如有任何建议,我们将不胜感激!

def getDnaProjectsIdsWithActivity(
                                   wsClient: StandaloneAhcWSClient,
                                   startTime: String,
                                   endTime: String,
                                   nextPage: String = ""
                                 ): Future[Seq[Project]] = {
  val request = nextPage match {
    case "" =>
      wsClient
        .url(baseUrl + projectResource)
        .addQueryStringParameters(
          "private_token" -> token,
          "visibility" -> "private",
          "archived" -> "false",
          "pagination" -> "keyset",
          "per_page" -> "1",
          "order_by" -> "id",
          "sort" -> "asc",
          "simple" -> "true",
          "last_activity_after" -> startTime,
          "last_activity_before" -> endTime,
          "owned" -> "true"
        )

    case page: String =>
      wsClient.url(page)
  }

  request
    .get()
    .flatMap { response =>
      val nextPage = response.header("Link")
      nextPage match {
        case None => Future { processProjectIdResponse(response) } // Process a json response to Seq[Project]
        case Some(value) => {
          val page = value.split("[<>]")(1)
          Future { processProjectIdResponse(response) }
          getDnaProjectsIdsWithActivity(wsClient, startTime, endTime, page)
        }
      }
    }
}

您的问题是您没有将当前结果与递归结果连接起来。试试这个:

request
  .get()
  .flatMap { response =>
    response.header("Link") match {
      case None => Future.successful(processProjectIdResponse(response)) // Process a json response to Seq[Project]
      case Some(value) => {
        val page = value.split("[<>]")(1)
        getDnaProjectsIdsWithActivity(wsClient, startTime, endTime, page).map { response1 =>
          processProjectIdResponse(response) ++ response1

        }
      }
    }
  }