Merge lp:~jvalenzuela1977/txjsonrpc/devel into lp:txjsonrpc

Proposed by Jason Valenzuela
Status: Needs review
Proposed branch: lp:~jvalenzuela1977/txjsonrpc/devel
Merge into: lp:txjsonrpc
Diff against target: 88 lines (+43/-3)
2 files modified
txjsonrpc/web/jsonrpc.py (+35/-1)
txjsonrpc/web/test/test_jsonrpc.py (+8/-2)
To merge this branch: bzr merge lp:~jvalenzuela1977/txjsonrpc/devel
Reviewer Review Type Date Requested Status
Duncan McGreggor Pending
Review via email: mp+32049@code.launchpad.net
To post a comment you must log in.

Unmerged revisions

77. By Jason Valenzuela

Added decorator class allowing published methods to receive request objects.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'txjsonrpc/web/jsonrpc.py'
2--- txjsonrpc/web/jsonrpc.py 2009-11-09 06:31:56 +0000
3+++ txjsonrpc/web/jsonrpc.py 2010-08-08 13:37:43 +0000
4@@ -63,6 +63,37 @@
5 self.result.errback(NotImplementedError("Implement run() in subclasses"))
6
7
8+class IncludeRequest(object):
9+ """
10+ A decorator which can be used to inform the JSONRPC resource that
11+ the decorated method call wants access to the request object which
12+ dispatched the RPC. To illustrate usage, consider the following published
13+ method which accepts three arguments:
14+
15+ jsonrpc_spam(self, x, y, z)
16+
17+ Decorated with this class or derived subclass, the method will now
18+ receive the request object followed by the arguments provided by the
19+ caller.
20+
21+ @IncludeRequest
22+ jsonrpc_spam(self, request, x, y, z)
23+ """
24+
25+ def __init__(self, method):
26+ self.method = method
27+
28+ def __call__(self, *args, **kw):
29+ """Dispatches the RPC with the included request object.
30+
31+ args -- Normal arguments supplied by the caller.
32+
33+ kw -- Dictionary containing the request object under key value 'req'.
34+
35+ """
36+ return self.method(self, kw['req'], *args)
37+
38+
39 class JSONRPC(resource.Resource, BaseSubhandler):
40 """
41 A resource that implements JSON-RPC.
42@@ -107,7 +138,10 @@
43 self._cbRender(f, request, id, version)
44 else:
45 request.setHeader("content-type", "text/json")
46- d = defer.maybeDeferred(function, *args)
47+ if isinstance(function, IncludeRequest):
48+ d = defer.maybeDeferred(function, *args, req=request)
49+ else:
50+ d = defer.maybeDeferred(function, *args)
51 d.addErrback(self._ebRender, id)
52 d.addCallback(self._cbRender, request, id, version)
53 return server.NOT_DONE_YET
54
55=== modified file 'txjsonrpc/web/test/test_jsonrpc.py'
56--- txjsonrpc/web/test/test_jsonrpc.py 2009-11-09 06:10:20 +0000
57+++ txjsonrpc/web/test/test_jsonrpc.py 2010-08-08 13:37:43 +0000
58@@ -74,6 +74,10 @@
59 def jsonrpc_none(self):
60 return "null"
61
62+ @jsonrpc.IncludeRequest
63+ def jsonrpc_request(self, request, arg):
64+ return isinstance(request, server.Request) and arg
65+
66 def _getFunction(self, functionPath):
67 try:
68 return jsonrpc.JSONRPC._getFunction(self, functionPath)
69@@ -124,7 +128,8 @@
70 1,
71 ["a", 1],
72 "null",
73- {"a": ["b", "c", 12, []], "D": "foo"}]
74+ {"a": ["b", "c", 12, []], "D": "foo"},
75+ True]
76
77 def testResults(self):
78 input = [
79@@ -133,7 +138,8 @@
80 ("dict", ({"a": 1}, "a")),
81 ("pair", ("a", 1)),
82 ("none", ()),
83- ("complex", ())]
84+ ("complex", ()),
85+ ("request", (True,))]
86
87 dl = []
88 for methodAndArgs, output in zip(input, self.getExpectedOutput()):

Subscribers

People subscribed via source and target branches

to all changes: