Hi,

I just started playing around with Scala and Lift and with @dpp's help wrote
a simple RESTful web service.
I wanted to share the code with this group and get feedback and suggestions
about improvements.
See below.
Thanks.

Chris


In Boot.scala you register one or more WsEndpoints (a trait that is defined
later).

I was wondering whether Lift has a way to search and register them
automatically?

class Boot {

  def boot {

    val x :List[WsEndpoint] = List(ExampleApi)
    x.foreach ( endpoint => LiftRules.dispatch.append
(endpoint.dispatchRules)  )

  }

}

----

Here is the class that defines methods that take parameters from the HTTP
request and returns XML nodes.

class ExampleApi {

     def doGet(id: String) : Node = {
       <info3 id={id} />
      }

}

The companion object sets up the mapping rules, e.g. /api/item/1 ->
exampleApi.doGet(1)

object ExampleApi extends WsEndpoint {

    val exampleApi = new ExampleApi()

    override def wsDispatchRules  =
        {
          case Req("api" :: "item" :: id :: _, _, GetRequest) => () =>
exampleApi.doGet(id)
        }
}

WsEndpoint is a trait that handles the conversion from what Lift expects,
i.e. () => Full[XmlResponse] to what is written above, i.e .() => Node

trait WsEndpoint {

    def wsDispatchRules : PartialFunction[Req,() => Node]

    def dispatchRules : PartialFunction[Req,() => Full[XmlResponse]] = {
      new MyAdapter(wsDispatchRules)
    }


    abstract class PartialFunctionAdapter [F, T1, T2] (adaptee:
PartialFunction[F, T1]) extends PartialFunction[F, T2] {
      override def isDefinedAt(r : F) = adaptee.isDefinedAt(r)

      override def apply(r : F)  = {
        converter(adaptee.apply(r))
      }

      def converter (x : T1) : T2

    }

    class MyAdapter(adaptee: PartialFunction[Req, () => Node])
        extends PartialFunctionAdapter[Req, () => Node, () =>
Full[XmlResponse] ](adaptee)   {

       override def converter (x : () => Node)  = {
        () => Full(XmlResponse(x()))
       }
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to