|
|
@@ -3,8 +3,7 @@ package balldontlie
|
|
|
import requests._
|
|
|
import upickle.default._
|
|
|
import game._
|
|
|
-import os.Path
|
|
|
-import scala.util.{Try, Success, Failure}
|
|
|
+import utils.Utils
|
|
|
|
|
|
case class ResponseMetadata(
|
|
|
total_pages: Int,
|
|
|
@@ -18,52 +17,6 @@ object ResponseMetadata {
|
|
|
macroR[ResponseMetadata]
|
|
|
}
|
|
|
|
|
|
-object Utils {
|
|
|
- def get_and_decode[T: Reader](
|
|
|
- endpoint: String,
|
|
|
- params: List[(String, Any)] = List()
|
|
|
- ): T = {
|
|
|
- // Try to be a good web citizen and not flood the server with rapid requests
|
|
|
- Thread.sleep(100)
|
|
|
-
|
|
|
- val r = Try(
|
|
|
- requests.get(
|
|
|
- endpoint,
|
|
|
- params = params.map({ case (k, v) => (k, v.toString()) })
|
|
|
- )
|
|
|
- )
|
|
|
-
|
|
|
- r match {
|
|
|
- case Success(response) =>
|
|
|
- println(endpoint, params)
|
|
|
-
|
|
|
- val json = ujson.read(response.text())
|
|
|
- upickle.default.read[T](json)
|
|
|
-
|
|
|
- case Failure(ex) =>
|
|
|
- ex match {
|
|
|
- case exc: requests.RequestFailedException
|
|
|
- if exc.response.statusCode == 429 => {
|
|
|
- println("Got 429 error, retrying after 1s")
|
|
|
- Thread.sleep(1000)
|
|
|
- get_and_decode[T](endpoint, params)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- def writeToFile[T: Writer](data: T, file: Path): Unit = {
|
|
|
- val string = upickle.default.write[T](data, indent = 4)
|
|
|
-
|
|
|
- os.write.over(file, string)
|
|
|
- }
|
|
|
-
|
|
|
- def readFromFile[T: Reader](file: Path): T = {
|
|
|
- val string = os.read(file)
|
|
|
- upickle.default.read[T](string)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
case class EndpointResponse[T](data: T, meta: ResponseMetadata) {
|
|
|
type Params = Iterable[(String, String)]
|
|
|
|