Showing posts with label rest. Show all posts
Showing posts with label rest. Show all posts

Monday, August 18, 2008

PURE + AJAX + REST + JSON Web Framework

I am imagining a web framework that delivered static HTML which made data requests to the server using AJAX to REST services that returned JSON, which were mapped into the HTML using PURE.

I'm curious to see what working in that kind of structure would be like. Seems very tightly compartmentalized, quite possibly in a good way.

In particular, I'm not totally sold on the idea that interleaving application data with HTML on the server-side is the right approach - sending down a clean, cacheable template of HTML (possibly still generated on the server-side to put components together and so forth), and then sending down a really thin data representation and having the client interleave the two seems appealing, and I haven't yet worked with a web framework that goes in that direction.

Tuesday, April 1, 2008

JAX-RS Momentum

It's great to see JAX-RS picking up momentum. I've done work in Restlet, and I'd like to spend some time in Jersey, so it's great to see that I'll have more choices next time.

Next comes tool support, integration, and then ... the world. Although, to be honest, I'd be happier with web services and remoting if I felt that they were consistently being used appropriately. I still find that people consider remoting when they don't need it -- too busy looking for the long-term flexibility and not enough time actually building the immediately necessary functionality.

Thursday, February 7, 2008

REST and SOAP and the Right Tool for the Job

I don't disagree with netzooid's conclusions:

Please keep in mind that REST and SOAP programming models don’t mix well. There is this complete fallacy going around (I succumbed to it too at one point) that you can use the same service class or description to build a RESTful and a SOAP based service. However, the interactions and mappings should end up being completely different from one to the other! One is message based and one is resource based. You’re going to have to design your service differently as they’re two completely different beasts.
I largely agree with this. What I will say is that if you've already got a REST or a SOAP API and you have the capability to expose that API through the other technology, it's often possible to do so. While this might not truly be a REST or SOAP api, it's close enough for many people.

That said, the natural approach for REST and the natural approach for SOAP are quite different, and to do a good job of both, you'll find that your services end up being quite different, even if they're layered on top of the same basic infrastructure.

I've used Restlet, and I'm relatively happy with it. It's still maturing, but it's basically ready-to-go. I've been watching Jersey interestedly, and it seems to be shaping up well, but there's always time for the JCP to mess it all up before a final release. I've also used Enunciate, which is capable of generating both SOAP and REST. It's an interesting concept, although I do believe it needs more time to be mature enough to recommend.

Thursday, August 30, 2007

Rails: Resource Links in Resource List

Navigating a REST application should not require a deep understanding of the namespace of the application's addressable resources. As much as possible, one REST query should lead to another.

The most obvious case of this is when one has a resource that is a list or collection (e.g. /customers) of the available resources (e.g. /customers/1), e.g.:


<customers>
<customer>
<name>The Customer</name>
<a href="myserver/customers/1.xml" />
</customer>
</customers>


And yet, it's surprisingly difficult to put these kind of links into Rails resources, particularly if you use the approach that resource_scaffold suggests.

Resource scaffold generates the XML representation of the collection as by calling the to_xml method that Rails adds to Array, which in turn calls to_xml on the model classes, something like this:

@customers = Customers.find(:all)
render :xml => @customers.to_xml


In order to add links to the XML representation, you'd need two pieces of information: the URL structure for resource and the id of the resource. The ActionController has the most understanding of the URL structure (as it can use
url_for and named resource helpers like customer_path), but the model class knows its id and is responsible for generating the XML.

This awkward split in the available information makes adding these links to the XML somewhat painful; perhaps no more painful than building the XML by hand would normally be, but it's a departure from the usual ease of accomplishing common tasks in Rails.

After trying a number of alternatives, we settled on approach. In the controller, we do:

# GET /customers.xml
def index
@customers = Customer.find(:all)
respond_to do |format|
format.xml do
render :xml => @customers.to_xml( :format => :summary, :base=>url_for(:controller=>'customers') )
end
end
end

# GET /customers/1.xml
def show
@customer = Customer.find(params[:id])
respond_to do |format|
format.xml { render :xml => @customer.to_xml( :format => :full ) }
end
rescue ActiveRecord::RecordNotFound
head 404
end


This relies on some model code, like this:

def to_xml( options = {} )
case options[:format]
when :summary
resource_url = proc { |options| options[:builder].a(:href=>"#{options[:base]}/#{id}.xml") }
super( options.merge!( :only => [:id, :name], :procs => [resource_url] ) )
when :full
super( options.merge!( :include => [ :main_contact_info, :billing_contact_info ],
:except => [ :main_contact_id, :billing_contact_id ] ) )
else
super( options )
end
end


It's still a little awkward, and I'm hopeful that we'll find a better alternative, but it does the job and is less ugly than some of the other alternatives we considered.

The only other option we looked at that seems reasonable is to generate all the XML within the controller, which means reproducing the work done by Array.to_xml, but is otherwise relatively sensible.