Example #1
0
def do_request(
    method: str,
    host: str,
    path: str,
    params: Optional[Dict[str, Any]] = None,
    body: Optional[Dict[str, Any]] = None,
    headers: Optional[Dict[str, str]] = None,
    authenticated: bool = True,
    auth: Optional[authentication.Authentication] = None,
    cert: Optional[certs.Cert] = None,
    stream: bool = False,
) -> requests.Response:
    # If no explicit Authentication object was provided, use the cli's singleton Authentication.
    if auth is None:
        auth = authentication.cli_auth
    if cert is None:
        cert = certs.cli_cert

    if headers is None:
        h = {}  # type: Dict[str, str]
    else:
        h = headers

    if params is None:
        params = {}

    if authenticated:
        h = add_token_to_headers(h, auth)

    try:
        r = determined.common.requests.request(
            method,
            make_url(host, path),
            params=params,
            json=body,
            headers=h,
            verify=cert.bundle if cert else None,
            stream=stream,
            server_hostname=cert.name if cert else None,
        )
    except requests.exceptions.SSLError:
        raise
    except requests.exceptions.ConnectionError as e:
        raise errors.MasterNotFoundException(str(e))
    except requests.exceptions.RequestException as e:
        raise errors.BadRequestException(str(e))

    if r.status_code == 403:
        username = ""
        if auth is not None:
            username = auth.get_session_user()
        raise errors.UnauthenticatedException(username=username)
    elif r.status_code == 404:
        raise errors.NotFoundException(r)
    elif r.status_code >= 300:
        raise errors.APIException(r)

    return r
Example #2
0
def do_request(
    method: str,
    host: str,
    path: str,
    params: Optional[Dict[str, Any]] = None,
    body: Optional[Dict[str, Any]] = None,
    headers: Optional[Dict[str, str]] = None,
    authenticated: bool = True,
    stream: bool = False,
) -> requests.Response:
    if headers is None:
        h = {}  # type: Dict[str, str]
    else:
        h = headers

    if params is None:
        params = {}

    if authenticated:
        h = add_token_to_headers(h)

    try:
        r = determined.common.requests.request(
            method,
            make_url(host, path),
            params=params,
            json=body,
            headers=h,
            verify=_master_cert_bundle,
            stream=stream,
            server_hostname=_master_cert_name,
        )
    except requests.exceptions.SSLError:
        raise
    except requests.exceptions.ConnectionError as e:
        raise errors.MasterNotFoundException(str(e))
    except requests.exceptions.RequestException as e:
        raise errors.BadRequestException(str(e))

    if r.status_code == 403:
        username = authentication.Authentication.instance().get_session_user()
        raise errors.UnauthenticatedException(username=username)
    elif r.status_code == 404:
        raise errors.NotFoundException(r)
    elif r.status_code >= 300:
        raise errors.APIException(r)

    return r