Skip to content
toshikurauchi edited this page Feb 17, 2011 · 1 revision

Entry points

Entry points are commonly defined through a resource retrieval or creation request. Restfulie allows you to use both type of entry points through its API.

Resource retrieval entry point

Most systems will create a request retrieval entry point, which can be accessed as:

>>> city = Restfulie.at('http://localhost:3000/cities/5').get()

After that, the xml tree can be accessed and links followed. A typical city hypermedia file would be:

<city>
	<name>Sao Paulo</name>
	<population>
		<size>18000000</size>
		<growth>10</growth>
	</population>
	<link rel="next_largest" href="http://localhost:3000/cities/18" />
</city>

The information can be retrieved through the usual method invocations:

>>> print city.name
Sao Paulo

>>> print "size %s and growth %s" % (city.population.size, city.population.growth)
size 18000000 and growth 10

And links can be followed as:

>>> next_one = city.link('next_largest').follow()

Note that the client application knows what the rel attribute next_largest means, but does not know what it's value stands for (Rio de Janeiro).

Acessing the web response

In this case, you can access the http response through response, i.e.:

>>> city = Restfulie.at('http://localhost:3000/cities/5').get()
>>> print "Response code %s" % city.code

Media type and content negotiation

Restfulie entry point requests can make use of conneg by notifying the server which media type is being sent and which ones can be understood.

Restfulie.as_ will notify the server about the media type sent, setting the content-type header:

Restfulie.at('http://caelum.com.br/orders').as_('application/vnd_caelum_order+xml').get()

Restfulie.accepts notify the server of which media types are understood by the client using the Accepts header:

Restfulie.at('http://caelum.com.br/orders/2').accepts('application/vnd_caelum_order+xml').get