Browse code
Merge pull request #7 from fiddlerwoaroof/master
... | ... |
@@ -47,49 +47,63 @@ collections.Mapping.register(UserDict.DictMixin) |
47 | 47 |
|
48 | 48 |
@public |
49 | 49 |
class ServerEvents(object): |
50 |
- '''Subclass this and pass to :py:meth:`jsonrpc.customize` to customize the jsonrpc server''' |
|
50 |
+ '''Subclass this and pass to :py:meth:`JSON_RPC.customize` to customize the JSON-RPC server''' |
|
51 | 51 |
|
52 | 52 |
def __init__(self, server): |
53 | 53 |
#: A link to the JSON-RPC server instance |
54 | 54 |
self.server = server |
55 | 55 |
|
56 | 56 |
def callmethod(self, txrequest, rpcrequest, **extra): |
57 |
- '''Finds the method and calls it with the specified args''' |
|
57 |
+ '''Find the method and call it with the specified args |
|
58 |
+ |
|
59 |
+ :returns: the result of the method''' |
|
58 | 60 |
method = self.findmethod(rpcrequest.method) |
59 | 61 |
if method is None: raise jsonrpc.common.MethodNotFound |
60 | 62 |
extra.update(rpcrequest.kwargs) |
61 | 63 |
|
62 | 64 |
return method(*rpcrequest.args, **extra) |
63 | 65 |
|
64 |
- def findmethod(self, method): |
|
65 |
- '''Override to allow server to define methods''' |
|
66 |
- return lambda *a, **kw: 'Test Data' |
|
66 |
+ def findmethod(self, method_name): |
|
67 |
+ '''Return the callable associated with the method name |
|
68 |
+ |
|
69 |
+ :returns: a callable''' |
|
70 |
+ raise NotImplementedError |
|
67 | 71 |
|
68 | 72 |
def processrequest(self, result, args, **kw): |
69 | 73 |
'''Override to implement custom handling of the method result and request''' |
70 | 74 |
return result |
71 | 75 |
|
72 | 76 |
def log(self, response, txrequest, error=False): |
73 |
- '''Override to implement custom error handling''' |
|
77 |
+ '''Override to implement custom logging''' |
|
74 | 78 |
pass |
75 | 79 |
|
76 | 80 |
def processcontent(self, content, request): |
77 |
- '''Given the freshly decoded content of the request, return what content should be used''' |
|
81 |
+ '''Given the freshly decoded content of the request, return the content that should be used |
|
82 |
+ |
|
83 |
+ :returns: an object which implements the :py:class:`collections.MutableMapping` interface''' |
|
78 | 84 |
return content |
79 | 85 |
|
80 | 86 |
def getresponsecode(self, result): |
87 |
+ '''Take the result, and return an appropriate HTTP response code, returns 200 by default |
|
88 |
+ |
|
89 |
+ NOTE: if an error code is returned, the client error messages will be much less helpful! |
|
90 |
+ |
|
91 |
+ :returns: :py:class:`int`''' |
|
81 | 92 |
# returns 200 so that the python client can see something useful |
82 | 93 |
return 200 |
94 |
+ |
|
83 | 95 |
# for example |
84 |
- #code = 200 |
|
85 |
- #if not isinstance(result, list): |
|
86 |
- # if result is not None and result.error is not None: |
|
87 |
- # code = result.error.code or 500 |
|
88 |
- #return code |
|
96 |
+ #def getresponsecode(self, result): |
|
97 |
+ # code = 200 |
|
98 |
+ # if not isinstance(result, list): |
|
99 |
+ # if result is not None and result.error is not None: |
|
100 |
+ # code = result.error.code or 500 |
|
101 |
+ # return code |
|
89 | 102 |
|
90 | 103 |
def defer(self, method, *a, **kw): |
91 |
- # Defer to thread. Override this method if you are using a different ThreadPool, |
|
92 |
- # or if you want to return immediately. |
|
104 |
+ '''Defer to thread. Override this method if you are using a different ThreadPool, or if you want to return immediately. |
|
105 |
+ |
|
106 |
+ :returns: :py:class:`twisted.internet.defer.Deferred`''' |
|
93 | 107 |
return threads.deferToThread(method, *a, **kw) |
94 | 108 |
|
95 | 109 |
|
... | ... |
@@ -100,7 +114,10 @@ class JSON_RPC(Resource): |
100 | 114 |
'''This class implements a JSON-RPC 2.0 server as a Twisted Resource''' |
101 | 115 |
isLeaf = True |
102 | 116 |
|
103 |
- #: set by :py:meth:`customize` used to change the behavior of the server |
|
117 |
+ ### NOTE: these comments are used by Sphinx as documentation. |
|
118 |
+ #: An instance of :py:class:`ServerEvents` which supplies callbacks to |
|
119 |
+ #: customize the operation of the server. The proper way to initialize this |
|
120 |
+ #: is either to subclass and set it manually, or, preferably, to call :py:meth:`customize`. |
|
104 | 121 |
eventhandler = ServerEvents |
105 | 122 |
|
106 | 123 |
def customize(self, eventhandler): |