diff mbox

[2/3] views/xmlrpc: Add xmlrpc automatic documentation

Message ID 1444447311-29799-3-git-send-email-stephen.finucane@intel.com
State Superseded
Headers show

Commit Message

Stephen Finucane Oct. 10, 2015, 3:21 a.m. UTC
The DocXMLRPCServer module (standard library) provides documentation
for XML-RPC dispatchers via pydoc. Use this module rather than
reinventing the wheel.

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

Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
---
 patchwork/tests/test_xmlrpc.py |  6 ------
 patchwork/views/xmlrpc.py      | 26 +++++++++++++++++++-------
 2 files changed, 19 insertions(+), 13 deletions(-)
diff mbox

Patch

diff --git a/patchwork/tests/test_xmlrpc.py b/patchwork/tests/test_xmlrpc.py
index b7c629e..2ed1ebf 100644
--- a/patchwork/tests/test_xmlrpc.py
+++ b/patchwork/tests/test_xmlrpc.py
@@ -36,12 +36,6 @@  class XMLRPCTest(LiveServerTestCase):
                     reverse('patchwork.views.xmlrpc.xmlrpc'))
         self.rpc = xmlrpclib.Server(self.url)
 
-    def testGetRedirect(self):
-        response = self.client.get(self.url)
-        self.assertRedirects(response,
-                reverse('patchwork.views.help',
-                    kwargs = {'path': 'pwclient/'}))
-
     def testList(self):
         defaults.project.save()
         defaults.patch_author_person.save()
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