예제 #1
0
 def wrapper (self, *args, **kwds):
     """Decorator wrapper"""
     cherrypy.response.headers['Content-Type'] = "application/json"
     func._cp_config = {'response.stream': True}
     head, data = func (self, *args, **kwds)
     yield json.dumps(head)[:-1] # do not yield }
     yield ', "data": ['
     if  isinstance(data, dict):
         for chunk in JSONEncoder().iterencode(data):
             yield chunk
     elif  isinstance(data, list) or isinstance(data, types.GeneratorType):
         sep = ''
         for rec in data:
             if  sep:
                 yield sep
             for chunk in JSONEncoder().iterencode(rec):
                 yield chunk
             if  not sep:
                 sep = ', '
     else:
         msg = 'jsonstreamer, improper data type %s' % type(data)
         raise Exception(msg)
     yield ']}'
예제 #2
0
파일: tools.py 프로젝트: tsarangi/WMCore
 def wrapper(self, *args, **kwds):
     """Decorator wrapper"""
     cherrypy.response.headers['Content-Type'] = "application/json"
     func._cp_config = {'response.stream': True}
     head, data = func(self, *args, **kwds)
     yield json.dumps(head)[:-1]  # do not yield }
     yield ', "data": ['
     if isinstance(data, dict):
         for chunk in JSONEncoder().iterencode(data):
             yield chunk
     elif isinstance(data, list) or isinstance(data, types.GeneratorType):
         sep = ''
         for rec in data:
             if sep:
                 yield sep
             for chunk in JSONEncoder().iterencode(rec):
                 yield chunk
             if not sep:
                 sep = ', '
     else:
         msg = 'jsonstreamer, improper data type %s' % type(data)
         raise Exception(msg)
     yield ']}'
예제 #3
0
def getdata(url, params, headers=None, post=None, verbose=False, jsondecoder=True):
    """
    Invoke URL call and retrieve data from data-service based
    on provided URL and set of parameters. Use post=True to
    invoke POST request.
    """
    encoded_data = urllib.urlencode(params)
    if  not post:
        if  encoded_data:
            url = url + '?' + encoded_data
    if  not headers:
        headers = {}
    if  verbose:
        print '+++ getdata, url=%s, headers=%s' % (url, headers)
    obj=sys.version_info
    if  obj[0] == 2 and obj[1] == 7 and obj[2] >= 9:
        # disable SSL verification, since it is default in python 2.7.9
        # and many CMS services do not verify SSL cert.
        # https://www.python.org/dev/peps/pep-0476/
        import ssl
        ssl._create_default_https_context = ssl._create_unverified_context
    req = urllib2.Request(url)
    for key, val in headers.iteritems():
        req.add_header(key, val)
    if  verbose > 1:
        handler = urllib2.HTTPHandler(debuglevel=1)
        opener  = urllib2.build_opener(handler)
        urllib2.install_opener(opener)
    ckey, cert = get_key_cert()
    handler = HTTPSClientAuthHandler(ckey, cert, verbose)
    if  verbose:
        print "handler", handler, handler.__dict__
    opener  = urllib2.build_opener(handler)
    urllib2.install_opener(opener)
    try:
        if  post:
            data = urllib2.urlopen(req, encoded_data)
        else:
            data = urllib2.urlopen(req)
        info = data.info()
        code = data.getcode()
        if  verbose > 1:
            print "+++ response code:", code
            print "+++ response info\n", info
        if  jsondecoder:
            data = json.load(data)
        else:
            data = data.read()
    except urllib2.HTTPError as httperror:
        msg  = 'HTTPError, url=%s, args=%s, headers=%s' \
                    % (url, params, headers)
        data = {'error': 'Unable to contact %s' % url , 'reason': msg}
        try:
            data.update({'httperror':extract_http_error(httperror.read())})
        except Exception as exp:
            data.update({'httperror': None})
        data = json.dumps(data)
    except Exception as exp:
        msg  = 'HTTPError, url=%s, args=%s, headers=%s, error=%s' \
                    % (url, params, headers, str(exp))
        data = {'error': 'Unable to contact %s' % url, 'reason': msg}
        data = json.dumps(data)
    return data
예제 #4
0
def getdata(url,
            params,
            headers=None,
            post=None,
            verbose=False,
            jsondecoder=True):
    """
    Invoke URL call and retrieve data from data-service based
    on provided URL and set of parameters. Use post=True to
    invoke POST request.
    """
    encoded_data = urllib.urlencode(params)
    if not post:
        if encoded_data:
            url = url + '?' + encoded_data
    if not headers:
        headers = {}
    if verbose:
        print '+++ getdata, url=%s, headers=%s' % (url, headers)
    obj = sys.version_info
    if obj[0] == 2 and obj[1] == 7 and obj[2] >= 9:
        # disable SSL verification, since it is default in python 2.7.9
        # and many CMS services do not verify SSL cert.
        # https://www.python.org/dev/peps/pep-0476/
        import ssl
        ssl._create_default_https_context = ssl._create_unverified_context
    req = urllib2.Request(url)
    for key, val in headers.iteritems():
        req.add_header(key, val)
    if verbose > 1:
        handler = urllib2.HTTPHandler(debuglevel=1)
        opener = urllib2.build_opener(handler)
        urllib2.install_opener(opener)
    ckey, cert = get_key_cert()
    handler = HTTPSClientAuthHandler(ckey, cert, verbose)
    if verbose:
        print "handler", handler, handler.__dict__
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)
    try:
        if post:
            data = urllib2.urlopen(req, encoded_data)
        else:
            data = urllib2.urlopen(req)
        info = data.info()
        code = data.getcode()
        if verbose > 1:
            print "+++ response code:", code
            print "+++ response info\n", info
        if jsondecoder:
            data = json.load(data)
        else:
            data = data.read()
    except urllib2.HTTPError as httperror:
        msg  = 'HTTPError, url=%s, args=%s, headers=%s' \
                    % (url, params, headers)
        data = {'error': 'Unable to contact %s' % url, 'reason': msg}
        try:
            data.update({'httperror': extract_http_error(httperror.read())})
        except Exception as exp:
            data.update({'httperror': None})
        data = json.dumps(data)
    except Exception as exp:
        msg  = 'HTTPError, url=%s, args=%s, headers=%s, error=%s' \
                    % (url, params, headers, str(exp))
        data = {'error': 'Unable to contact %s' % url, 'reason': msg}
        data = json.dumps(data)
    return data