示例#1
0
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
                cursor = db.cursor()
                cursor.arraysize = 50
                sql = """
                    select s.url base_url, u.pattern url_pattern
                    from service_provider s, widget w, widget_url_pattern u
                    where s.name = :app_name and w.name = :service_name
                    """
                cursor.execute(sql, app_name=service.app, service_name=service.name)
                rows = cursor.fetchall()
                if rows:
                    service.base_url = rows[0][0]
                    print("found SDW.row  base_url = %s" % (service.base_url))

                else:
                    print("WARNING: No rows for app '%s' and service '%s'." % (service.app, service.name))
            except Exception, detail:
                print("WARNING: Can't load base URL and URL patterns from database: %s." % detail)
            finally:
                if db:
                    self.db.release(db)
            service_def = ServiceDefinition(service.name, app=service.app)
            service_def.base_url = service.base_url
            service_def.url_patterns = service.url_patterns
            ContentProxyFactory.cache_set(service_def_key, service_def)
        else:
            # now transfer service_def -> service
            service.base_url = service_def.base_url
            service.url_patterns = service_def.url_patterns

        return service