示例#1
0
def request(method, path, params=None, payload=None,
            parser=None, headers=None):
    """Create a generic handler for passing to specific request methods.

    :param method: the request method to execute
    :param path: path after server and port (i.e. /api/v1/credentials)
    :param params: uri encoding params (i.e. ?param1=hello&param2=world)
    :param payload: dictionary of payload to be posted
    :param parser: parser for printing usage on failure
    :param headers: headers to include
    :returns: reponse object
    :raises: AssertionError error if method is not supported
    """
    # grab the cli command for the log if the parser is provided
    log_command = None
    if parser is not None:
        log_command = parser.prog
    req_headers = {}
    token = read_client_token()
    # create the url by adding the path to the configured server location
    url = get_server_location() + path
    if headers:
        req_headers.update(headers)
    if token:
        req_headers['Authorization'] = 'Token {}'.format(token)

    try:
        if method == POST:
            result = handle_general_errors(post(url, payload, req_headers))
        elif method == GET:
            result = handle_general_errors(get(url, params, req_headers))
        elif method == PATCH:
            result = handle_general_errors(patch(url, payload, req_headers))
        elif method == DELETE:
            result = handle_general_errors(delete(url, req_headers))
        elif method == PUT:
            result = handle_general_errors(put(url, payload, req_headers))
        else:
            log.error('Unsupported request method %s', method)
            parser.print_help()
            sys.exit(1)
        try:
            log_request_info(method, log_command,
                             url, result.json(), result.status_code)
        except ValueError:
            log_request_info(method, log_command,
                             url, result.text, result.status_code)
        return result
    except requests.exceptions.SSLError as ssl_error:
        print(_(SSL_ERROR_MSG))
        log.error(ssl_error)
        if parser is not None:
            parser.print_help()
        sys.exit(1)
    except requests.exceptions.ConnectionError as conn_err:
        print(_(CONNECTION_ERROR_MSG))
        log.error(conn_err)
        if parser is not None:
            parser.print_help()
        sys.exit(1)
示例#2
0
文件: request.py 项目: wbclark/qpc
def request(method, path, params=None, payload=None,
            parser=None, headers=None, min_server_version=QPC_MIN_SERVER_VERSION):
    """Create a generic handler for passing to specific request methods.

    :param method: the request method to execute
    :param path: path after server and port (i.e. /api/v1/credentials)
    :param params: uri encoding params (i.e. ?param1=hello&param2=world)
    :param payload: dictionary of payload to be posted
    :param parser: parser for printing usage on failure
    :param headers: headers to include
    :param min_server_version: min qpc server version allowed
    :returns: reponse object
    :raises: AssertionError error if method is not supported
    """
    # grab the cli command for the log if the parser is provided
    log_command = None
    if parser is not None:
        log_command = parser.prog
    req_headers = {}
    token = read_client_token()
    # create the url by adding the path to the configured server location
    url = get_server_location() + path
    if headers:
        req_headers.update(headers)
    if token:
        req_headers['Authorization'] = 'Token {}'.format(token)

    try:
        if method == POST:
            result = handle_general_errors(post(url, payload, req_headers), min_server_version)
        elif method == GET:
            result = handle_general_errors(get(url, params, req_headers), min_server_version)
        elif method == PATCH:
            result = handle_general_errors(patch(url, payload, req_headers), min_server_version)
        elif method == DELETE:
            result = handle_general_errors(delete(url, req_headers), min_server_version)
        elif method == PUT:
            result = handle_general_errors(put(url, payload, req_headers), min_server_version)
        else:
            log.error('Unsupported request method %s', method)
            parser.print_help()
            sys.exit(1)
        try:
            log_request_info(method, log_command,
                             url, result.json(), result.status_code)
        except ValueError:
            log_request_info(method, log_command,
                             url, result.text, result.status_code)
        return result
    except (requests.exceptions.ConnectionError, requests.exceptions.SSLError):
        config = read_server_config()
        if config is not None:
            protocol = 'https'
            host = config.get(CONFIG_HOST_KEY)
            port = config.get(CONFIG_PORT_KEY)
            if config.get(CONFIG_USE_HTTP):
                protocol = 'http'
            log.error(_(CONNECTION_ERROR_MSG % (protocol, host, port)))
            log.error(_(messages.SERVER_CONFIG_REQUIRED % PKG_NAME))
        else:
            log.error(_(messages.SERVER_CONFIG_REQUIRED % PKG_NAME))
        sys.exit(1)