[PATCH v2 2/6] views/xmlrpc: Add xmlrpc automatic documentation

Stephen Finucane stephen.finucane at intel.com
Tue Oct 13 05:54:35 AEDT 2015


The DocXMLRPCServer module (standard library) provides documentation
for XML-RPC dispatchers via pydoc. Use this module rather than
reinventing the wheel.

Also update a test that no longer makes sense (the GET method now
retrieves documentation - it shouldn't redirect. Use PATCH instead).

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>

--

v2:
- Don't remove the test - replace it with a PATCH test.
---
 patchwork/tests/test_xmlrpc.py |  2 +-
 patchwork/views/xmlrpc.py      | 26 +++++++++++++++++++-------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/patchwork/tests/test_xmlrpc.py b/patchwork/tests/test_xmlrpc.py
index b7c629e..d37c281 100644
--- a/patchwork/tests/test_xmlrpc.py
+++ b/patchwork/tests/test_xmlrpc.py
@@ -37,7 +37,7 @@ class XMLRPCTest(LiveServerTestCase):
         self.rpc = xmlrpclib.Server(self.url)
 
     def testGetRedirect(self):
-        response = self.client.get(self.url)
+        response = self.client.patch(self.url)
         self.assertRedirects(response,
                 reverse('patchwork.views.help',
                     kwargs = {'path': 'pwclient/'}))
diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
index 8715dea..8766419 100644
--- a/patchwork/views/xmlrpc.py
+++ b/patchwork/views/xmlrpc.py
@@ -21,6 +21,7 @@
 #
 
 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
+from DocXMLRPCServer import XMLRPCDocGenerator
 import base64
 import sys
 import xmlrpclib
@@ -35,11 +36,16 @@ from patchwork.models import Patch, Project, Person, State
 from patchwork.views import patch_to_mbox
 
 
-class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher):
+class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher,
+                                XMLRPCDocGenerator):
+
+    server_name = 'Patchwork XML-RPC API'
+    server_title = 'Patchwork XML-RPC API v1 Documentation'
 
     def __init__(self):
         SimpleXMLRPCDispatcher.__init__(self, allow_none=False,
                                         encoding=None)
+        XMLRPCDocGenerator.__init__(self)
 
         def _dumps(obj, *args, **kwargs):
             kwargs['allow_none'] = self.allow_none
@@ -52,6 +58,7 @@ class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher):
         self.func_map = {}
 
     def register_function(self, fn, auth_required):
+        self.funcs[fn.__name__] = fn  # needed by superclass methods
         self.func_map[fn.__name__] = (auth_required, fn)
 
     def _user_for_request(self, request):
@@ -120,16 +127,21 @@ dispatcher = PatchworkXMLRPCDispatcher()
 
 @csrf_exempt
 def xmlrpc(request):
-    if request.method != 'POST':
+    if request.method not in ['POST', 'GET']:
         return HttpResponseRedirect(urlresolvers.reverse(
             'patchwork.views.help', kwargs={'path': 'pwclient/'}))
 
     response = HttpResponse()
-    try:
-        ret = dispatcher._marshaled_dispatch(request)
-        response.write(ret)
-    except Exception:
-        return HttpResponseServerError()
+
+    if request.method == 'POST':
+        try:
+            ret = dispatcher._marshaled_dispatch(request)
+        except Exception:
+            return HttpResponseServerError()
+    else:
+        ret = dispatcher.generate_html_documentation()
+
+    response.write(ret)
 
     return response
 
-- 
2.0.0



More information about the Patchwork mailing list