Beispiel #1
0
def get_data(name=None, mandatory=[], authorized=[], forbidden=[]):
    data = web.data()

    if not 'CONTENT_TYPE' in web.ctx.env:
        raise web.badrequest('You must specify a Content-Type.')

    ctype = web.ctx.env.get('CONTENT_TYPE')

    try:
        if 'text/xml' in ctype or  'application/xml' in ctype:
            data, dname = pyxml.loads(data, retname=True)
            if name and dname != name: data = None
        elif 'application/json' in ctype:
            data = json.loads(data)
            if name: data = data.get(name, None)
        else:
            raise web.badrequest('Content-type \'%s\' is not allowed.' % ctype)
    except ValueError:
        raise web.badrequest('Could not decode input data.' % mandatory)

    if name and data == None:
        raise web.badrequest('The object you are sending does not contain a \'%s\' entry.' % name)

    if not all(x in data for x in mandatory):
        raise web.badrequest('The following elements are missing, %s' % [x for x in mandatory if x not in data])
    if any(x in data for x in forbidden):
        raise web.badrequest('You are not allowed to send any of %s' % [x for x in forbidden if x in data])
    if authorized and any(x not in authorized for x in data):
        raise web.badrequest('You are not allowed to send any of %s' % ([x for x in data if x not in authorized]))

    return data
Beispiel #2
0
def APIRequest(method,
               url,
               data=None,
               encode=default_encoding,
               decode=default_encoding,
               content_type=None,
               expected_type=None,
               get_response=False):

    if encode == "json":
        data = json.dumps(data)
    elif encode == "xml":
        data = pyxml.dumps(data)

    if content_type == None:
        content_type = deduce_content_type(encode)

    surl = httplib.urlsplit(url)

    if encode and not url.endswith("." + encode):
        url = surl.path + "." + encode
    else:
        url = surl.path

    if surl.query:
        url += "?" + surl.query

    print >> sys.stderr, method, surl.geturl().replace(surl.path, url)
    conn = httplib.HTTPConnection(surl.hostname, surl.port)
    conn.request(method,
                 url,
                 body=data,
                 headers={"Content-Type": content_type})

    r = conn.getresponse()

    if expected_type == None:
        expected_type = deduce_content_type(decode)

    # TODO: enable this test once it is suported.
    # assert expected_type in r.getheader("Content-Type"), "received %s instead of %s" % (
    #     r.getheader("Content-Type"), expected_type)

    recv = r.read()

    try:
        if decode == "json":
            recv = json.loads(recv)
        elif decode == "xml":
            recv = pyxml.loads(recv)
    except:
        pass

    print >> sys.stderr, r.status, r.reason
    assert 200 <= r.status < 300, recv

    return (recv, r) if get_response else recv
Beispiel #3
0
def APIRequest(method, url, data=None, encode=default_encoding, decode=default_encoding, content_type=None, expected_type=None,
               get_response=False):

    if encode == "json":
        data = json.dumps(data)
    elif encode == "xml":
        data = pyxml.dumps(data)

    if content_type == None:
        content_type = deduce_content_type(encode)

    surl = httplib.urlsplit(url)

    if encode and not url.endswith("." + encode):
        url = surl.path + "." + encode
    else:
        url = surl.path

    if surl.query:
        url += "?" + surl.query

    print >>sys.stderr, method, surl.geturl().replace(surl.path, url)
    conn = httplib.HTTPConnection(surl.hostname, surl.port)
    conn.request(method, url, body=data, headers={"Content-Type":content_type})

    r = conn.getresponse()

    if expected_type == None:
        expected_type = deduce_content_type(decode)

    # TODO: enable this test once it is suported.
    # assert expected_type in r.getheader("Content-Type"), "received %s instead of %s" % (
    #     r.getheader("Content-Type"), expected_type)

    recv = r.read()

    try:
        if decode == "json":
            recv = json.loads(recv)
        elif decode == "xml":
            recv = pyxml.loads(recv)
    except:
        pass

    print >>sys.stderr, r.status, r.reason
    assert 200 <= r.status < 300, recv

    return (recv, r) if get_response else recv
Beispiel #4
0
def get_data(name=None, mandatory=[], authorized=[], forbidden=[]):
    data = web.data()

    if not data:
        raise web.badrequest(
            "You must suply some data. (mandatory: %s, authorized: %s)" %
            (mandatory, authorized))

    if "CONTENT_TYPE" not in web.ctx.env:
        raise web.badrequest("You must specify a Content-Type.")

    ctype = web.ctx.env.get("CONTENT_TYPE")

    try:
        if "text/xml" in ctype or "application/xml" in ctype:
            data, dname = pyxml.loads(data, retname=True)
            print("received \"%s\"" % dname)
            print(data)
            if name and dname != name:
                data = None
        elif "application/json" in ctype:
            data = json.loads(data.decode())
            if name:
                data = data.get(name, None)
        else:
            raise web.badrequest("Content-type \"%s\" is not allowed." % ctype)
    except (AttributeError, ValueError):
        raise web.badrequest("Could not decode input data (%s)." % data)

    if name and data is None:
        raise web.badrequest(
            "The object you are sending does not contain a \"%s\" entry." %
            name)

    if not all(x in data for x in mandatory):
        raise web.badrequest("The following elements are missing, \"%s\"" %
                             [x for x in mandatory if x not in data])
    if any(x in data for x in forbidden):
        raise web.badrequest("You are not allowed to send any of \"%s\"" %
                             [x for x in forbidden if x in data])
    if authorized and any(x not in authorized for x in data):
        raise web.badrequest("You are not allowed to send any of \"%s\"" %
                             ([x for x in data if x not in authorized]))

    return data
Beispiel #5
0
def get_data(name=None, mandatory=[], authorized=[], forbidden=[]):
    data = web.data()

    if not data:
        raise web.badrequest("You must suply some data. (mandatory: %s, authorized: %s)" % (mandatory, authorized))

    if not "CONTENT_TYPE" in web.ctx.env:
        raise web.badrequest("You must specify a Content-Type.")

    ctype = web.ctx.env.get("CONTENT_TYPE")

    try:
        if "text/xml" in ctype or  "application/xml" in ctype:
            data, dname = pyxml.loads(data, retname=True)
            print "received \"%s\"" % dname
            print data
            if name and dname != name: data = None
        elif "application/json" in ctype:
            data = json.loads(data)
            if name: data = data.get(name, None)
        else:
            raise web.badrequest("Content-type \"%s\" is not allowed." % ctype)
    except (AttributeError, ValueError):
        raise web.badrequest("Could not decode input data (%s)." % data)

    if name and data == None:
        raise web.badrequest("The object you are sending does not contain a \"%s\" entry." % name)

    if not all(x in data for x in mandatory):
        raise web.badrequest("The following elements are missing, \"%s\"" % [x for x in mandatory if x not in data])
    if any(x in data for x in forbidden):
        raise web.badrequest("You are not allowed to send any of \"%s\"" % [x for x in forbidden if x in data])
    if authorized and any(x not in authorized for x in data):
        raise web.badrequest("You are not allowed to send any of \"%s\"" % ([x for x in data if x not in authorized]))

    return data
Beispiel #6
0
 def loads(self, x):
     return pyxml.loads(x)
Beispiel #7
0
 def loads(self, x):
     return pyxml.loads(x)