blob: d9d39c3888b98c47642dfbb71cc4d5fb739d84ac (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package cask.endpoints
import cask.internal.{Router, Util}
import cask.internal.Router.EntryPoint
import cask.main.Routes
import cask.model.{ParamContext, Response}
sealed trait JsReader[T] extends Router.ArgReader[ujson.Js.Value, T, cask.model.ParamContext]
object JsReader{
implicit def defaultJsReader[T: upickle.default.Reader] = new JsReader[T]{
def arity = 1
def read(ctx: cask.model.ParamContext, label: String, input: ujson.Js.Value): T = {
implicitly[upickle.default.Reader[T]].apply(input)
}
}
implicit def paramReader[T: ParamReader] = new JsReader[T] {
override def arity = 0
override def read(ctx: cask.model.ParamContext, label: String, v: ujson.Js.Value) = {
implicitly[ParamReader[T]].read(ctx, label, Nil)
}
}
}
class postJson(val path: String, override val subpath: Boolean = false) extends Routes.Endpoint[Response]{
val methods = Seq("post")
type Input = ujson.Js.Value
type InputParser[T] = JsReader[T]
def getRawParams(ctx: ParamContext) = {
for{
str <-
try Right(new String(ctx.exchange.getInputStream.readAllBytes()))
catch{case e: Throwable => Left(cask.model.Response(
"Unable to deserialize input JSON text: " + e + "\n" + Util.stackTraceString(e)
))}
json <-
try Right(ujson.read(str))
catch{case e: Throwable => Left(cask.model.Response(
"Input text is invalid JSON: " + e + "\n" + Util.stackTraceString(e)
))}
obj <-
try Right(json.obj)
catch {case e: Throwable => Left(cask.model.Response("Input JSON must be a dictionary"))}
} yield obj.toMap
}
def wrapPathSegment(s: String): Input = ujson.Js.Str(s)
}
|