theenglishway (time) 2 роки тому
батько
коміт
b2c9cacdde
1 змінених файлів з 42 додано та 7 видалено
  1. 42 7
      src/main/scala/BallDontLie.scala

+ 42 - 7
src/main/scala/BallDontLie.scala

@@ -103,7 +103,12 @@ case class EndpointResponse[T](data: T, meta: ResponseMetadata) {
 
 object EndpointResponse {
   type ListOfTeams = List[Team]
+  type ListOfGames = List[GameData]
   type TeamsResponse = EndpointResponse[ListOfTeams]
+  type GamesResponse = EndpointResponse[ListOfGames]
+
+  val teams_per_page = 25
+  val games_per_page = 25
 
   def doPaginatedRequest[T: Reader](
       endpoint: String,
@@ -121,18 +126,16 @@ object EndpointResponse {
   def getTeams(): ListOfTeams = {
     implicit val teamsResponseR: Reader[TeamsResponse] = macroR[TeamsResponse]
 
-    def getAllData(
-        endpoint: String,
-        params: Map[String, Any] = Map()
-    ) = {
-      val response = doPaginatedRequest[TeamsResponse](endpoint, 1, 25, params)
+    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,
-            25,
-            params
+            teams_per_page
           )).data
         case None => response.data
       }
@@ -140,4 +143,36 @@ object EndpointResponse {
 
     getAllData("https://www.balldontlie.io/api/v1/teams")
   }
+
+  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](
+            endpoint,
+            page,
+            games_per_page,
+            params
+          )).data
+        case None => response.data
+      }
+    }
+
+    // Using an array as param to team_ids[] yields a 500 error so we make
+    // several requests instead
+    teams.flatMap(team => {
+      getAllData(
+        "https://www.balldontlie.io/api/v1/games",
+        Map(
+          "seasons[]" -> List(season).mkString(","),
+          "team_ids[]" -> team
+        )
+      )
+    })
+  }
 }