141
140
>>> bound_service_root = bind_to_testdata(service_root, 'root')
141
>>> sorted([param.name for param in bound_service_root.parameters()])
142
['bugs_collection_link', 'people_collection_link']
142
143
>>> sorted(bound_service_root.parameter_names())
143
144
['bugs_collection_link', 'people_collection_link']
144
145
>>> [method.id for method in bound_service_root.method_iter]
145
146
['service-root-get']
148
148
Now the bound resource object has a JSON representation, and now
149
149
'people_collection_link' makes sense. We can follow the
150
150
'people_collection_link' to a new Resource object.
274
274
>>> new_team.type_url
275
275
'http://api.launchpad.dev/beta/#team'
277
======================
280
The 'linked_resource' property of a parameter lets you follow a link
281
to another object. The 'link' property of a parameter lets you examine
282
links before following them.
284
>>> import simplejson
285
>>> links_wadl = application_for('links-wadl.xml')
286
>>> service_root = links_wadl.get_resource_by_path('')
287
>>> representation = simplejson.dumps(
288
... {'scalar_value': 'foo',
289
... 'known_link': 'http://known/',
290
... 'unknown_link': 'http://unknown/'})
291
>>> bound_root = service_root.bind(representation)
293
>>> print bound_root.get_parameter("scalar_value").link
296
>>> known_resource = bound_root.get_parameter("known_link")
297
>>> unknown_resource = bound_root.get_parameter("unknown_link")
299
>>> print known_resource.link.can_follow
301
>>> print unknown_resource.link.can_follow
304
A link whose type is unknown is a link to a resource not described by
305
WADL. Following this link using .linked_resource or .link.follow will
306
cause a wadllib error. You'll need to follow the link using a general
307
HTTP library or some other tool.
309
>>> known_resource.link.follow
310
<wadllib.application.Resource object ...>
311
>>> known_resource.linked_resource
312
<wadllib.application.Resource object ...>
314
>>> unknown_resource.link.follow
315
Traceback (most recent call last):
317
WADLError: Cannot follow a link when the target has no WADL
318
description. Try using a general HTTP client instead.
320
>>> unknown_resource.linked_resource
321
Traceback (most recent call last):
323
WADLError: Cannot follow a link when the target has no WADL
324
description. Try using a general HTTP client instead.
326
Creating a Resource from a representation definition
327
====================================================
329
Although every representation is a representation of some HTTP
330
resource, an HTTP resource doesn't necessarily correspond directly to
331
a WADL <resource> or <resource_type> tag. Sometimes a representation
332
is defined within a WADL <method> tag.
334
>>> find_method = personset_resource.get_method(
335
... query_params={'ws.op' : 'find'})
339
>>> representation_definition = (
340
... find_method.response.get_representation_definition(
341
... 'application/json'))
343
There may be no WADL <resource> or <resource_type> tag for the
344
representation defined here. That's why wadllib makes it possible to
345
instantiate an anonymous Resource object using only the representation
348
>>> from wadllib.application import Resource
349
>>> anonymous_resource = Resource(
350
... wadl, "http://foo/", representation_definition.tag)
352
We can bind this resource to a representation, as long as we
353
explicitly pass in the representation definition.
355
>>> anonymous_resource = anonymous_resource.bind(
356
... get_testdata('personset'), 'application/json',
357
... representation_definition=representation_definition)
359
Once the resource is bound to a representation, we can get its
362
>>> print anonymous_resource.get_parameter(
363
... 'total_size', 'application/json').get_value()
278
366
Resource instantiation
279
367
======================