summaryrefslogtreecommitdiff
path: root/cask/src/cask/endpoints/JsonEndpoint.scala
blob: e0d12571a41b533dc8a0896257c7829f8a4db15e (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
50
51
52
53
54
55
56
57
58
59
60
61
package cask.endpoints

import java.io.ByteArrayOutputStream

import cask.internal.{Router, Util}
import cask.main.Endpoint
import cask.model.{Request, Response}


sealed trait JsReader[T] extends Router.ArgReader[ujson.Value, T, cask.model.Request]
object JsReader{
  implicit def defaultJsReader[T: upickle.default.Reader] = new JsReader[T]{
    def arity = 1

    def read(ctx: cask.model.Request, label: String, input: ujson.Value): T = {
      val reader = implicitly[upickle.default.Reader[T]]
      upickle.default.read[T](input)(reader)
    }
  }

  implicit def paramReader[T: ParamReader] = new JsReader[T] {
    override def arity = 0

    override def read(ctx: cask.model.Request, label: String, v: ujson.Value) = {
      implicitly[ParamReader[T]].read(ctx, label, Nil)
    }
  }
}
class postJson(val path: String, override val subpath: Boolean = false) extends Endpoint{
  type Output = Response
  val methods = Seq("post")
  type Input = ujson.Js.Value
  type InputParser[T] = JsReader[T]
  def wrapFunction(ctx: Request,
                       delegate: Map[String, Input] => Router.Result[Output]): Router.Result[Response] = {
    val obj = for{
      str <-
        try {
          val boas = new ByteArrayOutputStream()
          Util.transferTo(ctx.exchange.getInputStream, boas)
          Right(new String(boas.toByteArray))
        }
        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
    obj match{
      case Left(r) => Router.Result.Success(r)
      case Right(params) => delegate(params)
    }
  }
  def wrapPathSegment(s: String): Input = ujson.Js.Str(s)
}