]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
views/xmlrpc: Add xmlrpc automatic documentation
authorStephen Finucane <stephen.finucane@intel.com>
Sat, 10 Oct 2015 02:50:53 +0000 (03:50 +0100)
committerStephen Finucane <stephen.finucane@intel.com>
Mon, 26 Oct 2015 21:03:03 +0000 (21:03 +0000)
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@intel.com>
patchwork/tests/test_xmlrpc.py
patchwork/views/xmlrpc.py

index b7c629e6b658dedeab2226918409dac8009ae98c..d37c281d517a3d2a206d0e5f9dadf7bfbce79e69 100644 (file)
@@ -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/'}))
index 8715dea937bb71658bccd6e62bc3609a4cad3322..876641991e02cf9d55302524fb5b605769ac9b29 100644 (file)
@@ -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