Examples

 

It is rather hard to write good documentation for a library. That said, all public methods should be documented in the API docs and there are some examples in the code itself, if you care to download the sources.


Instead of writing a proper manual, I’m going to present a couple of examples and usage patterns for the library.


Well, some general guidelines are in order:

Create an instance of us.monoid.web.Resty and hold onto it someplace.

Then you usually call a method that sounds like the expected return type and use a URL as the first parameter. If you add a second parameter, that is usually the content you are sending to the web-service. Surround it with a call to put(...) and it becomes a PUT operation otherwise it will default to a POST. For DELETE, just add a delete() call as second parameter.


Resty makes extensive use of static helper methods.

Use import static us.monoid.web.Resty.*; to enjoy the comfort of short and concise code.

On to the examples.

Resty will stream the bytes to disk and return the File object that you passed into the save method. The bytes method accepts application/octet-stream,*/* as MIME type.

This example is quite a bit more complex, as it combines accessing a JSON resource, extracting a URL via a path expression, accessing this URL as XML and using an XPath expression to access the title of a calendar entry.


The static method path(...) creates JSONPathExpression that is applied to the JSONResource. The result of which is interpreted as an URL, which is used in the xml(...) call. Since resource classes are inheriting from Resty. This makes it rather easy to follow hyperlinks that are values of JSON attributes or XML fragments.


The get(...) method of the XmlResource, returned by the xml(...) call takes an XPath expression and the type of the returned node.

This simplest of all examples will request text/* from the given URL and turn it into a String.