code: def do_request(
      method: str,
      host: str,
      path: str,
      params: Optional[Dict[str, Any]] = None,
      json: Any = None,
      data: Optional[str] = None,
      headers: Optional[Dict[str, str]] = None,
      authenticated: bool = True,
      auth: Optional[authentication.Authentication] = None,
      cert: Optional[certs.Cert] = None,
      stream: bool = False,
      timeout: Optional[Union[Tuple, float]] = None,
      max_retries: Optional[urllib3.util.retry.Retry] = None,
  ) -> requests.Response:
      if headers is None:
          h: Dict[str, str] = {}
      else:
          h = headers
  
      if cert is None:
          cert = certs.cli_cert
  
      # set the token and username based on this order:
      # - argument `auth`
      # - header `Authorization`
      # - existing cli_auth
      # - allocation_token
  
      username = ""
      if auth is not None:
          if authenticated:
              h["Authorization"] = "Bearer {}".format(
                  auth.get_session_token()
              )
          username = auth.get_session_user()
      elif h.get("Authorization") is not None:
          pass
      elif authentication.cli_auth is not None:
          if authenticated:
              h["Authorization"] = "Bearer {}".format(
                  authentication.cli_auth.get_session_token()
              )
          username = authentication.cli_auth.get_session_user()
      elif (
          authenticated
          and h.get("Grpc-Metadata-x-allocation-token") is None
      ):
          allocation_token = authentication.get_allocation_token()
          if allocation_token:
              h[
                  "Grpc-Metadata-x-allocation-token"
              ] = "Bearer {}".format(allocation_token)
  
      if params is None:
          params = {}
  
      # Allow the json json to come pre-encoded, if we need custom encoding.
      if json is not None and data is not None:
          raise ValueError(
              "json and data must not be provided together"
          )
  
      if json:
          data = det.util.json_encode(json)
  
      try:
          r = determined.common.requests.request(
              method,
              make_url(host, path),
              params=params,
              data=data,
              headers=h,
              verify=cert.bundle if cert else None,
              stream=stream,
              timeout=timeout,
              server_hostname=cert.name if cert else None,
              max_retries=max_retries,
          )
      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))
  
      def _get_error_str(r: requests.models.Response) -> str:
          try:
              json_resp = _json.loads(r.text)
              mes = json_resp.get("message")
              if mes is not None:
                  return str(mes)
              # Try getting GRPC error description if message does not exist.
              return str(json_resp.get("error").get("error"))
          except Exception:
              return ""
  
      if r.status_code == 403:
          raise errors.ForbiddenException(
              username=username, message=_get_error_str(r)
          )
      if r.status_code == 401:
          raise errors.UnauthenticatedException(username=username)
      elif r.status_code == 404:
          raise errors.NotFoundException(_get_error_str(r))
      elif r.status_code >= 300:
          raise errors.APIException(r)
  
      return r
  
imports: import json
  import os
  import types
  import webbrowser
  from typing import Any, Dict, Iterator, Optional, Tuple, Union
  from urllib import parse
  import requests
  import urllib3
  import determined
  import determined.common.requests
  from determined.common.api import authentication, certs, errors
label_desc: Use image node_id 6 for calling a global function with example usage: do_request(method, host, path, params, json, data, headers, authenticated, auth, cert, stream, timeout, max_retries) and returns: r, str, str, str