Most of the client features can be composed during the request building phase. This means that when invoking Restfulie.at() one can specify which features are supposed to be used and which can be left out, allowing an infinite number of combinations without the need to write extra classes.
Another characteristic of REST over HTTP is that some requests can be easily retried by adding a new feature to the DSL. The RetryWhenUnavailable feature demonstrates how to use it when the server returns 503. The following code will retry the operation once by using this feature:
Response response = Restfulie.at("uri").retryWhenUnavailable().get();
It is easy to create your own feature, even with configuration parameters, simply create a class and implemente the interface RequestFeature
public class MyFatureRequest implement RequestFeature { public Response process(RequestChain chain, Request request, String verb, URI uri, Object payload) { //do something before the request is dispatched Response response = chain.next(request, verb, uri, payload); //do someething after the request have been dispatched return response; } }
After implementing it you just have to pass it so restfulie add it at the stack of features.
//the method features can receive more than one feature Restfulie.at("resource_uri").withFeature(MyFeatureRequest.class).get();
A common asked feature is to throw na error in case some specific response codes are returned. There is a sample feature that can be tweaked as required, that raises an exception when 300+ codes are returned. It's name is "ThrowError" and therefore to load it one only has to invoke it in the DSL:
Response response = Restfulie.at(uri).throwError().get();
The history feature gives access to the history of executed requests, so one can re-execute it or customize it prior to sending the request again. To add it, simply load it into your DSL:
Response response = Restfulie.at(uri).history().get(); # sends the request again response.getRequestHistory().get(0);
The ConnegWhenUnaccepted feature retry an request when the server response with the 406("unaccepted media type") code, the new request will be sent with another media type supported by the server(throug Accept) if the client is able to generate it.
Product product = Restfulie.at("http://localhost:3000/product/2").connegWhenUnaccepted().get();
It is easy to create your own feature, even with configuration parameters, simply create a class and implement the interface ResponseFeature
public class MyFature implement ResponseFeature { public Response process(ResponseChain responseChain, Response response) { Response reponse = responseChain.next(response); return response; } }
And then pass it so restfulie add it at the stack of features.
//the method features can receive more than one feature Restfulie.at("resource_uri").withFeature(MyFeatureResponse.class,OtherFeatureResponse.class).get();
All Restfulie client features are implemented through a set of feature classes such as this one. Feel free to contribute with extra features, configuration sets or documentation to them or ask around in the mailing lists for other people contributions.