Skip to content

Instantly share code, notes, and snippets.

@rodrigopr
Last active August 29, 2015 14:11
Show Gist options
  • Save rodrigopr/ee2a190286ac9949ac5d to your computer and use it in GitHub Desktop.
Save rodrigopr/ee2a190286ac9949ac5d to your computer and use it in GitHub Desktop.

Instead of using TurnJsonIntoHttp in each place:

class MyEndpoint extends Endpoint[HttpRequest, HttpResponse] {
  def route = {
    case Method.Get -> Root / "items" 
      List() ! TurnJsonIntoHttp[List[MyObj]]

    case Method.Get -> Root / "item" / Long(id) 
      GetDetail(id) ! TurnJsonIntoHttp[MyObj]
    
    //...
  }
}

==========

We could do something like:

implicit class ToJsonOps[Req, Resp](service: Service[Req, Resp]) {
  def asJson(implicit encode: EncodeJson[Resp]) =
    new Service[Req, HttpResponse] {
      def apply(req: Req) = service(req) flatMap TurnJsonIntoHttp[Resp]
    }
}

And in route:

class MyEndpoint extends Endpoint[HttpRequest, HttpResponse] {
  def route = {
    case Method.Get -> Root / "items"  
      List().asJson

    case Method.Get -> Root / "item" / Long(id)  
      GetDetail(id).asJson
    
    //...
  }
}
@vkostyukov
Copy link

Sure! Please go ahead with submitting those two PR. Looking forward to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment