Esempio n. 1
0
def _send_data(relative_url, obj):
    """
    Helper method that POSTs an object to URL as JSON.

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @param obj:          The object to be parsed to JSON and sent.
    @type obj:           object

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("POST", SERVER_URL + "/" + relative_url, headers={"Accept" : "application/json"}, data=json.dumps(obj))
    buf = resp.read()
    if resp.getStatus() >= 200 and resp.getStatus() <= 300:
        data = json.loads(buf)
    else:
        data = buf
    return (data, resp)
Esempio n. 2
0
def _get_data(relative_url):
    """
    Helper method accesses a URL on the server and returns the data (interprets the JSON).

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("GET", SERVER_URL + "/" + relative_url, headers={"Accept" : "application/json"})
    buf = resp.read()
    if resp.getStatus() == 200:
        data = json.loads(buf)
    else:
        data = buf
    return (data, resp)
Esempio n. 3
0
def _send_data(relative_url, obj):
    """
    Helper method that POSTs an object to URL as JSON.

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @param obj:          The object to be parsed to JSON and sent.
    @type obj:           object

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("POST",
                        SERVER_URL + "/" + relative_url,
                        headers={"Accept": "application/json"},
                        data=json.dumps(obj))
    buf = resp.read()
    if resp.getStatus() >= 200 and resp.getStatus() <= 300:
        data = json.loads(buf)
    else:
        data = buf
    return (data, resp)
Esempio n. 4
0
def _get_data(relative_url):
    """
    Helper method accesses a URL on the server and returns the data (interprets the JSON).

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("GET",
                        SERVER_URL + "/" + relative_url,
                        headers={"Accept": "application/json"})
    buf = resp.read()
    if resp.getStatus() == 200:
        data = json.loads(buf)
    else:
        data = buf
    return (data, resp)
Esempio n. 5
0
def _delete(relative_url):
    """
    Helper method that sends a DELETE message to the server.

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("DELETE", SERVER_URL + "/" + relative_url)
    buf = resp.read()
    return buf, resp
Esempio n. 6
0
def _delete(relative_url):
    """
    Helper method that sends a DELETE message to the server.

    @param relative_url: The relative URL on the server. A starting slash may be
                         specified, but either way, it's always interpreted to be
                         relative to "/".
    @type  relative_url: string

    @return:             The JSON interpreted data and the response object as a tuple.
    @rtype               tuple

    """
    if DOCROOT:
        if relative_url.startswith(DOCROOT):
            relative_url = relative_url[len(DOCROOT):]
    if relative_url.startswith("/"):
        relative_url = relative_url[1:]
    resp = http.urlopen("DELETE", SERVER_URL + "/" + relative_url)
    buf = resp.read()
    return buf, resp