What Ramone does is to wrap a lot of the bread and butter code into a library for easy reuse (see this previous post for my motivation). Out of the box it offers XML, JSON, ATOM and HTML formatters, URL templating and parameter binding, hyper-media traversal, form handling, and has HTTPs "Uniform Interface" as first class citizen in the interface.
With Ramone you won't see methods like GetOrders(...) or SaveUserData(...). You will instead be working with URLs that points to resources and use HTTPs uniform interface to interact with these. This means for instance that GetOrders(...) may become OrderRequest.Get() and SaveUserData(...) becomes UserRequest.Put(...).
The smallest example I can think of is this little four-liner that fetches the timeline of a specific Twitter account:
// All interaction with Ramone goes through a session (which defines a service base URL) ISession session = RamoneConfiguration.NewSession(new Uri("https://api.twitter.com")); // Creating a Ramone request by binding variables to a URI template Request request = session.Bind(UserTimeLineTemplate, new { screen_name = ScreenName, count = 2 }); // GET response from Twitter Response response = request.Get(); // Extract payload as a C# dynamic created from the JSON response dynamic timeline = response.Body;
What happens here is:
- A session is created. This carries information about the base URL for template binding and keeps track of client state (cookies among other things).
- A request is created by binding a URL template with parameters. Input is a relative URL template path ("/1/statuses/user_timeline.json?screen_name={screen_name}&count={count}") and an anonymous parameter object. Output is a request for an absolute URI.
- One of the operations from HTTPs Uniform Interface is called (GET) and the response is recorded.
- Ramone checks the returned media-type, sees it is application/json, and converts it to a dynamic C# object with late binding of the returned values.
Console.WriteLine("This is the timeline for {0}:", ScreenName); Console.WriteLine(); foreach (dynamic tweet in timeline) { Console.WriteLine("* {0}.", tweet.text); Console.WriteLine(); }
A few things worth noticing:
- Twitter may not be the best REST example out there, but it is well understood by many people and is a web API which is quite easy to work with.
- Ramone does not only handle JSON. It works with a repository of parsers and formatters (called codecs) which are responsible for handling the various formats Ramone may encounter.
You can find a complete Twitter demo at Github: https://github.com/JornWildt/Ramone/tree/master/TwitterDemo/TwitterDemo. This demo shows both how to fetch a timeline as well as how to post status updates with OAuth1 authentication.
All source code is available on Github: https://github.com/JornWildt/Ramone. At the time of writing there are no official binaries released so you will have to download the code from Github and compile it yourself.
Later on I will write about how Ramone helps you consuming more RESTful hyper-media based services by making links, link relations and forms first class citizens of the interface. For now you can check the blog example at Github: https://github.com/JornWildt/Ramone/tree/master/Ramone.Tests/Blog
Have fun!
Ingen kommentarer:
Send en kommentar