trait XMLApiHelper extends AnyRef
Mix this trait into your REST service provider to convert between different response types and a LiftResponse. You need to define the createTag method to provide a root element for your API. You may optionally override the successAttrName, operationAttrName, and/or msgAttrName defs to control the attributes that will be applied to your root element based on the return from your API.
For example, the following code implements a simple API that takes a comma- separated string of integers and reduces them with various operations.
object CalculatorApi extends XmlApiHelper { // Define our root tag def createTag(contents : NodeSeq) : Elem = <api>{contents}</api> // The LiftResponses here will be converted to Box[LiftResponse] // via the putResponseInBox implicit conversion def calculator : LiftRules.DispatchPF = { case r @ Req(List("api","sum"), _, GetRequest) => () => doSum(r) case r @ Req(List("api","product"), _, GetRequest) => () => doProduct(r) case r @ Req(List("api","max"), _, GetRequest) => () => doMax(r) case r @ Req(List("api","min"), _, GetRequest) => () => doMin(r) case Req("api" :: _, _, _) => () => BadResponse() } // Define a common handler def reduceOp (operation : (Int,Int) => Int)(r : Req) : Box[Elem] = tryo { (r.param("args").map { args => <result>{args.split(",").map(_.toInt).reduceLeft(operation)}</result> }) ?~ "Missing args" } match { case Full(x) => x case f : Failure => f case Empty => Empty } // Using a return type of LiftResponse causes the canNodeToResponse // implicit to be invoked def doSum (r : Req) : LiftResponse = reduceOp(_ + _)(r) def doProduct (r : Req) : LiftResponse = reduceOp(_ * _)(r) def doMax (r : Req) : LiftResponse = reduceOp(_ max _)(r) def doMin (r : Req) : LiftResponse = reduceOp(_ min _)(r) }
With this API, the URL
http://foo.com/api/sum?args=1,2,3,4,5would return
<api operation="sum" success="true"><result>15</result></api>
http://foo.com/api/sum?args=1,2,3,4,5 return
<api operation="sum" success="true"><result>15</result></api>
- Alphabetic
- By Inheritance
- XMLApiHelper
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
-
abstract
def
createTag(in: NodeSeq): Elem
The method that wraps the outer-most tag around the body.
The method that wraps the outer-most tag around the body. The success, operation and msg attributes will be merged into the returned Elem.
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
implicit
def
boolToResponse(in: Boolean): LiftResponse
Converts a boolean into a response of a root element with no contents and the "success" attribute set to the value of the "in" parameter.
-
def
buildResponse(success: Boolean, msg: Box[NodeSeq], body: NodeSeq): LiftResponse
Build the Response based on Success, an optional message and the body
Build the Response based on Success, an optional message and the body
- Attributes
- protected
-
implicit
def
canBoolToResponse(in: Box[Boolean]): LiftResponse
Converts a boxed boolean into a response of a root element with no contents and the "success" attribute set to the value of the "in" parameter.
Converts a boxed boolean into a response of a root element with no contents and the "success" attribute set to the value of the "in" parameter. If the Box is a Failure, the "msg" attribute of the root element will be set to the Failure's msg value.
-
implicit
def
canNodeToResponse(in: Box[Seq[Node]]): LiftResponse
Converts a boxed Seq[Node] into a response.
Converts a boxed Seq[Node] into a response. If the Box is a Full, the root element uses the contents of the Box as its contents, and sets the "success" attribute to "true". If the Box is a Failure, the "success" attribute is set to "false" and the "msg" attribute is set to the Failure's msg value. If the Box is Empty then the root element is returned with no contents and the "success" attribute set to "false".
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
implicit
def
listElemToResponse(in: Seq[Node]): LiftResponse
Converts a Seq[Node] into a root element with the "success" attribute set to "true" and the Seq[Node] as the contents.
-
def
msgAttrName: String
The name for the msg attribute
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
operation: Option[NodeSeq]
Determines the value to place in the "operation" attribute of the root element based on the second element of the request path.
Determines the value to place in the "operation" attribute of the root element based on the second element of the request path.
- Attributes
- protected
-
def
operationAttrName: String
The name for the operation attribue
-
implicit
def
pairToResponse(in: (Boolean, String)): LiftResponse
Converts a pair of (Boolean,String) into a response of a root element with no contents, the "success" attribute set to the value of the first element of the pair, and the "msg" attribute set to the value of the second element of the pair.
-
implicit
def
putResponseInBox(in: LiftResponse): Box[LiftResponse]
Converts a given LiftResponse into a Full[LiftResponse]
-
def
successAttrName: String
The name for the success attribute
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )