示例#1
0
def jsonRequest(obj, method, url, body=None, content_type="application/x-www-form-urlencoded",
                callback=None, accept=None, headers=None):
    """
    Create a request that expects a JSON response.

    The response can optionally be saved to a file-like object if
    the connection object has the _saveFile and _saveAccept attributes.

    Instead of being returned the response might be passed to a callback function.

    Raise an exception if the returned status is not in the 2XX range.

    :param obj: Service object with connection information (e.g. credentials).
    :type obj: franz.openrdf.miniclient.repository.Service
    :param method: Request method (``"GET"``, ``"POST"``, ...).
    :type method: string
    :param url: Target address
    :type url: string
    :param body: Request body (for PUT/POST requests) or query string, optional.
    :type body: basestring|file
    :param accept: Value of the accept header (default: ``"application/json"``)
    :type accept: string
    :param content_type: MIME type of the request body, optional.
    :type content_type: string
    :param headers: Either a dictionary mapping headers to values or
                    a list of strings that will be included in the request's headers.
    :type headers: Iterable[string] | dict[string, string] | None
    :return: Status code and response body, unless callback is specified (in that case None is returned).
    :param callback: A callback function that will be called for each response chunk (optional).
                     The return value should be either None or the number of bytes
                     received, anything else will cause the request to be aborted.
    :type callback: (bytestring) -> int

    :return: A parsed JSON response or ``None`` if the response was saved to a file or processed by a callback.
    :rtype: dict|string|int|float|None
    """
    if accept is None:
        accept = "application/json"

    # If there is a _saveFile and _saveAccept, they override the arguments
    if hasattr(obj, '_saveFile') and hasattr(obj, '_saveAccept'):
        accept = obj._saveAccept
        callback = obj._saveFile.write

    headers = merge_headers(obj.getHeaders(), headers)
    if callback is None:
        status, body = makeRequest(obj, method, url, body, accept, content_type,
                                   headers=headers)
        if status == 200:
            if accept in ('application/json', 'text/integer', "application/x-quints+json"):
                body = decode_json(body)
            return body
        else: raise RequestError(status, body)
    else:
        def raiseErr(status, message): raise RequestError(status, message)
        makeRequest(obj, method, url, body, accept, content_type, callback=callback, errCallback=raiseErr, headers=headers)
示例#2
0
 def takeArrayAt(start):
     scanned = start + 1
     while True:
         end = string.find("]", scanned)
         if end == -1: return False
         try:
             useArray(decode_json(string[start : end + 1].decode("utf-8")))
             pos[0] = end + 1
             return True
         except JsonDecodeError:
             scanned = end + 1
示例#3
0
def jsonRequest(obj, method, url, body=None, content_type="application/x-www-form-urlencoded",
                callback=None, accept=None, headers=None):
    """
    Create a request that expects a JSON response.

    The response can optionally be saved to a file-like object if
    the connection object has the _saveFile and _saveAccept attributes.

    Instead of being returned the response might be passed to a callback function.

    Raise an exception if the returned status is not in the 2XX range.

    :param obj: Service object with connection information (e.g. credentials).
    :type obj: franz.openrdf.miniclient.repository.Service
    :param method: Request method (``"GET"``, ``"POST"``, ...).
    :type method: string
    :param url: Target address
    :type url: string
    :param body: Request body (for PUT/POST requests) or query string, optional.
    :type body: basestring|file
    :param accept: Value of the accept header (default: ``"application/json"``)
    :type accept: string
    :param content_type: MIME type of the request body, optional.
    :type content_type: string
    :param headers: Either a dictionary mapping headers to values or
                    a list of strings that will be included in the request's headers.
    :type headers: Iterable[string] | dict[string, string] | None
    :return: Status code and response body, unless callback is specified (in that case None is returned).
    :param callback: A callback function that will be called for each response chunk (optional).
                     The return value should be either None or the number of bytes
                     received, anything else will cause the request to be aborted.
    :type callback: (bytestring) -> int

    :return: A parsed JSON response or ``None`` if the response was saved to a file or processed by a callback.
    :rtype: dict|string|int|float|None
    """
    if accept is None:
        accept = "application/json"

    # If there is a _saveFile and _saveAccept, they override the arguments
    if hasattr(obj, '_saveFile') and hasattr(obj, '_saveAccept'):
        accept = obj._saveAccept
        callback = obj._saveFile.write

    if callback is None:
        status, body = makeRequest(obj, method, url, body, accept, content_type, headers=headers)
        if status == 200:
            if accept in ('application/json', 'text/integer', "application/x-quints+json"):
                body = decode_json(body)
            return body
        else: raise RequestError(status, body)
    else:
        def raiseErr(status, message): raise RequestError(status, message)
        makeRequest(obj, method, url, body, accept, content_type, callback=callback, errCallback=raiseErr, headers=headers)