REST API for Yahoo Web Search
Inspired by Dave Orchard's effort to represent Yahoo Web Search in WSDL 2.0, i was thinking about how we could support codegen in Axis2.0 for REST-style services. Before we can do that we need a REST-style API that's easy to use. So i ping-ed our REST guru, Mr. Mark Baker. Here's an API that we came up with.
Step #1: Use scomp in xmlbeans to generate code from the xsd. Let's assume that it generates com.yahoo.search.api.ResultSet class
Step #2: Write sample code to invoke the "webSearch"
import com.yahoo.search.api.ResultSet;
try {
Message m = new Message("");
m.addParam("appid","YahooDemo");
m.addParam("query","madonna");
m.addParam("results","2");
m.setResultParam(ResultSet.class);
ResultSet set = (ResultSet) m.invoke("GET");
} catch (HTTPClientException e){
System.out.println("You did something wrong: " + e.getCode());
System.out.println("Error Message:" + m.getResponse().xpath("/Error/Message"));
} catch (HTTPServerException e){
System.out.println("The server did something wrong: " + e.getCode());
System.out.println("Error Message:" + m.getResponse().xpath("/Error/Message"));
}
Notes:
- 4xx HTTP error codes are translated to HTTPClientException
- 5xx HTTP error codes are translated to HTTPServerException
- Yahoo Error Message details are here.
- If the developer just needs tiny bit from the response and does not want to do databinding, they can just use xpath to extract the relevant portion using xpath. (similar to how we extract the error message)
Does this make sense? Comments? Concerns?
Great stuff! I'm looking forward to publish REST services in Geronimo for AJAX clients.
Posted by: Miguel A Paraz | October 13, 2005 11:53 PM