Created
February 27, 2012 12:54
-
-
Save dyokomizo/1923530 to your computer and use it in GitHub Desktop.
RestAgent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RestAgent agent = new HttpClientRestAgent(); | |
String text = agent.goTo(someUri) | |
.get() | |
.post(someEntity) | |
.redirect() | |
.get() | |
.read(plainTextHandler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URI; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.client.ResponseHandler; | |
// Inspired by http://www.bizcoder.com/index.php/2010/08/28/agent-fielding-is-on-a-mission/ | |
public interface RestAgent { | |
// set the current URI | |
RestAgent goTo(URI uri); | |
// execute the request on the current URI and save the response | |
RestAgent get(); | |
RestAgent post(HttpEntity entity); | |
RestAgent put(HttpEntity entity); | |
RestAgent delete(); | |
// clears the last saved response | |
RestAgent clear(); | |
// set the current URI to the Location Header of last response | |
RestAgent redirect(); | |
// Applies the handler to the last response | |
// With the appropriate ResponseHandlers you can get the status code, headers, body parsed, etc. | |
<T> T read(ResponseHandler<T> handler); | |
} |
Instead of using HttpClient one can replace it with Restlet, etc., with no loss of expressivity. If one wants to use another language, no problem at all. Actually languages supporting lightweight closure definition the usage is much, much improved on the the higher-order methods (i.e. anything using Action or ResponseHandler).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course a couple of additional methods may be necessary:
1. To modify the request (e.g. set headers)
2. To do some arbitrary action on the response (e.g. check the status code and throw an exception if it's client or server error).
For both my current design is based on:
And many small actions.
So we need to add a couple of methods to RestAgent:
I didn't include those because I'm not 100% sure about their design yet, because I don't use this kind of feature a lot and it may not be sufficient, but they are theoretically enough.
With the appropriate actions it becomes: