示例#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,
    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)

    if r.status_code >= 300:
        raise errors.APIException(r)

    return r
示例#2
0
 def __iter__(self) -> Iterator[Any]:
     for event in self.socket.connect(ping_rate=0):
         if isinstance(event, lomond.events.Connected):
             # Ignore the initial connection event.
             pass
         elif isinstance(event, lomond.events.Closing) or isinstance(
                 event, lomond.events.Disconnected):
             # The socket was successfully closed so we just return.
             return
         elif (isinstance(event, lomond.events.ConnectFail)
               or isinstance(event, lomond.events.Rejected)
               or isinstance(event, lomond.events.ProtocolError)):
             # Any unexpected failures raise the standard API exception.
             raise errors.BadRequestException(
                 message="WebSocket failure: {}".format(event))
         elif isinstance(event, lomond.events.Text):
             # All web socket connections are expected to be in a JSON
             # format.
             yield simplejson.loads(event.text)