Merge lp:~leonardr/lazr.restful/active-versions into lp:lazr.restful
- active-versions
- Merge into trunk
Proposed by
Leonard Richardson
Status: | Merged |
---|---|
Approved by: | Aaron Bentley |
Approved revision: | not available |
Merged at revision: | not available |
Proposed branch: | lp:~leonardr/lazr.restful/active-versions |
Merge into: | lp:lazr.restful |
Diff against target: |
583 lines (+287/-62) 18 files modified
src/lazr/restful/NEWS.txt (+10/-0) src/lazr/restful/docs/absoluteurl.txt (+7/-7) src/lazr/restful/docs/django.txt (+2/-2) src/lazr/restful/docs/webservice-declarations.txt (+1/-1) src/lazr/restful/docs/webservice-request.txt (+1/-1) src/lazr/restful/docs/webservice.txt (+1/-1) src/lazr/restful/example/base/root.py (+1/-1) src/lazr/restful/example/base/tests/service.txt (+12/-31) src/lazr/restful/example/multiversion/README.txt (+6/-0) src/lazr/restful/example/multiversion/resources.py (+35/-0) src/lazr/restful/example/multiversion/root.py (+53/-0) src/lazr/restful/example/multiversion/site.zcml (+13/-0) src/lazr/restful/example/multiversion/tests/introduction.txt (+64/-0) src/lazr/restful/example/multiversion/tests/test_integration.py (+53/-0) src/lazr/restful/example/wsgi/root.py (+1/-1) src/lazr/restful/interfaces/_rest.py (+14/-10) src/lazr/restful/publisher.py (+12/-6) src/lazr/restful/testing/webservice.py (+1/-1) |
To merge this branch: | bzr merge lp:~leonardr/lazr.restful/active-versions |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Aaron Bentley (community) | code | Approve | |
Review via email: mp+14912@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Leonard Richardson (leonardr) wrote : | # |
Revision history for this message
Aaron Bentley (abentley) wrote : | # |
Looks fine.
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/restful/NEWS.txt' | |||
2 | --- src/lazr/restful/NEWS.txt 2009-11-12 16:30:50 +0000 | |||
3 | +++ src/lazr/restful/NEWS.txt 2009-11-16 15:00:27 +0000 | |||
4 | @@ -5,6 +5,10 @@ | |||
5 | 5 | Development | 5 | Development |
6 | 6 | =========== | 6 | =========== |
7 | 7 | 7 | ||
8 | 8 | Special note: this version contains backwards-incompatible | ||
9 | 9 | changes. You *must* change your configuration object to get your code | ||
10 | 10 | to work in this version! See "active_versions" below. | ||
11 | 11 | |||
12 | 8 | Added the precursor of a versioning system for web services. Clients | 12 | Added the precursor of a versioning system for web services. Clients |
13 | 9 | can now request the "trunk" of a web service as well as one published | 13 | can now request the "trunk" of a web service as well as one published |
14 | 10 | version. Apart from the URIs served, the two web services are exactly | 14 | version. Apart from the URIs served, the two web services are exactly |
15 | @@ -16,6 +20,12 @@ | |||
16 | 16 | BaseWebServiceConfiguration or one of its subclasses, you'll need to | 20 | BaseWebServiceConfiguration or one of its subclasses, you'll need to |
17 | 17 | set a value for this. | 21 | set a value for this. |
18 | 18 | 22 | ||
19 | 23 | This release _replaces_ one of the fields in | ||
20 | 24 | IWebServiceConfiguration. The string service_version_uri_prefix has | ||
21 | 25 | become the list active_versions. The simplest way to deal with this is | ||
22 | 26 | to just put your service_version_uri_prefix into a list and call it | ||
23 | 27 | active_versions. | ||
24 | 28 | |||
25 | 19 | 0.9.17 (2009-11-10) | 29 | 0.9.17 (2009-11-10) |
26 | 20 | =================== | 30 | =================== |
27 | 21 | 31 | ||
28 | 22 | 32 | ||
29 | === modified file 'src/lazr/restful/docs/absoluteurl.txt' | |||
30 | --- src/lazr/restful/docs/absoluteurl.txt 2009-11-12 17:03:06 +0000 | |||
31 | +++ src/lazr/restful/docs/absoluteurl.txt 2009-11-16 15:00:27 +0000 | |||
32 | @@ -29,7 +29,7 @@ | |||
33 | 29 | ... implements(IWebServiceConfiguration) | 29 | ... implements(IWebServiceConfiguration) |
34 | 30 | ... hostname = "hostname" | 30 | ... hostname = "hostname" |
35 | 31 | ... service_root_uri_prefix = "root_uri_prefix/" | 31 | ... service_root_uri_prefix = "root_uri_prefix/" |
37 | 32 | ... service_version_uri_prefix = "service_version_uri_prefix" | 32 | ... active_versions = ['active_version'] |
38 | 33 | ... latest_version_uri_prefix = "latest_version_uri_prefix" | 33 | ... latest_version_uri_prefix = "latest_version_uri_prefix" |
39 | 34 | ... port = 1000 | 34 | ... port = 1000 |
40 | 35 | ... use_https = True | 35 | ... use_https = True |
41 | @@ -50,20 +50,20 @@ | |||
42 | 50 | >>> resource = RootResource() | 50 | >>> resource = RootResource() |
43 | 51 | >>> request = Request("", {}) | 51 | >>> request = Request("", {}) |
44 | 52 | >>> request.annotations[request.VERSION_ANNOTATION] = ( | 52 | >>> request.annotations[request.VERSION_ANNOTATION] = ( |
46 | 53 | ... 'service_version_uri_prefix') | 53 | ... 'active_version') |
47 | 54 | >>> adapter = getMultiAdapter((resource, request), IAbsoluteURL) | 54 | >>> adapter = getMultiAdapter((resource, request), IAbsoluteURL) |
48 | 55 | 55 | ||
49 | 56 | Calling the RootResourceAbsoluteURL will give the service root's | 56 | Calling the RootResourceAbsoluteURL will give the service root's |
50 | 57 | absolute URL. | 57 | absolute URL. |
51 | 58 | 58 | ||
52 | 59 | >>> print adapter() | 59 | >>> print adapter() |
54 | 60 | https://hostname:1000/root_uri_prefix/service_version_uri_prefix/ | 60 | https://hostname:1000/root_uri_prefix/active_version/ |
55 | 61 | 61 | ||
56 | 62 | Converting the adapter to a string will give the same result, but | 62 | Converting the adapter to a string will give the same result, but |
57 | 63 | without the trailing slash. | 63 | without the trailing slash. |
58 | 64 | 64 | ||
59 | 65 | >>> print str(adapter) | 65 | >>> print str(adapter) |
61 | 66 | https://hostname:1000/root_uri_prefix/service_version_uri_prefix | 66 | https://hostname:1000/root_uri_prefix/active_version |
62 | 67 | 67 | ||
63 | 68 | (This is useful for the recursive case. When finding the URL to a | 68 | (This is useful for the recursive case. When finding the URL to a |
64 | 69 | subordinate resource, Zope's AbsoluteURL implementation calls str() on | 69 | subordinate resource, Zope's AbsoluteURL implementation calls str() on |
65 | @@ -75,11 +75,11 @@ | |||
66 | 75 | 75 | ||
67 | 76 | >>> configuration.use_https = False | 76 | >>> configuration.use_https = False |
68 | 77 | >>> print getMultiAdapter((resource, request), IAbsoluteURL)() | 77 | >>> print getMultiAdapter((resource, request), IAbsoluteURL)() |
70 | 78 | http://hostname:1000/root_uri_prefix/service_version_uri_prefix/ | 78 | http://hostname:1000/root_uri_prefix/active_version/ |
71 | 79 | 79 | ||
72 | 80 | >>> configuration.port = None | 80 | >>> configuration.port = None |
73 | 81 | >>> print getMultiAdapter((resource, request), IAbsoluteURL)() | 81 | >>> print getMultiAdapter((resource, request), IAbsoluteURL)() |
75 | 82 | http://hostname/root_uri_prefix/service_version_uri_prefix/ | 82 | http://hostname/root_uri_prefix/active_version/ |
76 | 83 | 83 | ||
77 | 84 | The URL generated includes a version identifier taken from the | 84 | The URL generated includes a version identifier taken from the |
78 | 85 | value of the 'lazr.restful.version' annotation. | 85 | value of the 'lazr.restful.version' annotation. |
79 | @@ -106,7 +106,7 @@ | |||
80 | 106 | Cleanup. | 106 | Cleanup. |
81 | 107 | 107 | ||
82 | 108 | >>> request.annotations[request.VERSION_ANNOTATION] = ( | 108 | >>> request.annotations[request.VERSION_ANNOTATION] = ( |
84 | 109 | ... 'service_version_uri_prefix') | 109 | ... 'active_version') |
85 | 110 | 110 | ||
86 | 111 | 111 | ||
87 | 112 | MultiplePathPartAbsoluteURL | 112 | MultiplePathPartAbsoluteURL |
88 | 113 | 113 | ||
89 | === modified file 'src/lazr/restful/docs/django.txt' | |||
90 | --- src/lazr/restful/docs/django.txt 2009-09-08 13:33:01 +0000 | |||
91 | +++ src/lazr/restful/docs/django.txt 2009-11-16 15:00:27 +0000 | |||
92 | @@ -110,8 +110,8 @@ | |||
93 | 110 | Attributes that don't have values in settings.py are given their | 110 | Attributes that don't have values in settings.py are given their |
94 | 111 | default value. | 111 | default value. |
95 | 112 | 112 | ||
98 | 113 | >>> utility.service_version_uri_prefix | 113 | >>> utility.active_versions |
99 | 114 | u'' | 114 | [] |
100 | 115 | >>> utility.use_https | 115 | >>> utility.use_https |
101 | 116 | True | 116 | True |
102 | 117 | 117 | ||
103 | 118 | 118 | ||
104 | === modified file 'src/lazr/restful/docs/webservice-declarations.txt' | |||
105 | --- src/lazr/restful/docs/webservice-declarations.txt 2009-06-08 20:03:55 +0000 | |||
106 | +++ src/lazr/restful/docs/webservice-declarations.txt 2009-11-16 15:00:27 +0000 | |||
107 | @@ -953,7 +953,7 @@ | |||
108 | 953 | >>> class MyWebServiceConfiguration: | 953 | >>> class MyWebServiceConfiguration: |
109 | 954 | ... implements(IWebServiceConfiguration) | 954 | ... implements(IWebServiceConfiguration) |
110 | 955 | ... view_permission = "lazr.View" | 955 | ... view_permission = "lazr.View" |
112 | 956 | ... service_version_uri_prefix = "beta" | 956 | ... active_versions = ["beta"] |
113 | 957 | ... code_revision = "1.0b" | 957 | ... code_revision = "1.0b" |
114 | 958 | ... default_batch_size = 50 | 958 | ... default_batch_size = 50 |
115 | 959 | ... | 959 | ... |
116 | 960 | 960 | ||
117 | === modified file 'src/lazr/restful/docs/webservice-request.txt' | |||
118 | --- src/lazr/restful/docs/webservice-request.txt 2009-08-11 18:36:20 +0000 | |||
119 | +++ src/lazr/restful/docs/webservice-request.txt 2009-11-16 15:00:27 +0000 | |||
120 | @@ -22,7 +22,7 @@ | |||
121 | 22 | >>> class SimpleWebServiceConfiguration: | 22 | >>> class SimpleWebServiceConfiguration: |
122 | 23 | ... implements(IWebServiceConfiguration) | 23 | ... implements(IWebServiceConfiguration) |
123 | 24 | ... path_override = 'api' | 24 | ... path_override = 'api' |
125 | 25 | ... service_version_uri_prefix = 'beta' | 25 | ... active_versions = ['beta'] |
126 | 26 | ... | 26 | ... |
127 | 27 | ... def createRequest(self, body_stream, environ): | 27 | ... def createRequest(self, body_stream, environ): |
128 | 28 | ... request = Request(body_stream, environ) | 28 | ... request = Request(body_stream, environ) |
129 | 29 | 29 | ||
130 | === modified file 'src/lazr/restful/docs/webservice.txt' | |||
131 | --- src/lazr/restful/docs/webservice.txt 2009-11-12 16:30:50 +0000 | |||
132 | +++ src/lazr/restful/docs/webservice.txt 2009-11-16 15:00:27 +0000 | |||
133 | @@ -517,7 +517,7 @@ | |||
134 | 517 | 517 | ||
135 | 518 | >>> class WebServiceConfiguration(BaseWebServiceConfiguration): | 518 | >>> class WebServiceConfiguration(BaseWebServiceConfiguration): |
136 | 519 | ... use_https = False | 519 | ... use_https = False |
138 | 520 | ... service_version_uri_prefix = 'beta' | 520 | ... active_versions = ['beta'] |
139 | 521 | ... latest_version_uri_prefix = 'devel' | 521 | ... latest_version_uri_prefix = 'devel' |
140 | 522 | ... code_revision = 'test' | 522 | ... code_revision = 'test' |
141 | 523 | ... max_batch_size = 100 | 523 | ... max_batch_size = 100 |
142 | 524 | 524 | ||
143 | === modified file 'src/lazr/restful/example/base/root.py' | |||
144 | --- src/lazr/restful/example/base/root.py 2009-11-12 16:43:57 +0000 | |||
145 | +++ src/lazr/restful/example/base/root.py 2009-11-16 15:00:27 +0000 | |||
146 | @@ -387,7 +387,7 @@ | |||
147 | 387 | default_batch_size=5 | 387 | default_batch_size=5 |
148 | 388 | hostname='cookbooks.dev' | 388 | hostname='cookbooks.dev' |
149 | 389 | match_batch_size=50 | 389 | match_batch_size=50 |
151 | 390 | service_version_uri_prefix='1.0' | 390 | active_versions=['1.0'] |
152 | 391 | use_https=False | 391 | use_https=False |
153 | 392 | view_permission='lazr.restful.example.base.View' | 392 | view_permission='lazr.restful.example.base.View' |
154 | 393 | 393 | ||
155 | 394 | 394 | ||
156 | === modified file 'src/lazr/restful/example/base/tests/service.txt' | |||
157 | --- src/lazr/restful/example/base/tests/service.txt 2009-11-12 16:35:38 +0000 | |||
158 | +++ src/lazr/restful/example/base/tests/service.txt 2009-11-16 15:00:27 +0000 | |||
159 | @@ -10,37 +10,18 @@ | |||
160 | 10 | Versioning | 10 | Versioning |
161 | 11 | ========== | 11 | ========== |
162 | 12 | 12 | ||
194 | 13 | In addition to the published version of the web service, lazr.restful | 13 | lazr.restful allows you to publish multiple named versions of a web |
195 | 14 | publishes a "development" version which may contain backwards | 14 | service, as well as a "development" version which includes changes you |
196 | 15 | incompatible changes. The default name of the development version is | 15 | have yet to batch together into a named version. The default name of |
197 | 16 | "devel". | 16 | the development version is "devel". |
198 | 17 | 17 | ||
199 | 18 | >>> body = webservice.get('/', api_version="devel").jsonBody() | 18 | >>> top_level_response = webservice.get( |
200 | 19 | 19 | ... "/", api_version="devel").jsonBody() | |
201 | 20 | The development version serves URLs that reflect its version name. | 20 | >>> print top_level_response['resource_type_link'] |
202 | 21 | 21 | http://cookbooks.dev/devel/#service-root | |
203 | 22 | >>> print body['recipes_collection_link'] | 22 | |
204 | 23 | http://cookbooks.dev/devel/recipes | 23 | The web service in examples/multiversion is devoted solely to testing |
205 | 24 | >>> print body['resource_type_link'] | 24 | the versioning code. |
175 | 25 | http://cookbooks.dev/devel/#service-root | ||
176 | 26 | |||
177 | 27 | Currently there is no way to publish multiple versions of the web | ||
178 | 28 | service, so apart from the URLs, the development version is exactly | ||
179 | 29 | the same as the only published version. | ||
180 | 30 | |||
181 | 31 | All versions of the web service can be accessed through Ajax. | ||
182 | 32 | |||
183 | 33 | >>> from lazr.restful.testing.webservice import WebServiceAjaxCaller | ||
184 | 34 | >>> ajax = WebServiceAjaxCaller(domain='cookbooks.dev') | ||
185 | 35 | >>> body = ajax.get('/', api_version="devel").jsonBody() | ||
186 | 36 | >>> print body['resource_type_link'] | ||
187 | 37 | http://cookbooks.dev/devel/#service-root | ||
188 | 38 | |||
189 | 39 | An attempt to access a nonexistent version yields a 404 error. | ||
190 | 40 | |||
191 | 41 | >>> print webservice.get('/', api_version="no_such_version") | ||
192 | 42 | HTTP/1.1 404 Not Found | ||
193 | 43 | ... | ||
206 | 44 | 25 | ||
207 | 45 | Nonexistent resources | 26 | Nonexistent resources |
208 | 46 | ===================== | 27 | ===================== |
209 | 47 | 28 | ||
210 | === added directory 'src/lazr/restful/example/multiversion' | |||
211 | === added file 'src/lazr/restful/example/multiversion/README.txt' | |||
212 | --- src/lazr/restful/example/multiversion/README.txt 1970-01-01 00:00:00 +0000 | |||
213 | +++ src/lazr/restful/example/multiversion/README.txt 2009-11-16 15:00:27 +0000 | |||
214 | @@ -0,0 +1,6 @@ | |||
215 | 1 | Multiversion web service | ||
216 | 2 | ************************ | ||
217 | 3 | |||
218 | 4 | This web service demonstrates lazr.restful's ability to serve multiple | ||
219 | 5 | backwards-incompatible versions of a web service from the same | ||
220 | 6 | underlying code. | ||
221 | 0 | 7 | ||
222 | === added file 'src/lazr/restful/example/multiversion/__init__.py' | |||
223 | === added file 'src/lazr/restful/example/multiversion/resources.py' | |||
224 | --- src/lazr/restful/example/multiversion/resources.py 1970-01-01 00:00:00 +0000 | |||
225 | +++ src/lazr/restful/example/multiversion/resources.py 2009-11-16 15:00:27 +0000 | |||
226 | @@ -0,0 +1,35 @@ | |||
227 | 1 | __metaclass__ = type | ||
228 | 2 | |||
229 | 3 | __all__ = ['IKeyValuePair', | ||
230 | 4 | 'IPairSet', | ||
231 | 5 | 'KeyValuePair', | ||
232 | 6 | 'PairSet'] | ||
233 | 7 | |||
234 | 8 | from zope.schema import Text | ||
235 | 9 | from zope.location.interfaces import ILocation | ||
236 | 10 | |||
237 | 11 | from lazr.restful.declarations import ( | ||
238 | 12 | collection_default_content, export_as_webservice_collection, | ||
239 | 13 | export_as_webservice_entry, exported) | ||
240 | 14 | |||
241 | 15 | # We don't need separate implementations of these classes, so borrow | ||
242 | 16 | # the implementations from the WSGI example. | ||
243 | 17 | from lazr.restful.example.wsgi.resources import PairSet, KeyValuePair | ||
244 | 18 | |||
245 | 19 | # Our interfaces _will_ diverge from the WSGI example interfaces, so | ||
246 | 20 | # define them separately. | ||
247 | 21 | class IKeyValuePair(ILocation): | ||
248 | 22 | export_as_webservice_entry() | ||
249 | 23 | key = exported(Text(title=u"The key")) | ||
250 | 24 | value = exported(Text(title=u"The value")) | ||
251 | 25 | |||
252 | 26 | |||
253 | 27 | class IPairSet(ILocation): | ||
254 | 28 | export_as_webservice_collection(IKeyValuePair) | ||
255 | 29 | |||
256 | 30 | @collection_default_content() | ||
257 | 31 | def getPairs(): | ||
258 | 32 | """Return the key-value pairs.""" | ||
259 | 33 | |||
260 | 34 | def get(request, name): | ||
261 | 35 | """Retrieve a key-value pair by its key.""" | ||
262 | 0 | 36 | ||
263 | === added file 'src/lazr/restful/example/multiversion/root.py' | |||
264 | --- src/lazr/restful/example/multiversion/root.py 1970-01-01 00:00:00 +0000 | |||
265 | +++ src/lazr/restful/example/multiversion/root.py 2009-11-16 15:00:27 +0000 | |||
266 | @@ -0,0 +1,53 @@ | |||
267 | 1 | """The RESTful service root.""" | ||
268 | 2 | |||
269 | 3 | __metaclass__ = type | ||
270 | 4 | __all__ = [ | ||
271 | 5 | 'BelowRootAbsoluteURL', | ||
272 | 6 | 'RootAbsoluteURL', | ||
273 | 7 | 'WebServiceConfiguration', | ||
274 | 8 | 'MultiversionWebServiceRootResource', | ||
275 | 9 | ] | ||
276 | 10 | |||
277 | 11 | from zope.traversing.browser import AbsoluteURL | ||
278 | 12 | |||
279 | 13 | from lazr.restful.wsgi import BaseWSGIWebServiceConfiguration | ||
280 | 14 | from lazr.restful.simple import RootResource, RootResourceAbsoluteURL | ||
281 | 15 | from lazr.restful.example.multiversion.resources import ( | ||
282 | 16 | IKeyValuePair, PairSet, KeyValuePair) | ||
283 | 17 | |||
284 | 18 | |||
285 | 19 | class RootAbsoluteURL(RootResourceAbsoluteURL): | ||
286 | 20 | """A technique for generating the service's root URL. | ||
287 | 21 | |||
288 | 22 | This class contains no code of its own. It's defined so that | ||
289 | 23 | grok will pick it up. | ||
290 | 24 | """ | ||
291 | 25 | |||
292 | 26 | |||
293 | 27 | class BelowRootAbsoluteURL(AbsoluteURL): | ||
294 | 28 | """A technique for generating a root URL given an ILocation. | ||
295 | 29 | |||
296 | 30 | This class contains no code of its own. It's defined so that | ||
297 | 31 | grok will pick it up. | ||
298 | 32 | """ | ||
299 | 33 | |||
300 | 34 | |||
301 | 35 | class WebServiceConfiguration(BaseWSGIWebServiceConfiguration): | ||
302 | 36 | code_revision = '1' | ||
303 | 37 | active_versions = ['beta', '1.0', '2.0'] | ||
304 | 38 | latest_version_uri_prefix = 'trunk' | ||
305 | 39 | use_https = False | ||
306 | 40 | view_permission = 'zope.Public' | ||
307 | 41 | |||
308 | 42 | |||
309 | 43 | class MultiversionWebServiceRootResource(RootResource): | ||
310 | 44 | """The root resource for the WSGI example web service.""" | ||
311 | 45 | def _build_top_level_objects(self): | ||
312 | 46 | pairset = PairSet() | ||
313 | 47 | pairset.pairs = [ | ||
314 | 48 | KeyValuePair(self, "foo", "bar"), | ||
315 | 49 | KeyValuePair(self, "1", "2") | ||
316 | 50 | ] | ||
317 | 51 | collections = dict(pairs=(IKeyValuePair, pairset)) | ||
318 | 52 | return collections, {} | ||
319 | 53 | |||
320 | 0 | 54 | ||
321 | === added file 'src/lazr/restful/example/multiversion/site.zcml' | |||
322 | --- src/lazr/restful/example/multiversion/site.zcml 1970-01-01 00:00:00 +0000 | |||
323 | +++ src/lazr/restful/example/multiversion/site.zcml 2009-11-16 15:00:27 +0000 | |||
324 | @@ -0,0 +1,13 @@ | |||
325 | 1 | <configure | ||
326 | 2 | xmlns="http://namespaces.zope.org/zope" | ||
327 | 3 | xmlns:webservice="http://namespaces.canonical.com/webservice" | ||
328 | 4 | xmlns:grok="http://namespaces.zope.org/grok"> | ||
329 | 5 | |||
330 | 6 | <include package="lazr.restful" file="basic-site.zcml" /> | ||
331 | 7 | <webservice:register | ||
332 | 8 | module="lazr.restful.example.multiversion.resources" /> | ||
333 | 9 | <grok:grok package="lazr.restful.example.multiversion" /> | ||
334 | 10 | |||
335 | 11 | <securityPolicy | ||
336 | 12 | component="zope.security.simplepolicies.PermissiveSecurityPolicy" /> | ||
337 | 13 | </configure> | ||
338 | 0 | 14 | ||
339 | === added directory 'src/lazr/restful/example/multiversion/tests' | |||
340 | === added file 'src/lazr/restful/example/multiversion/tests/__init__.py' | |||
341 | === added file 'src/lazr/restful/example/multiversion/tests/introduction.txt' | |||
342 | --- src/lazr/restful/example/multiversion/tests/introduction.txt 1970-01-01 00:00:00 +0000 | |||
343 | +++ src/lazr/restful/example/multiversion/tests/introduction.txt 2009-11-16 15:00:27 +0000 | |||
344 | @@ -0,0 +1,64 @@ | |||
345 | 1 | Multi-version web services | ||
346 | 2 | ************************** | ||
347 | 3 | |||
348 | 4 | lazr.restful lets you publish two or more mutually incompatible | ||
349 | 5 | web services from the same underlying code. This lets you improve your | ||
350 | 6 | web service to take advantage of new features of lazr.restful, without | ||
351 | 7 | sacrificing backwards compatibility. | ||
352 | 8 | |||
353 | 9 | The web service in example/multiversion illustrates the multiversion | ||
354 | 10 | features of lazr.restful. | ||
355 | 11 | |||
356 | 12 | >>> from lazr.restful.testing.webservice import WebServiceCaller | ||
357 | 13 | >>> webservice = WebServiceCaller(domain='multiversion.dev') | ||
358 | 14 | |||
359 | 15 | The multiversion web service serves three named versions of the same | ||
360 | 16 | web service: "beta", "1.0", and "2.0". Once you make a request to the | ||
361 | 17 | service root of a particular version, the web service only serves you | ||
362 | 18 | links within that version. | ||
363 | 19 | |||
364 | 20 | >>> top_level_response = webservice.get( | ||
365 | 21 | ... "/", api_version="beta").jsonBody() | ||
366 | 22 | >>> print top_level_response['key_value_pairs_collection_link'] | ||
367 | 23 | http://multiversion.dev/beta/pairs | ||
368 | 24 | |||
369 | 25 | >>> top_level_response = webservice.get( | ||
370 | 26 | ... "/", api_version="1.0").jsonBody() | ||
371 | 27 | >>> print top_level_response['key_value_pairs_collection_link'] | ||
372 | 28 | http://multiversion.dev/1.0/pairs | ||
373 | 29 | |||
374 | 30 | >>> top_level_response = webservice.get( | ||
375 | 31 | ... "/", api_version="2.0").jsonBody() | ||
376 | 32 | >>> print top_level_response['key_value_pairs_collection_link'] | ||
377 | 33 | http://multiversion.dev/2.0/pairs | ||
378 | 34 | |||
379 | 35 | Like all web services, the multiversion service also serves a | ||
380 | 36 | development version which tracks the current state of the web service, | ||
381 | 37 | including all changes that have not yet been folded into a named | ||
382 | 38 | version. The default name for the development version is "devel" (see | ||
383 | 39 | example/base/tests/service.txt), but in this web service it's called | ||
384 | 40 | "trunk". | ||
385 | 41 | |||
386 | 42 | >>> top_level_response = webservice.get( | ||
387 | 43 | ... "/", api_version="trunk").jsonBody() | ||
388 | 44 | >>> print top_level_response['key_value_pairs_collection_link'] | ||
389 | 45 | http://multiversion.dev/trunk/pairs | ||
390 | 46 | |||
391 | 47 | All versions of the web service can be accessed through Ajax. | ||
392 | 48 | |||
393 | 49 | >>> from lazr.restful.testing.webservice import WebServiceAjaxCaller | ||
394 | 50 | >>> ajax = WebServiceAjaxCaller(domain='multiversion.dev') | ||
395 | 51 | |||
396 | 52 | >>> body = ajax.get('/', api_version="1.0").jsonBody() | ||
397 | 53 | >>> print body['resource_type_link'] | ||
398 | 54 | http://multiversion.dev/1.0/#service-root | ||
399 | 55 | |||
400 | 56 | >>> body = ajax.get('/', api_version="trunk").jsonBody() | ||
401 | 57 | >>> print body['resource_type_link'] | ||
402 | 58 | http://multiversion.dev/trunk/#service-root | ||
403 | 59 | |||
404 | 60 | An attempt to access a nonexistent version yields a 404 error. | ||
405 | 61 | |||
406 | 62 | >>> print webservice.get('/', api_version="no_such_version") | ||
407 | 63 | HTTP/1.1 404 Not Found | ||
408 | 64 | ... | ||
409 | 0 | 65 | ||
410 | === added file 'src/lazr/restful/example/multiversion/tests/test_integration.py' | |||
411 | --- src/lazr/restful/example/multiversion/tests/test_integration.py 1970-01-01 00:00:00 +0000 | |||
412 | +++ src/lazr/restful/example/multiversion/tests/test_integration.py 2009-11-16 15:00:27 +0000 | |||
413 | @@ -0,0 +1,53 @@ | |||
414 | 1 | # Copyright 2008 Canonical Ltd. All rights reserved. | ||
415 | 2 | |||
416 | 3 | """Test harness for doctests for lazr.restful multiversion example service.""" | ||
417 | 4 | |||
418 | 5 | __metaclass__ = type | ||
419 | 6 | __all__ = [] | ||
420 | 7 | |||
421 | 8 | import os | ||
422 | 9 | import doctest | ||
423 | 10 | from pkg_resources import resource_filename | ||
424 | 11 | |||
425 | 12 | from zope.component import getUtility | ||
426 | 13 | from van.testing.layer import zcml_layer, wsgi_intercept_layer | ||
427 | 14 | |||
428 | 15 | from lazr.restful.example.multiversion.root import ( | ||
429 | 16 | MultiversionWebServiceRootResource) | ||
430 | 17 | from lazr.restful.interfaces import IWebServiceConfiguration | ||
431 | 18 | from lazr.restful.simple import Publication | ||
432 | 19 | from lazr.restful.testing.webservice import WebServiceApplication | ||
433 | 20 | |||
434 | 21 | |||
435 | 22 | DOCTEST_FLAGS = ( | ||
436 | 23 | doctest.ELLIPSIS | | ||
437 | 24 | doctest.NORMALIZE_WHITESPACE | | ||
438 | 25 | doctest.REPORT_NDIFF) | ||
439 | 26 | |||
440 | 27 | |||
441 | 28 | class FunctionalLayer: | ||
442 | 29 | zcml = os.path.abspath(resource_filename( | ||
443 | 30 | 'lazr.restful.example.multiversion', 'site.zcml')) | ||
444 | 31 | zcml_layer(FunctionalLayer) | ||
445 | 32 | |||
446 | 33 | |||
447 | 34 | class WSGILayer(FunctionalLayer): | ||
448 | 35 | |||
449 | 36 | @classmethod | ||
450 | 37 | def make_application(self): | ||
451 | 38 | getUtility(IWebServiceConfiguration).hostname = "multiversion.dev" | ||
452 | 39 | getUtility(IWebServiceConfiguration).port = None | ||
453 | 40 | root = MultiversionWebServiceRootResource() | ||
454 | 41 | return WebServiceApplication(root, Publication) | ||
455 | 42 | wsgi_intercept_layer(WSGILayer) | ||
456 | 43 | |||
457 | 44 | |||
458 | 45 | def additional_tests(): | ||
459 | 46 | """See `zope.testing.testrunner`.""" | ||
460 | 47 | tests = sorted( | ||
461 | 48 | [name | ||
462 | 49 | for name in os.listdir(os.path.dirname(__file__)) | ||
463 | 50 | if name.endswith('.txt')]) | ||
464 | 51 | suite = doctest.DocFileSuite(optionflags=DOCTEST_FLAGS, *tests) | ||
465 | 52 | suite.layer = WSGILayer | ||
466 | 53 | return suite | ||
467 | 0 | 54 | ||
468 | === modified file 'src/lazr/restful/example/wsgi/root.py' | |||
469 | --- src/lazr/restful/example/wsgi/root.py 2009-11-12 16:43:57 +0000 | |||
470 | +++ src/lazr/restful/example/wsgi/root.py 2009-11-16 15:00:27 +0000 | |||
471 | @@ -34,7 +34,7 @@ | |||
472 | 34 | 34 | ||
473 | 35 | class WebServiceConfiguration(BaseWSGIWebServiceConfiguration): | 35 | class WebServiceConfiguration(BaseWSGIWebServiceConfiguration): |
474 | 36 | code_revision = '1' | 36 | code_revision = '1' |
476 | 37 | service_version_uri_prefix = '1.0' | 37 | active_versions = ['1.0'] |
477 | 38 | use_https = False | 38 | use_https = False |
478 | 39 | view_permission = 'zope.Public' | 39 | view_permission = 'zope.Public' |
479 | 40 | 40 | ||
480 | 41 | 41 | ||
481 | === modified file 'src/lazr/restful/interfaces/_rest.py' | |||
482 | --- src/lazr/restful/interfaces/_rest.py 2009-11-12 16:43:57 +0000 | |||
483 | +++ src/lazr/restful/interfaces/_rest.py 2009-11-16 15:00:27 +0000 | |||
484 | @@ -51,7 +51,7 @@ | |||
485 | 51 | 'IWebServiceLayer', | 51 | 'IWebServiceLayer', |
486 | 52 | ] | 52 | ] |
487 | 53 | 53 | ||
489 | 54 | from zope.schema import Bool, Int, TextLine | 54 | from zope.schema import Bool, Int, List, TextLine |
490 | 55 | from zope.interface import Attribute, Interface | 55 | from zope.interface import Attribute, Interface |
491 | 56 | # These two should really be imported from zope.interface, but | 56 | # These two should really be imported from zope.interface, but |
492 | 57 | # the import fascist complains because they are not in __all__ there. | 57 | # the import fascist complains because they are not in __all__ there. |
493 | @@ -417,15 +417,19 @@ | |||
494 | 417 | "put the URL prefix here. (In the example case, the URL prefix " | 417 | "put the URL prefix here. (In the example case, the URL prefix " |
495 | 418 | "is 'web-service/').") | 418 | "is 'web-service/').") |
496 | 419 | 419 | ||
506 | 420 | service_version_uri_prefix = TextLine( | 420 | active_versions = List( |
507 | 421 | default=u"", | 421 | value_type=TextLine(), |
508 | 422 | description=u"""The versioning string, if any, to use as the | 422 | default = [], |
509 | 423 | URI prefix for web service URIs. A popular string is 'beta', | 423 | title=u"The active versions of the web service.", |
510 | 424 | but you could also use a version number or the date the API | 424 | description = u"""A list of names of active versions of this |
511 | 425 | was finalized. | 425 | web service. They might be version numbers, names such as |
512 | 426 | 426 | "beta", or the date a particular version was finalized. | |
513 | 427 | The service version URI prefix shows up in URIs *after* any | 427 | |
514 | 428 | value for service_root_uri_prefix.""") | 428 | Newer versions should show up later in the list than earlier |
515 | 429 | versions. The most recent active version should be at the end | ||
516 | 430 | of the list. | ||
517 | 431 | |||
518 | 432 | Currently this list must contain at least one version name.""") | ||
519 | 429 | 433 | ||
520 | 430 | latest_version_uri_prefix = TextLine( | 434 | latest_version_uri_prefix = TextLine( |
521 | 431 | default=u"devel", | 435 | default=u"devel", |
522 | 432 | 436 | ||
523 | === modified file 'src/lazr/restful/publisher.py' | |||
524 | --- src/lazr/restful/publisher.py 2009-11-12 17:03:06 +0000 | |||
525 | +++ src/lazr/restful/publisher.py 2009-11-16 15:00:27 +0000 | |||
526 | @@ -231,11 +231,11 @@ | |||
527 | 231 | alsoProvides(self, IWebBrowserInitiatedRequest) | 231 | alsoProvides(self, IWebBrowserInitiatedRequest) |
528 | 232 | 232 | ||
529 | 233 | # Only accept versioned URLs. Either the | 233 | # Only accept versioned URLs. Either the |
532 | 234 | # service_version_uri_prefix or the | 234 | # latest_version_uri_prefix or one of the active_versions is |
533 | 235 | # latest_version_uri_prefix is acceptable. | 235 | # acceptable. |
534 | 236 | version = None | 236 | version = None |
537 | 237 | for version_string in [config.service_version_uri_prefix, | 237 | for version_string in ( |
538 | 238 | config.latest_version_uri_prefix]: | 238 | config.active_versions + [config.latest_version_uri_prefix]): |
539 | 239 | if version_string is not None: | 239 | if version_string is not None: |
540 | 240 | version = self._popTraversal(version_string) | 240 | version = self._popTraversal(version_string) |
541 | 241 | if version is not None: | 241 | if version is not None: |
542 | @@ -265,13 +265,17 @@ | |||
543 | 265 | 265 | ||
544 | 266 | @implementer(IWebServiceClientRequest) | 266 | @implementer(IWebServiceClientRequest) |
545 | 267 | @adapter(IBrowserRequest) | 267 | @adapter(IBrowserRequest) |
547 | 268 | def browser_request_to_web_service_request(website_request): | 268 | def browser_request_to_web_service_request( |
548 | 269 | website_request, web_service_version=None): | ||
549 | 269 | """An adapter from a browser request to a web service request. | 270 | """An adapter from a browser request to a web service request. |
550 | 270 | 271 | ||
551 | 271 | Used to instantiate Resource objects when handling normal web | 272 | Used to instantiate Resource objects when handling normal web |
552 | 272 | browser requests. | 273 | browser requests. |
553 | 273 | """ | 274 | """ |
554 | 274 | config = getUtility(IWebServiceConfiguration) | 275 | config = getUtility(IWebServiceConfiguration) |
555 | 276 | if web_service_version is None: | ||
556 | 277 | web_service_version = config.active_versions[-1] | ||
557 | 278 | |||
558 | 275 | body = website_request.bodyStream.getCacheStream().read() | 279 | body = website_request.bodyStream.getCacheStream().read() |
559 | 276 | environ = dict(website_request.environment) | 280 | environ = dict(website_request.environment) |
560 | 277 | # Zope picks up on SERVER_URL when setting the _app_server attribute | 281 | # Zope picks up on SERVER_URL when setting the _app_server attribute |
561 | @@ -279,6 +283,8 @@ | |||
562 | 279 | environ['SERVER_URL'] = website_request.getApplicationURL() | 283 | environ['SERVER_URL'] = website_request.getApplicationURL() |
563 | 280 | web_service_request = config.createRequest(body, environ) | 284 | web_service_request = config.createRequest(body, environ) |
564 | 281 | web_service_request.setVirtualHostRoot( | 285 | web_service_request.setVirtualHostRoot( |
566 | 282 | names=[config.path_override, config.service_version_uri_prefix]) | 286 | names=[config.path_override, web_service_version]) |
567 | 287 | web_service_request.annotations[web_service_request.VERSION_ANNOTATION] = ( | ||
568 | 288 | web_service_request) | ||
569 | 283 | web_service_request._vh_root = website_request.getVirtualHostRoot() | 289 | web_service_request._vh_root = website_request.getVirtualHostRoot() |
570 | 284 | return web_service_request | 290 | return web_service_request |
571 | 285 | 291 | ||
572 | === modified file 'src/lazr/restful/testing/webservice.py' | |||
573 | --- src/lazr/restful/testing/webservice.py 2009-11-12 16:30:50 +0000 | |||
574 | +++ src/lazr/restful/testing/webservice.py 2009-11-16 15:00:27 +0000 | |||
575 | @@ -163,7 +163,7 @@ | |||
576 | 163 | @property | 163 | @property |
577 | 164 | def default_api_version(self): | 164 | def default_api_version(self): |
578 | 165 | return getUtility( | 165 | return getUtility( |
580 | 166 | IWebServiceConfiguration).service_version_uri_prefix | 166 | IWebServiceConfiguration).active_versions[-1] |
581 | 167 | 167 | ||
582 | 168 | def getAbsoluteUrl(self, resource_path, api_version=None): | 168 | def getAbsoluteUrl(self, resource_path, api_version=None): |
583 | 169 | """Convenience method for creating a url in tests. | 169 | """Convenience method for creating a url in tests. |
This branch makes two major changes:
1. It introduces a new web service specifically for testing the multiversion code. This is pretty much a full copy of the WSGI test service, though I was able to import a few classes from the WSGI test service rather than re-defining them. (Many classes, even empty ones, had to be re-defined so that grok would handle them properly.)
Most of the multiversion tests in examples/ base/tests/ service. txt have been moved to examples/ multiversion/ tests/introduct ion.txt
2. It replaces the string service_ version_ uri_prefix (this is "beta" in the Launchpad web service) with a list of active_versions (this would be ["beta"] in the Launchpad web service). A request to any of the versions in active_versions will succeed, and although all versions still serve the exact same resources, the URLs to those resources will change depending on the version number.