[Opensrf-commits] r1056 - trunk/src/python/osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Jul 22 18:07:25 EDT 2007


Author: erickson
Date: 2007-07-22 18:06:49 -0400 (Sun, 22 Jul 2007)
New Revision: 1056

Modified:
   trunk/src/python/osrf/gateway.py
Log:
added JSON gateway support

Modified: trunk/src/python/osrf/gateway.py
===================================================================
--- trunk/src/python/osrf/gateway.py	2007-07-21 20:50:19 UTC (rev 1055)
+++ trunk/src/python/osrf/gateway.py	2007-07-22 22:06:49 UTC (rev 1056)
@@ -5,14 +5,17 @@
 import urllib, urllib2, sys, re
 
 defaultHost = None
-#paramRegex = re.compile('\%27')
 
 class GatewayRequest:
     def __init__(self, service, method, params=[]):
         self.service = service
         self.method = method
         self.params = params
+        self.path = 'gateway'
 
+    def setPath(self, path):
+        self.path = path
+
     def send(self):
         params = self.buildPOSTParams()
         request = urllib2.Request(self.buildURL(), data=params)
@@ -45,9 +48,34 @@
     setDefaultHost = staticmethod(setDefaultHost)
 
     def buildURL(self):
-        return 'http://%s/gateway' % defaultHost
+        return 'http://%s/%s' % (defaultHost, self.path)
 
+class JSONGatewayRequest(GatewayRequest):
+    def __init__(self, service, method, *params):
+        GatewayRequest.__init__(self, service, method, list(params))
 
+    def getFormat(self):
+        return 'json'
+
+    def getInputFormat(self):
+        return self.getFormat()
+
+    def handleResponse(self, response):
+        s = response.read()
+        obj = osrfJSONToObject(s)
+        if obj['status'] != 200:
+            sys.stderr.write('JSON gateway returned status %d:\n%s\n' % (obj['status'], s))
+            return None
+
+        # the gateway wraps responses in an array to handle streaming data
+        # if there is only one item in the array, it (probably) wasn't a streaming request
+        p = obj['payload']
+        if len(p) > 1: return p
+        return p[0]
+
+    def encodeParam(self, param):
+        return osrfObjectToJSON(param)
+
 class XMLGatewayRequest(GatewayRequest):
 
     def __init__(self, service, method, *params):



More information about the opensrf-commits mailing list