1 Reply Latest reply on Jun 17, 2009 9:20 PM by gesrat

    Data Services - REST-ful URLs and path substitution  parameters

    gesrat

      It is common for REST-full APIs (implemented on top of HTTPService) to require URL paths to contain substitution parameters.  For example, if my service end point is defined as 'http://...../rest/users/{id}/workspaces', then it would be nice if the generated service wrapper included a parameter for the substitution values - ie "function get_userWorkspaces(id:String) : AsyncToken "

        • 1. Re: Data Services - REST-ful URLs and path substitution  parameters
          gesrat Level 1

          I found a technique to achieve this using a custom Serializer for the Service Operation that overrides the "serializeURL" method.  However due to a bug (see SDK-21872 ) the technique doesnt work in FB4 beta 1.  See code snippet below...         

           

           

          package services.listeningpost

          {

          import com.adobe.serializers.xml.XMLSerializationFilter;

           

          import mx.rpc.http.Operation;


          public class ListeningPost extends _Super_ListeningPost

          {

           

          protected var pathParams:Object = [];

           

          public function setPathParam(key:String, value:String):void

          {

          pathParams[key] = value;

          }

           

           

          public function getPathParam(key:String):String

          {

          return pathParams[key];

          }

           

           

          private var _serializer:LPXMLSerializationFilter;

          public function ListeningPost()

          {

              _serializer = new LPXMLSerializationFilter(this);

             

              super;

              for each(var o:Operation in this.operations){

              if (o.serializationFilter is XMLSerializationFilter)

              o.serializationFilter = _serializer;

              }

          }

          }


          }

           

          // Private class below provides custom Serializer

          //

          import com.adobe.serializers.xml.XMLSerializationFilter;

          import mx.rpc.http.AbstractOperation;

          import services.listeningpost.ListeningPost;


          class LPXMLSerializationFilter extends XMLSerializationFilter{

           

          private var _service:ListeningPost;

           

          public function LPXMLSerializationFilter(service:ListeningPost)

          {

          super();

          _service = service;

          }

           

          override public function serializeURL(operation:AbstractOperation, obj:Object, url:String) : String

          {

          var useUrl:String = super.serializeURL(operation,obj,url);

          var keys:Array = useUrl.match(/\{([^\}]*)\}/g);

          for each(var s:String in keys){

          var r:String = _service.getPathParam(s);

          if (r != null)

          useUrl = useUrl.replace(s,r);

          }

          return useUrl;

          }

          }