From: erickson Date: Sun, 22 Jul 2007 22:06:49 +0000 (+0000) Subject: added JSON gateway support X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=43bbde85f4edc494364d02ce70d57358ba192114;p=working%2FOpenSRF.git added JSON gateway support git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1056 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/python/osrf/gateway.py b/src/python/osrf/gateway.py index 98c8686..a3d8215 100644 --- a/src/python/osrf/gateway.py +++ b/src/python/osrf/gateway.py @@ -5,13 +5,16 @@ from net_obj import * 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() @@ -45,8 +48,33 @@ class GatewayRequest: 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):