add user JSON API

pull/83/head
Thibault Duplessis 2013-12-31 03:00:56 +01:00
parent 69a62bea05
commit 25d1ce60f5
5 changed files with 52 additions and 2 deletions

View File

@ -68,6 +68,16 @@ sbt play -Dhttp.port=9663
From here you can now run the application (`run`).
## API
### GET user infos
> /api/user/<username>
```
> curl en.lichess.org/api/user/thibault
```
### Read the move stream
Lichess streams all played moves on http://en.lichess.org/stream using chunked HTTP response and the following format:

View File

@ -0,0 +1,35 @@
package controllers
import play.api.libs.json._
import play.api.mvc._, Results._
import lila.app._
import lila.user.{ UserRepo, User UserModel, Perf, Perfs }
object Api extends LilaController {
private implicit val perfWrites: Writes[Perf] = Writes { o
Json.obj(
"nbGames" -> o.nb,
"rating" -> o.glicko.rating.toInt,
"deviation" -> o.glicko.deviation.toInt)
}
private implicit val perfsWrites: Writes[Perfs] = Writes { o
JsObject(o.perfs map {
case (name, perf) name -> perfWrites.writes(perf)
})
}
private implicit val userWrites: Writes[UserModel] = Writes { u
Json.obj(
"username" -> u.username,
"perfs" -> u.perfs,
"progress" -> u.progress)
}
def user(username: String) = Action.async { req
UserRepo named username map {
case None NotFound
case Some(u) Ok(Json toJson u) as JSON
}
}
}

View File

@ -108,7 +108,7 @@ private[controllers] trait LilaController
}
protected def JsonOptionFuOk[A, B: Writes](fua: Fu[Option[A]])(op: A Fu[B])(implicit ctx: Context) =
fua flatMap { _.fold(notFound(ctx))(a op(a) map { b => Ok(Json toJson b) as JSON }) }
fua flatMap { _.fold(notFound(ctx))(a op(a) map { b Ok(Json toJson b) as JSON }) }
protected def JsOk(fua: Fu[String], headers: (String, String)*) =
fua map { a Ok(a) as JAVASCRIPT withHeaders (headers: _*) }
@ -148,7 +148,7 @@ private[controllers] trait LilaController
protected def OptionFuResult[A](fua: Fu[Option[A]])(op: A Fu[SimpleResult])(implicit ctx: Context) =
fua flatMap { _.fold(notFound(ctx))(a op(a)) }
protected def notFound(implicit ctx: Context): Fu[SimpleResult] =
protected def notFound(implicit ctx: Context): Fu[SimpleResult] =
if (HTTPRequest isSynchronousHttp ctx.req) Lobby renderHome Results.NotFound
else Results.NotFound("resource not found").fuccess

View File

@ -208,6 +208,9 @@ GET /report/thanks controllers.Report.thanks
GET /report/list controllers.Report.list
POST /report/:id/process controllers.Report.process(id: String)
# API
GET /api/user/:id controllers.Api.user(id: String)
# Misc
POST /cli controllers.Cli.command
GET /captcha/$id<[\w]{8}> controllers.Main.captchaCheck(id: String)

2
todo
View File

@ -102,3 +102,5 @@ replay games using move time
antiboost tool for mods
list all players by ranking.
full page recent forum posts
denormalized game counts are fucked up http://en.lichess.org/@/awwaey/all
chrome missing gradients on setup buttons