Merge lp:~leonardr/wadllib/what-kind-of-link into lp:wadllib
Proposed by
Leonard Richardson
Status: | Merged |
---|---|
Approved by: | Curtis Hovey |
Approved revision: | 23 |
Merged at revision: | 20 |
Proposed branch: | lp:~leonardr/wadllib/what-kind-of-link |
Merge into: | lp:wadllib |
Diff against target: |
259 lines (+117/-14) 4 files modified
src/wadllib/NEWS.txt (+10/-0) src/wadllib/README.txt (+52/-8) src/wadllib/application.py (+54/-5) src/wadllib/version.txt (+1/-1) |
To merge this branch: | bzr merge lp:~leonardr/wadllib/what-kind-of-link |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Curtis Hovey (community) | code | Approve | |
j.c.sackett (community) | code* | Approve | |
Review via email: mp+48485@code.launchpad.net |
Description of the change
Currently, wadllib assumes that all <link>s are links to other WADL-controlled resources. If you try to follow a link that's just a random link to some URL, you'll get an exception. There is no easy way to examine a link beforehand to see what kind of link it is.
This branch adds the '.link' property to Parameter, which lets you get the Link object under consideration. It adds a '.can_follow' property to Link, which lets you see whether you can handle the link with wadllib or whether you need to use a general HTTP client. For convenience, it also adds a '.follow' property to Link.
To post a comment you must log in.
Leonard--
This looks like a good improvement.
I have two suggestions (not requirements):
> === modified file 'src/wadllib/ README. txt' README. txt 2010-05-03 15:58:32 +0000 README. txt 2011-02-03 15:16:29 +0000 api.launchpad. dev/beta/ #team' for('links- wadl.xml' ) get_resource_ by_path( '') known/', unknown/ '}) root.bind( representation) get_parameter( "scalar_ value") .link get_parameter( "known_ link") known_resource = known_resource.link get_parameter( "unknown_ link") unknown_ resource = unknown_ resource. link known_resource. can_follow unknown_ resource. can_follow
> --- src/wadllib/
> +++ src/wadllib/
> @@ -274,6 +273,57 @@
> >>> new_team.type_url
> 'http://
>
> +Examining links
> +---------------
> +
> +The 'linked_resource' property of a parameter lets you follow a link
> +to another object. The 'link' property of a parameter lets you examine
> +links before following them.
> +
> + >>> import simplejson
> + >>> links_wadl = application_
> + >>> service_root = links_wadl.
> + >>> representation = simplejson.dumps(
> + ... {'scalar_value': 'foo',
> + ... 'known_link': 'http://
> + ... 'unknown_link': 'http://
> + >>> bound_root = service_
> +
> + >>> print bound_root.
> + None
> +
> + >>> known_resource = bound_root.
> + >>> link_to_
> + >>> unknown_resource = bound_root.
> + >>> link_to_
> +
> + >>> print link_to_
> + True
> + >>> print link_to_
> + False
This might read easier without assigning the .link to a variable. e.g.
+ >>> known_resource = bound_root. get_parameter( "known_ link") get_parameter( "unknown_ link") link.can_ follow resource. link.can_ follow
+ >>> unknown_resource = bound_root.
+
+ >>> print known_resource.
+ True
+ >>> print unknown_
I only suggest this because I think it's a clearer indication of using the "link" property, which is the ostensible point of this section of the doc test.
>=== modified file 'src/wadllib/ application. py' application. py 2010-05-03 16:15:19 +0000 application. py 2011-02-03 15:16:29 +0000 definition( ) definition_ url()
>--- src/wadllib/
>+++ src/wadllib/
>@@ -962,6 +976,28 @@
> self.parameter = parameter
> self.tag = link_tag
>
>+ @property
>+ def follow(self):
>+ """Follow the link to another Resource."""
>+ if not self.can_follow:
>+ raise WADLError("Cannot follow a link when the target has no "
>+ "WADL description. Try using a general HTTP "
>+ "client instead.")
>+ return self.resolve_
>+
>+ @property
>+ def can_follow(self):
>+ """Can this link be followed within wadllib?
>+
>+ wadllib can follow a link if it points to a resource that has
>+ a WADL definition.
>+ """
>+ try:
>+ definition_url = self._get_
>+ return True
>+ except WADLError:
>+ return False
It might make a bit more sense to return True as the last statement of this property, rather than returning True in the "try" clause. i.e.:
>+ try: definition_ url()
>+ definition_url = self._get_
>+ except WADLError:
>+ return False
>+ return True
Min...