def parse_hudsucker_request(data): """Parse request into Service Definition json v1:: {"service": "memcached", "params": {"key": "some_key", "value": "some_value", "expiry_minutes": "60"} } json v1.1:: {"app": "hudsucker", "service": "memcached", "version":"1.1", "expiry_minutes": "60", "params": {"key": "some_key", "value": "some_value"} } """ import simplejson sd = ServiceDefinition('tbd') #print('Input data: ' + data) command = simplejson.loads(data) params = command['params'] if 'version' not in command: # Legacy format before versioning if not command.has_key('service'): raise Exception('Requires service: {"service":"myservice", ...}') svc = str(command['service']) if svc in HUDSUCKER_SERVICES: sd.app = 'hudsucker' sd.name = svc else: sd.app = svc sd.name = str(params['widget']) sd.format = 'gadgetmini' if params.has_key('format'): sd.format = str(params.get('format')) if not params.has_key('request_path'): raise Exception('Remote Content Services Require a Request Path') else: sd.url_pattern = str(params['request_path']) if 'expiry_minutes' in params: sd.cache_time = float(params['expiry_minutes']) * 60 else: sd.cache_time = 0 sd.data = params elif str(command['version']) == '1.1': # new versionable request's if not command.has_key('app'): raise Exception('Requires an App to be specified: {"app": "hudsucker","version":"1.1",....') sd.app = str(command['app']) if not command.has_key('service'): raise Exception('Requires a Service to be specified: {"app": "hudsucker", "service":"myservice","version":"1.1",....') sd.name = str(command['service']) if command.has_key('url_pattern'): #print('setting sd.url_pattern = %s' % (command['url_pattern'])) sd.url_pattern = str(command['url_pattern']) if 'expiry_minutes' in command: sd.cache_time = float(command['expiry_minutes']) * 60 else: sd.cache_time = 0 sd.format = 'gadgetmini' if params.has_key('format'): sd.format = str(params.get('format')) if command.has_key('base_url'): sd.base_url = str(command['base_url']) if command.has_key('base_url') and command.has_key('url_pattern'): sd.isdefined = True sd.data = params else: raise Exception('Unknown Request Format') return sd