Python hypermedia clients

toshikurauchi edited this page Feb 17, 2011 · 1 revision

Hypermedia support information is scattered aroud the entire documentation as this is a hypermedia based library..

The response object

The response object to a restfulie http request allows access to four different methods, the response code, its body, headers and an extra method called resource that will try to unmarshall the response body according to its content type.

The following example shows a typical response to a xml request:

>>> response = Restfulie.at(uri).accepts("application/xml").get()
>>> print response.body

<order>
    <link rel='payment' href='http://store.caelum.com.br/orders/2345/payment' />
    <items>
        <item>
            <id>Rest from scratch book</id>
        </item>
    </items>
    <price>1235.4</price>
</order>

>>> print response.code
200
>>> print response.headers['content-type']
application/xml
>>> resource = response.resource()
>>> print resource.items.item.id
Rest from scratch book

Supported media types

Restfulie client comes with built in support to xml, json and opensearch and can be easily enhanced by providing your own driver.

Link support

For representations that contain link elements in its body, such as this one in xml:

<order>
    <link rel='payment' href='http://store.caelum.com.br/orders/2345/payment' />
    <items>
        <item>
            <id>Rest from scratch book</id>
        </item>
    </items>
    <price>1235.4</price>
</order>

This link can be accessed by unmarshalling the resource and then accessing the link itself:

>>> response = Restfulie.at(uri).get()

# follow the payment link within the order
>>> response = response.resource().links.payment.follow().get()

# will print the payment body
>>> print resource.body

Once you have invoked the follow method, the object returned is the same as a Restfulie.at() invocation, a request dsl, which allows you to configure the request as you wish, and execute it by invoking the proper http verb (i.e. get, post or put).

Link header support

If your service provides a link header such as:

Link: <http://amundsen.com/examples/mazes/2d/ten-by-ten/0:north>; rel="start"; type="application/xml"

This header can be accessed by using the headers.link and links method:

>>> resource = Restfulie.at(uri).get()
>>> print resource.link("start").href

following the link

another_resource = resource.link("start").follow().get()