Merge lp:~wallyworld/lazr.restfulclient/named-post-url-change-1056546 into lp:lazr.restfulclient

Proposed by Ian Booth
Status: Merged
Approved by: Curtis Hovey
Approved revision: 136
Merged at revision: 133
Proposed branch: lp:~wallyworld/lazr.restfulclient/named-post-url-change-1056546
Merge into: lp:lazr.restfulclient
Diff against target: 80 lines (+28/-4)
5 files modified
src/lazr/restfulclient/NEWS.txt (+6/-0)
src/lazr/restfulclient/docs/operations.txt (+13/-0)
src/lazr/restfulclient/resource.py (+7/-2)
src/lazr/restfulclient/version.txt (+1/-1)
versions.cfg (+1/-1)
To merge this branch: bzr merge lp:~wallyworld/lazr.restfulclient/named-post-url-change-1056546
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+126375@code.launchpad.net

Commit message

If a Location header is in a named operation post response, use it as the replacement url for the resource.

Description of the change

A named post operation may result in the resource on which the post is operating to have a different url at the end of the operation. When the lazr restful client has finished making the api call, it invokes lp_refresh() to get the new state of the resource, and this fails with a 404 if the url has changed, since the old url is all the client knows about.

I added a bit of code to detect the 301 response and reload the resource if necessary.

I added a dependency on the corresponding new lazr.resful lib and updated a doctest to reflect the new behaviour.

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

Thank you. Users do not install this lib. I doubt it will even be backported to LTS releases, so I expect users will still see this problem for years after we release the fix.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restfulclient/NEWS.txt'
2--- src/lazr/restfulclient/NEWS.txt 2012-06-19 13:47:10 +0000
3+++ src/lazr/restfulclient/NEWS.txt 2012-09-26 11:13:26 +0000
4@@ -2,6 +2,12 @@
5 NEWS for lazr.restfulclient
6 ===========================
7
8+0.13.1 (2012-09-26)
9+===================
10+
11+ - Named POST operations may result in a resource moving to a new location.
12+ Detect the redirect and reload the resource from its new URL.
13+
14 0.13.0 (2012-06-19)
15 ===================
16
17
18=== modified file 'src/lazr/restfulclient/docs/operations.txt'
19--- src/lazr/restfulclient/docs/operations.txt 2011-11-18 20:38:06 +0000
20+++ src/lazr/restfulclient/docs/operations.txt 2012-09-26 11:13:26 +0000
21@@ -74,6 +74,19 @@
22 >>> print cookbook.find_recipe_for(dish=dish)
23 None
24
25+A named operation may change the resource's location so we get a 301 response
26+with the new URL.
27+
28+ >>> from urllib import quote
29+ >>> from lazr.restful.testing.webservice import WebServiceCaller
30+ >>> webservice = WebServiceCaller(domain='cookbooks.dev')
31+ >>> url = quote("/cookbooks/New cookbook")
32+ >>> print webservice.named_post(url, 'make_more_interesting')
33+ HTTP/1.1 301 Moved Permanently
34+ ...
35+ Location: http://cookbooks.dev/devel/cookbooks/The%20New%20New%20cookbook
36+ ...
37+
38 JSON-encoding
39 -------------
40
41
42=== modified file 'src/lazr/restfulclient/resource.py'
43--- src/lazr/restfulclient/resource.py 2012-06-19 11:58:42 +0000
44+++ src/lazr/restfulclient/resource.py 2012-09-26 11:13:26 +0000
45@@ -589,8 +589,13 @@
46 else:
47 if http_method == 'post':
48 # The method call probably modified this resource in
49- # an unknown way. Refresh its representation.
50- self.resource.lp_refresh()
51+ # an unknown way. If it moved to a new location, reload it or
52+ # else just refresh its representation.
53+ if response.status == 301:
54+ url = response['location']
55+ response, content = self.root._browser._request(url)
56+ else:
57+ self.resource.lp_refresh()
58 return self._handle_200_response(url, response, content)
59
60 def _handle_201_response(self, url, response, content):
61
62=== modified file 'src/lazr/restfulclient/version.txt'
63--- src/lazr/restfulclient/version.txt 2012-06-19 13:47:10 +0000
64+++ src/lazr/restfulclient/version.txt 2012-09-26 11:13:26 +0000
65@@ -1,1 +1,1 @@
66-0.13.0
67+0.13.1
68
69=== modified file 'versions.cfg'
70--- versions.cfg 2012-06-19 13:28:22 +0000
71+++ versions.cfg 2012-09-26 11:13:26 +0000
72@@ -25,7 +25,7 @@
73 lazr.delegates = 1.2.0
74 lazr.enum = 1.1.2
75 lazr.lifecycle = 1.0
76-lazr.restful = 0.19.2
77+lazr.restful = 0.19.7
78 lazr.uri = 1.0.2
79 lxml = 2.2.7
80 martian = 0.11

Subscribers

People subscribed via source and target branches