[Lift] Re: RESTful JSON server that supports jQuery or JSONP callback

2009-10-07 Thread opyate

Hey guys,

I managed to build in callback support, and I draw your attention to
the implementation here:
http://github.com/opyate/Ken/blob/master/ken-server/src/main/scala/com/opyate/ken/lib/API.scala

Excerpt:

snip
override def dispatch: LiftRules.DispatchPF = {
// modify the returned function to one which converts the result
to JSON
// or a pure LiftResponse (usefull when HTTP response code is not
200)
dispatchMethod.andThen(x =
  {
val unboxed = unbox(x)
unboxed match {
  case l:LiftResponse = { () = Full(l) }
  case _ = { () = {
if (S.param(callback).isEmpty) {
  Full(
JsonResponse(
  jsonAttributes(
unbox(x)
  )
)
  )
} else {
  Full(
JavaScriptResponse(
  JE.JsFunc(S.param(callback).open_!,
jsonAttributes(
  unbox(x)
)
  ).cmd
)
  )
}
}
  }
}
  }
)
  }
/snip

So, basically it will handle any callback function name you give it,
because it will just echo it back in the response. (default for jQuery
JSONP is C).

Please be very critical of my code: I'm not doing things properly, or
in a particularly functional manner, but I'm learning :-)

Shameless plug: another little Scala/Lift project of mine which I'd
like feedback on:
http://wiki.github.com/opyate/yauser

Thanks, and happy coding!
Juan


On Oct 6, 6:23 pm, opyate opy...@gmail.com wrote:
 Sorry - APIResponse code not borrowed from skittr, but ESME (http://
 incubator.apache.org/esme/)
 Thus, depending on your call's extension (.json or .xml) you can serve
 the response in the required format.

 Anyhoo, back to the question... :-)

 Thanks,
 Juan

 On Oct 6, 5:48 pm, Juan M Uys opy...@gmail.com wrote:



  Hi Lifters,

  I'm building a REST server and a separate stand-alone DHTML client
  which won't necessarily be hosted on the same server. Some browsers do
  not support cross site Ajax, henceJSONP(i.e. callback support).

  My DHTML client now makes calls like the following, using jQuery:

  snip
  GET /api/version.json?callback=C_1254838856015= HTTP/1.1
  Host: localhost:8080
  User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US;
  rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
  Accept: */*
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip,deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Keep-Alive: 300
  Connection: keep-alive
  Cookie: JSESSIONID=13k8ykwq2u2w6
  /snip

  I need to know if I need to build manual support in for the callback:
  * grab the token 'C'
  * generate a JsCmd named 'C' with my JSON response as parameter (It
  will be great if someone can suggest which parts of the Lift API to
  use for this)
  * send it back to the client (as an extra parameter to the original
  JSON, or on it's own?)

  It doesn't seem like Lift automatically picks up on ?callback= (it's
  not a standard, so - understandable), and my response looks like this:

  snip
  HTTP/1.1 200 OK
  Content-Length: 16
  Content-Type: application/json
  X-Lift-Version: 1.1-SNAPSHOT
  Server: Jetty(6.1.21)

  {version: 1}
  /snip

  The above response is generated by:

  snip
  def version(): Box[APIResponse] = {
      Full(
        Right(
          Map(version -
            Map(version - APIHelper.getParam(api.version))
          )
        )
      )
    }
  /snip

  ...where APIResponse is borrowed from TwitterResponse (from the skittr 
  example).

  Another problem I have is that Firefox complains about invalid label
  for the above response, so I think wrapping it up in a callback will
  fix it.

  Here's an answer related to PHP for this 
  issue:http://stackoverflow.com/questions/790910/jquery-getjson-to-external-...

  Thanks,

  --
  juan
  +44 7702 783 956

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Lift] Re: RESTful JSON server that supports jQuery or JSONP callback

2009-10-06 Thread opyate

Sorry - APIResponse code not borrowed from skittr, but ESME (http://
incubator.apache.org/esme/)
Thus, depending on your call's extension (.json or .xml) you can serve
the response in the required format.

Anyhoo, back to the question... :-)

Thanks,
Juan

On Oct 6, 5:48 pm, Juan M Uys opy...@gmail.com wrote:
 Hi Lifters,

 I'm building a REST server and a separate stand-alone DHTML client
 which won't necessarily be hosted on the same server. Some browsers do
 not support cross site Ajax, hence JSONP (i.e. callback support).

 My DHTML client now makes calls like the following, using jQuery:

 snip
 GET /api/version.json?callback=C_1254838856015= HTTP/1.1
 Host: localhost:8080
 User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US;
 rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
 Accept: */*
 Accept-Language: en-us,en;q=0.5
 Accept-Encoding: gzip,deflate
 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
 Keep-Alive: 300
 Connection: keep-alive
 Cookie: JSESSIONID=13k8ykwq2u2w6
 /snip

 I need to know if I need to build manual support in for the callback:
 * grab the token 'C'
 * generate a JsCmd named 'C' with my JSON response as parameter (It
 will be great if someone can suggest which parts of the Lift API to
 use for this)
 * send it back to the client (as an extra parameter to the original
 JSON, or on it's own?)

 It doesn't seem like Lift automatically picks up on ?callback= (it's
 not a standard, so - understandable), and my response looks like this:

 snip
 HTTP/1.1 200 OK
 Content-Length: 16
 Content-Type: application/json
 X-Lift-Version: 1.1-SNAPSHOT
 Server: Jetty(6.1.21)

 {version: 1}
 /snip

 The above response is generated by:

 snip
 def version(): Box[APIResponse] = {
     Full(
       Right(
         Map(version -
           Map(version - APIHelper.getParam(api.version))
         )
       )
     )
   }
 /snip

 ...where APIResponse is borrowed from TwitterResponse (from the skittr 
 example).

 Another problem I have is that Firefox complains about invalid label
 for the above response, so I think wrapping it up in a callback will
 fix it.

 Here's an answer related to PHP for this 
 issue:http://stackoverflow.com/questions/790910/jquery-getjson-to-external-...

 Thanks,

 --
 juan
 +44 7702 783 956

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---