add user JSON API
parent
69a62bea05
commit
25d1ce60f5
10
README.md
10
README.md
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue