lila/app/controllers/Api.scala

155 lines
4.9 KiB
Scala
Raw Normal View History

2013-12-30 19:00:56 -07:00
package controllers
2015-01-17 04:35:54 -07:00
import play.api.libs.json._
2013-12-30 19:00:56 -07:00
import play.api.mvc._, Results._
import scala.concurrent.duration._
2013-12-30 19:00:56 -07:00
import lila.app._
import lila.common.HTTPRequest
2013-12-30 19:00:56 -07:00
object Api extends LilaController {
2014-01-07 18:43:20 -07:00
private val userApi = Env.api.userApi
private val gameApi = Env.api.gameApi
2016-08-02 04:43:13 -06:00
private lazy val apiStatusResponse = {
val api = lila.api.Mobile.Api
2015-01-17 04:35:54 -07:00
Ok(Json.obj(
"api" -> Json.obj(
2016-07-15 11:41:48 -06:00
"current" -> api.currentVersion.value,
"olds" -> api.oldVersions.map { old =>
Json.obj(
2016-07-15 11:41:48 -06:00
"version" -> old.version.value,
"deprecatedAt" -> old.deprecatedAt,
"unsupportedAt" -> old.unsupportedAt)
2016-04-08 23:30:49 -06:00
})
2015-01-17 04:35:54 -07:00
)) as JSON
}
2016-08-02 04:43:13 -06:00
val status = Action { req =>
apiStatusResponse
}
2016-08-02 04:40:10 -06:00
def user(name: String) = ApiRequest { implicit ctx =>
userApi one name map toApiResult
2013-12-30 19:00:56 -07:00
}
2014-01-07 18:43:20 -07:00
private val UsersRateLimitGlobal = new lila.memo.RateLimit(
credits = 1000,
duration = 1 minute,
name = "team users API global")
private val UsersRateLimitPerIP = new lila.memo.RateLimit(
credits = 1000,
duration = 10 minutes,
name = "team users API per IP")
2016-08-02 04:40:10 -06:00
def users = ApiRequest { implicit ctx =>
2016-08-31 05:00:39 -06:00
val page = (getInt("page") | 1) atLeast 1 atMost 50
val nb = (getInt("nb") | 10) atLeast 1 atMost 50
val cost = page * nb + 10
val ip = HTTPRequest lastRemoteAddress ctx.req
implicit val default = ornicar.scalalib.Zero.instance[ApiResult](Limited)
UsersRateLimitPerIP(ip, cost = cost, msg = ip) {
UsersRateLimitGlobal("-", cost = cost, msg = ip) {
2016-08-11 16:46:05 -06:00
lila.mon.api.teamUsers.cost(cost)
(get("team") ?? Env.team.api.team).flatMap {
_ ?? { team =>
Env.team.pager(team, page, nb) map userApi.pager map some
}
} map toApiResult
2016-08-11 16:32:39 -06:00
}
}
2014-01-08 17:06:20 -07:00
}
private val GamesRateLimitPerIP = new lila.memo.RateLimit(
credits = 10 * 1000,
duration = 10 minutes,
name = "user games API per IP")
private val GamesRateLimitPerUA = new lila.memo.RateLimit(
credits = 10 * 1000,
2016-08-01 07:11:18 -06:00
duration = 5 minutes,
name = "user games API per UA")
private val GamesRateLimitGlobal = new lila.memo.RateLimit(
credits = 10 * 1000,
duration = 1 minute,
name = "user games API global")
2016-07-08 17:32:54 -06:00
2016-08-02 04:40:10 -06:00
def userGames(name: String) = ApiRequest { implicit ctx =>
2016-08-31 05:00:39 -06:00
val page = (getInt("page") | 1) atLeast 1 atMost 200
val nb = (getInt("nb") | 10) atLeast 1 atMost 100
val cost = page * nb + 10
2016-08-02 03:00:41 -06:00
val ip = HTTPRequest lastRemoteAddress ctx.req
2016-08-02 04:40:10 -06:00
implicit val default = ornicar.scalalib.Zero.instance[ApiResult](Limited)
2016-08-02 03:00:41 -06:00
GamesRateLimitPerIP(ip, cost = cost, msg = ip) {
GamesRateLimitPerUA(~HTTPRequest.userAgent(ctx.req), cost = cost, msg = ip) {
GamesRateLimitGlobal("-", cost = cost, msg = ip) {
2016-08-11 16:46:05 -06:00
lila.mon.api.userGames.cost(cost)
lila.user.UserRepo named name flatMap {
_ ?? { user =>
gameApi.byUser(
user = user,
rated = getBoolOpt("rated"),
playing = getBoolOpt("playing"),
analysed = getBoolOpt("analysed"),
withAnalysis = getBool("with_analysis"),
withMoves = getBool("with_moves"),
withOpening = getBool("with_opening"),
withMoveTimes = getBool("with_movetimes"),
token = get("token"),
nb = nb,
page = page
) map some
}
2016-08-02 04:40:10 -06:00
} map toApiResult
2016-07-08 17:32:54 -06:00
}
2016-01-21 23:45:07 -07:00
}
}
2013-12-30 19:00:56 -07:00
}
2016-08-02 04:40:10 -06:00
def game(id: String) = ApiRequest { implicit ctx =>
2016-08-11 16:46:05 -06:00
lila.mon.api.game.cost(1)
gameApi.one(
id = id take lila.game.Game.gameIdSize,
withAnalysis = getBool("with_analysis"),
withMoves = getBool("with_moves"),
withOpening = getBool("with_opening"),
withFens = getBool("with_fens"),
withMoveTimes = getBool("with_movetimes"),
2016-08-02 04:40:10 -06:00
token = get("token")) map toApiResult
2014-06-06 03:08:43 -06:00
}
def currentTournaments = ApiRequest { implicit ctx =>
Env.tournament.api.fetchVisibleTournaments map
Env.tournament.scheduleJsonView.apply map Data.apply
}
def tournament(id: String) = ApiRequest { implicit ctx =>
2016-08-31 05:00:39 -06:00
val page = (getInt("page") | 1) atLeast 1 atMost 200
lila.tournament.TournamentRepo byId id flatMap {
_ ?? { tour =>
Env.tournament.jsonView(tour, page.some, none, none, none) map some
}
} map toApiResult
}
2016-08-02 04:40:10 -06:00
sealed trait ApiResult
case class Data(json: JsValue) extends ApiResult
case object NoData extends ApiResult
case object Limited extends ApiResult
def toApiResult(json: Option[JsValue]) = json.fold[ApiResult](NoData)(Data.apply)
private def ApiRequest(js: lila.api.Context => Fu[ApiResult]) = Open { implicit ctx =>
js(ctx) map {
2016-08-02 04:40:10 -06:00
case Limited => TooManyRequest(jsonError("Try again later"))
case NoData => NotFound
case Data(json) => get("callback") match {
2014-02-17 02:12:19 -07:00
case None => Ok(json) as JSON
case Some(callback) => Ok(s"$callback($json)") as JAVASCRIPT
2013-12-31 03:45:37 -07:00
}
2013-12-30 19:00:56 -07:00
}
}
}