Merge lp:~wallyworld/lazr.restfulclient/relative-url-fix-681767 into lp:lazr.restfulclient

Proposed by Ian Booth
Status: Merged
Approved by: Curtis Hovey
Approved revision: 124
Merged at revision: 124
Proposed branch: lp:~wallyworld/lazr.restfulclient/relative-url-fix-681767
Merge into: lp:lazr.restfulclient
Diff against target: 154 lines (+29/-11)
5 files modified
.bzrignore (+1/-0)
src/lazr/restfulclient/NEWS.txt (+6/-0)
src/lazr/restfulclient/docs/entries.txt (+9/-0)
src/lazr/restfulclient/resource.py (+12/-10)
src/lazr/restfulclient/version.txt (+1/-1)
To merge this branch: bzr merge lp:~wallyworld/lazr.restfulclient/relative-url-fix-681767
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+102050@code.launchpad.net

Commit message

Fix ServiceRoot.load() so that it properly handles relative URLs in a way that doesn't break subsequent API calls

Description of the change

== Implementation ==

The ServiceRoot.load() API takes a url as a parameter. In the case where the URL is relative, a URI object was being constructed and assigned to the url. However, this conflicted with the case where a full url was passed in as a string, and also downstream processing which expects the url to be a string. So the simple fix is simply to stringify the constructed URI object when a relative URL is passed in.

== Tests ==

Add some extra checks to entries.txt

== Lint ==

Did some lint cleanup to make everything happy.

