Explorar o código

Add recursion to ensure collection of the whole data

theenglishway (time) %!s(int64=2) %!d(string=hai) anos
pai
achega
db15049587
Modificáronse 1 ficheiros con 32 adicións e 24 borrados
  1. 32 24
      src/main/scala/BallDontLie.scala

+ 32 - 24
src/main/scala/BallDontLie.scala

@@ -126,18 +126,17 @@ object EndpointResponse {
   def getTeams(): ListOfTeams = {
     implicit val teamsResponseR: Reader[TeamsResponse] = macroR[TeamsResponse]
 
-    def getAllData(endpoint: String) = {
-      val response =
-        doPaginatedRequest[TeamsResponse](endpoint, 1, teams_per_page)
-      response.getNext() match {
-        case Some(page) =>
-          // TODO: This stops on the second page. This should be recursive
-          response.data ++ (doPaginatedRequest[TeamsResponse](
-            endpoint,
-            page,
-            teams_per_page
-          )).data
-        case None => response.data
+    def getAllData(
+        endpoint: String,
+        dataSoFar: ListOfTeams = Nil,
+        page: Option[Int] = Some(1)
+    ): ListOfTeams = {
+      page match {
+        case None => dataSoFar
+        case Some(value) =>
+          val response =
+            doPaginatedRequest[TeamsResponse](endpoint, value, teams_per_page)
+          getAllData(endpoint, response.data ++ dataSoFar, response.getNext())
       }
     }
 
@@ -147,19 +146,28 @@ object EndpointResponse {
   def getGames(season: Int, teams: List[Int]): ListOfGames = {
     implicit val gamesResponseR: Reader[GamesResponse] = macroR[GamesResponse]
 
-    def getAllData(endpoint: String, params: Map[String, Any]) = {
-      val response =
-        doPaginatedRequest[GamesResponse](endpoint, 1, games_per_page, params)
-      response.getNext() match {
-        case Some(page) =>
-          // TODO: This stops on the second page. This should be recursive
-          response.data ++ (doPaginatedRequest[GamesResponse](
+    def getAllData(
+        endpoint: String,
+        params: Map[String, Any],
+        dataSoFar: ListOfGames = Nil,
+        page: Option[Int] = Some(1)
+    ): ListOfGames = {
+      page match {
+        case None => dataSoFar
+        case Some(value) =>
+          val response =
+            doPaginatedRequest[GamesResponse](
+              endpoint,
+              value,
+              teams_per_page,
+              params
+            )
+          getAllData(
             endpoint,
-            page,
-            games_per_page,
-            params
-          )).data
-        case None => response.data
+            params,
+            response.data ++ dataSoFar,
+            response.getNext()
+          )
       }
     }