Merge lp:~leonardr/lazr.restful/service-request-to-web-request into lp:lazr.restful

Proposed by Leonard Richardson
Status: Rejected
Rejected by: Leonard Richardson
Proposed branch: lp:~leonardr/lazr.restful/service-request-to-web-request
Merge into: lp:lazr.restful
Diff against target: None lines
To merge this branch: bzr merge lp:~leonardr/lazr.restful/service-request-to-web-request
Reviewer Review Type Date Requested Status
Gavin Panella Needs Information
Review via email: mp+7027@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Leonard Richardson (leonardr) wrote :

This branch adds the framework necessary for converting a web service request into a website request. I don't want to land it until I know this is what I need for Launchpad.

Revision history for this message
Gavin Panella (allenap) wrote :

Leonard, is this proposal still active? The branch looks to not have been touched since 3rd June.

review: Needs Information

Unmerged revisions

49. By Leonard Richardson

Merge from trunk.

48. By Leonard Richardson

Removed adapter registration for function that's no longer an adapter.

47. By Leonard Richardson

Cleanup and removed the registration for web_service_request_to_browser_request, which will never be used because it goes from the specific to the general.

46. By Leonard Richardson

Added createWebsiteRequest to IWebServiceConfiguration.

45. By Leonard Richardson

Okay, it appears to work, at any rate.

44. By Leonard Richardson

First stab at implementation.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/docs/webservice-request.txt'
2--- src/lazr/restful/docs/webservice-request.txt 2009-03-28 14:35:27 +0000
3+++ src/lazr/restful/docs/webservice-request.txt 2009-06-01 18:45:20 +0000
4@@ -6,15 +6,15 @@
5 website neeeds to be treated as a web service request. It's easy to
6 adapt an IBrowserRequest into an IIWebServiceClientRequest request.
7
8-This adapters uses the request factory from the IWebServiceConfiguration
9-utility.
10+This adapter uses a factory for web service requests defined in
11+IWebServiceConfiguration.createRequest.
12
13 >>> from lazr.restful.interfaces import (
14 ... IWebServiceConfiguration, IWebServiceClientRequest)
15 >>> from lazr.restful.publisher import (
16 ... browser_request_to_web_service_request)
17 >>> from lazr.restful.testing.webservice import (
18- ... WebServiceTestPublication, WebServiceTestRequest)
19+ ... TestPublication, WebServiceTestPublication, WebServiceTestRequest)
20 >>> from zope.interface import implements
21 >>> from zope.component import getSiteManager
22 >>> from zope.publisher.browser import TestRequest
23@@ -28,6 +28,12 @@
24 ... request = WebServiceTestRequest(body_stream, environ)
25 ... request.setPublication(WebServiceTestPublication(None))
26 ... return request
27+ ...
28+ ... def createWebsiteRequest(self, body_stream, environ):
29+ ... request = TestRequest(body_stream, environ)
30+ ... request.setPublication(TestPublication(None))
31+ ... return request
32+
33
34 >>> sm = getSiteManager()
35 >>> sm.registerUtility(SimpleWebServiceConfiguration())
36@@ -40,6 +46,29 @@
37 >>> request.getApplicationURL()
38 'http://cookbooks.dev/api/beta'
39
40+Adapting a web service request into a browser request
41+*****************************************************
42+
43+Sometimes it goes the other way: a web service request needs to be
44+treated as a browser request. The reverse transformation is also easy.
45+
46+This adapter uses a factory for website requests defined in
47+IWebServiceConfiguration.createWebsiteRequest.
48+
49+ >>> from lazr.restful.publisher import (
50+ ... web_service_request_to_browser_request)
51+ >>> from cStringIO import StringIO
52+
53+ >>> service_request = WebServiceTestRequest(
54+ ... StringIO(""), {'SERVER_URL': "http://cookbooks.dev/"})
55+
56+ >>> website_request = web_service_request_to_browser_request(
57+ ... service_request)
58+ >>> website_request
59+ <zope.publisher.browser.TestRequest...>
60+ >>> website_request.getApplicationURL()
61+ 'http://cookbooks.dev'
62+
63 ==============
64 The JSON Cache
65 ==============
66
67=== modified file 'src/lazr/restful/interfaces/_rest.py'
68--- src/lazr/restful/interfaces/_rest.py 2009-04-15 19:05:23 +0000
69+++ src/lazr/restful/interfaces/_rest.py 2009-06-01 18:07:47 +0000
70@@ -435,6 +435,19 @@
71 :param environ: A dict containing the request environment.
72 """
73
74+ def createWebsiteRequest(body_instream, environ):
75+ """A factory method that creates a request for the website.
76+
77+ This is necessary when converting web service requests into
78+ website requests.
79+
80+ It should have the correct publication set for the application.
81+
82+ :param body_instream: A file-like object containing the request
83+ input stream.
84+ :param environ: A dict containing the request environment.
85+ """
86+
87 def get_request_user():
88 """The user who made the current web service request.
89
90
91=== modified file 'src/lazr/restful/publisher.py'
92--- src/lazr/restful/publisher.py 2009-03-27 04:25:34 +0000
93+++ src/lazr/restful/publisher.py 2009-06-01 18:43:23 +0000
94@@ -11,6 +11,7 @@
95 'browser_request_to_web_service_request',
96 'WebServicePublicationMixin',
97 'WebServiceRequestTraversal',
98+ 'web_service_request_to_browser_request',
99 ]
100
101 import urllib
102@@ -250,3 +251,19 @@
103 names=[config.path_override, config.service_version_uri_prefix])
104 web_service_request._vh_root = website_request.getVirtualHostRoot()
105 return web_service_request
106+
107+
108+def web_service_request_to_browser_request(web_service_request):
109+ """An adapter from a web service request to a browser request.
110+
111+ Used when generating XHTML representations (and, in the future,
112+ the web_link for JSON representations).
113+ """
114+ config = getUtility(IWebServiceConfiguration)
115+ body = web_service_request.bodyStream.getCacheStream().read()
116+ environ = dict(web_service_request.environment)
117+ # Zope picks up on SERVER_URL when setting the _app_server attribute
118+ # of the new request.
119+ environ['SERVER_URL'] = web_service_request.getApplicationURL()
120+ website_request = config.createWebsiteRequest(body, environ)
121+ return website_request

Subscribers

People subscribed via source and target branches