Example #1
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
Example #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
Example #3
0
def default_renderer(format, authorized, content, name_hint):
    if format == "xml":
        return pyxml.dumps(content, obj_name=name_hint)
    elif format == "sld":
        return str(content)
    elif format == "html":
        url = web.ctx.path + web.ctx.query
        if url.endswith(".html"):
            url = url[:-5]
        urls = [(x, "%s.%s" % (url, x)) for x in authorized if x != "html"]
        templates = os.path.join(os.path.dirname(__file__), "templates/")
        render = web.template.render(templates)
        return render.response(web.ctx.home, web.ctx.path.split("/"), urls, pyhtml.dumps(content, obj_name=name_hint, indent=4))
    elif format == "json":
        return json.dumps({name_hint:content})
    else:
        return str(content)
Example #4
0
def default_renderer(format, authorized, content, name_hint):
    if format == "xml":
        return pyxml.dumps(content, obj_name=name_hint)
    elif format == "sld":
        return str(content)
    elif format == "html":
        url = web.ctx.path + web.ctx.query
        if url.endswith(".html"):
            url = url[:-5]
        urls = [(x, "%s.%s" % (url, x)) for x in authorized if x != "html"]
        templates = os.path.join(os.path.dirname(__file__), "templates/")
        render = web.template.render(templates)
        return render.response(
            web.ctx.home, web.ctx.path.split("/"), urls,
            pyhtml.dumps(content, obj_name=name_hint, indent=4))
    elif format == "json":
        return json.dumps({name_hint: content})
    else:
        return str(content)
Example #5
0
 def dumps(self, x):
     if isinstance(x, dict) and len(x) == 1:
         return pyxml.dumps(next(x.itervalues()), root=next(x.iterkeys()))
     return x
Example #6
0
 def dumps(self, x):
     if isinstance(x, dict) and len(x) == 1:
         return pyxml.dumps(next(x.itervalues()), root=next(x.iterkeys()))
     return x