To post a comment you must log in.
Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you for the fix and the de-linting.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2011-11-18 20:23:37 +0000
3+++ .bzrignore 2012-04-16 03:23:21 +0000
4@@ -11,3 +11,4 @@
5 build
6 *.egg
7 dist
8+MANIFEST
9
10=== modified file 'src/lazr/restfulclient/NEWS.txt'
11--- src/lazr/restfulclient/NEWS.txt 2012-03-28 20:37:13 +0000
12+++ src/lazr/restfulclient/NEWS.txt 2012-04-16 03:23:21 +0000
13@@ -2,6 +2,12 @@
14 NEWS for lazr.restfulclient
15 ===========================
16
17+0.12.2 (2012-04-16)
18+===================
19+
20+ - Fix ServiceRoot.load() so that it properly handles relative URLs
21+ in a way that doesn't break subsequent API calls (bug 681767).
22+
23 0.12.1 (2012-03-28)
24 ===================
25
26
27=== modified file 'src/lazr/restfulclient/docs/entries.txt'
28--- src/lazr/restfulclient/docs/entries.txt 2011-11-18 20:38:06 +0000
29+++ src/lazr/restfulclient/docs/entries.txt 2012-04-16 03:23:21 +0000
30@@ -176,14 +176,23 @@
31 currently in use.
32
33 >>> cookbooks = service.load("cookbooks")
34+ >>> assert isinstance(cookbooks._wadl_resource.url, basestring)
35+ >>> print cookbooks._wadl_resource.url
36+ http://cookbooks.dev/1.0/cookbooks
37 >>> print cookbooks['The Joy of Cooking'].self_link
38 http://cookbooks.dev/1.0/cookbooks/The%20Joy%20of%20Cooking
39
40 >>> cookbook = service.load("/cookbooks/The%20Joy%20of%20Cooking")
41+ >>> assert isinstance(cookbook._wadl_resource.url, basestring)
42+ >>> print cookbook._wadl_resource.url
43+ http://cookbooks.dev/1.0/cookbooks/The%20Joy%20of%20Cooking
44 >>> print cookbook.self_link
45 http://cookbooks.dev/1.0/cookbooks/The%20Joy%20of%20Cooking
46
47 >>> service_root = service.load("")
48+ >>> assert isinstance(service_root._wadl_resource.url, basestring)
49+ >>> print service_root._wadl_resource.url
50+ http://cookbooks.dev/1.0/
51 >>> print service_root.cookbooks['The Joy of Cooking'].name
52 The Joy of Cooking
53
54
55=== modified file 'src/lazr/restfulclient/resource.py'
56--- src/lazr/restfulclient/resource.py 2011-11-18 20:38:06 +0000
57+++ src/lazr/restfulclient/resource.py 2012-04-16 03:23:21 +0000
58@@ -50,6 +50,7 @@
59
60 missing = object()
61
62+
63 class HeaderDictionary:
64 """A dictionary that bridges httplib2's and wadllib's expectations.
65
66@@ -218,8 +219,8 @@
67 param_name + suffix)
68 if param is not None:
69 try:
70- value = param.get_value()
71- except KeyError, e:
72+ param.get_value()
73+ except KeyError:
74 # The parameter could have been present, but isn't.
75 # Try the next parameter.
76 continue
77@@ -242,7 +243,7 @@
78 :return: A NamedOperation instance that can be called with
79 appropriate arguments to invoke the operation.
80 """
81- params = { 'ws.op' : operation_name }
82+ params = {'ws.op': operation_name}
83 method = self._wadl_resource.get_method('get', query_params=params)
84 if method is None:
85 method = self._wadl_resource.get_method(
86@@ -369,7 +370,8 @@
87 type_link = representation['resource_type_link']
88 if (type_link is not None
89 and type_link != self._wadl_resource.type_url):
90- resource_type = self._root._wadl.get_resource_type(type_link)
91+ resource_type = self._root._wadl.get_resource_type(
92+ type_link)
93 self._wadl_resource.tag = resource_type.tag
94 self.__dict__['_wadl_resource'] = self._wadl_resource.bind(
95 representation, self.JSON_MEDIA_TYPE,
96@@ -388,7 +390,7 @@
97 """Return the scalar value."""
98 self._ensure_representation()
99 return self._wadl_resource.representation
100-
101+
102
103 class HostedFile(Resource):
104 """A resource representing a file managed by a lazr.restful service."""
105@@ -501,7 +503,7 @@
106 # it with the service root resource.
107 if url[:1] == '/':
108 url = url[1:]
109- url = self._root_uri.append(url)
110+ url = str(self._root_uri.append(url))
111 document = self._browser.get(url)
112 try:
113 representation = simplejson.loads(unicode(document))
114@@ -568,7 +570,7 @@
115 (media_type,
116 in_representation) = self.wadl_method.build_representation(
117 **args)
118- extra_headers = { 'Content-type' : media_type }
119+ extra_headers = {'Content-type': media_type}
120 # Pass uppercase method names to httplib2, as that is what it works
121 # with. If you pass a lowercase method name to httplib then it doesn't
122 # consider it to be a GET, PUT, etc., and so will do things like not
123@@ -806,7 +808,7 @@
124 return self._get_slice(key)
125 else:
126 # Look up a single item by its position in the list.
127- found_slice = self._get_slice(slice(key, key+1))
128+ found_slice = self._get_slice(slice(key, key + 1))
129 if len(found_slice) != 1:
130 raise IndexError("list index out of range")
131 return found_slice[0]
132@@ -858,7 +860,7 @@
133 page_url = self._with_url_query_variable_set(
134 self._wadl_resource.url, 'ws.start', start)
135
136- desired_size = stop-start
137+ desired_size = stop - start
138 more_needed = desired_size - len(entry_dicts)
139
140 # Iterate over pages until we have the correct number of entries.
141@@ -1060,5 +1062,5 @@
142 disposition = 'attachment; filename="%s"' % self.filename
143 self.hosted_file._root._browser.put(
144 self.url, self.getvalue(),
145- self.content_type, {'Content-Disposition' : disposition})
146+ self.content_type, {'Content-Disposition': disposition})
147 StringIO.close(self)
148
149=== modified file 'src/lazr/restfulclient/version.txt'
150--- src/lazr/restfulclient/version.txt 2012-03-28 20:37:13 +0000
151+++ src/lazr/restfulclient/version.txt 2012-04-16 03:23:21 +0000
152@@ -1,1 +1,1 @@
153-0.12.1
154+0.12.2

Subscribers

People subscribed via source and target branches