summaryrefslogtreecommitdiff
path: root/cask/src/cask/endpoints/JsonEndpoint.scala
blob: 80fac9aaacd714f3927d0e5752e6d6510b6ea6e7 (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
package cask.endpoints

import cask.internal.Router
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) =
    ujson.read(new String(ctx.exchange.getInputStream.readAllBytes())).obj.toMap
  def wrapPathSegment(s: String): Input = ujson.Js.Str(s)
}