# Path: quantrocket/exceptions.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# class CannotConnectToHouston(Exception):
#     pass
# 
# Path: quantrocket/cli/utils/output.py
# def json_to_cli(func, *args, **kwargs):
#     """
#     Converts a JSON response to a more appropriate CLI response.
# 
#     If JSON is preferred, the response will be returned as-is. Otherwise:
# 
#     - If the JSON is a list of scalars, the output will be simplified to a
#     string of newline-separated values suitable for the command line (unless
#     simplify_lists is False).
# 
#     - If the JSON is empty, nothing will be returned.
# 
#     - YAML will be returned.
#     """
#     exit_code = 0
#     simplify_list = kwargs.pop("simplify_list", True)
#     try:
#         json_response = func(*args, **kwargs)
#     except requests.exceptions.HTTPError as e:
#         # use json response from service, if available
#         json_response = getattr(e, "json_response", {}) or {"status": "error", "msg": repr(e)}
#         exit_code = 1
#     if not json_response:
#         return None, exit_code
#     if os.environ.get("QUANTROCKET_CLI_OUTPUT_FORMAT", "").lower() == "json":
#         return json.dumps(json_response), exit_code
#     if simplify_list and isinstance(json_response, list) and not any([
#         isinstance(item, (dict, list, tuple, set)) for item in json_response]):
#         return "\n".join([str(item) for item in json_response]), exit_code
#     return yaml.safe_dump(json_response, default_flow_style=False).strip(), exit_code


import os
import six
import requests
import re
import uuid
from .exceptions import ImproperlyConfigured, CannotConnectToHouston
from quantrocket.cli.utils.output import json_to_cli
                kwargs["timeout"] = self.DEFAULT_TIMEOUT

        # Move params to data if too long
        for param_name, param_vals in kwargs.get("params", {}).copy().items():
            if isinstance(param_vals, list) and len(param_vals) > 50:
                data = kwargs.get("data", {}) or {}
                data[param_name] = param_vals
                kwargs["params"].pop(param_name)
                kwargs["data"] = data

        try:
            return super(Houston, self).request(
                method, url, *args, headers=self.headers, **kwargs)
        except requests.ConnectionError as error:
            if "Failed to establish a new connection" not in str(error):
                raise

            parsed = six.moves.urllib.parse.urlparse(error.request.url)

            if parsed.hostname == "houston" and parsed.port in (None, 80):
                # don't do anything special within containers
                raise

            if parsed.port == 443:
                raise CannotConnectToHouston(CANNOT_CONNECT_TO_HOUSTON_ERROR_CLOUD.format(
                    error=error,
                    scheme=parsed.scheme,
                    netloc=parsed.netloc
                ))


# Path: quantrocket/exceptions.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# class CannotConnectToHouston(Exception):
#     pass
# 
# Path: quantrocket/cli/utils/output.py
# def json_to_cli(func, *args, **kwargs):
#     """
#     Converts a JSON response to a more appropriate CLI response.
# 
#     If JSON is preferred, the response will be returned as-is. Otherwise:
# 
#     - If the JSON is a list of scalars, the output will be simplified to a
#     string of newline-separated values suitable for the command line (unless
#     simplify_lists is False).
# 
#     - If the JSON is empty, nothing will be returned.
# 
#     - YAML will be returned.
#     """
#     exit_code = 0
#     simplify_list = kwargs.pop("simplify_list", True)
#     try:
#         json_response = func(*args, **kwargs)
#     except requests.exceptions.HTTPError as e:
#         # use json response from service, if available
#         json_response = getattr(e, "json_response", {}) or {"status": "error", "msg": repr(e)}
#         exit_code = 1
#     if not json_response:
#         return None, exit_code
#     if os.environ.get("QUANTROCKET_CLI_OUTPUT_FORMAT", "").lower() == "json":
#         return json.dumps(json_response), exit_code
#     if simplify_list and isinstance(json_response, list) and not any([
#         isinstance(item, (dict, list, tuple, set)) for item in json_response]):
#         return "\n".join([str(item) for item in json_response]), exit_code
#     return yaml.safe_dump(json_response, default_flow_style=False).strip(), exit_code


# Path: quantrocket/houston.py
import os
import six
import requests
import re
import uuid
from .exceptions import ImproperlyConfigured, CannotConnectToHouston
from quantrocket.cli.utils.output import json_to_cli

                kwargs["timeout"] = self.DEFAULT_TIMEOUT

        # Move params to data if too long
        for param_name, param_vals in kwargs.get("params", {}).copy().items():
            if isinstance(param_vals, list) and len(param_vals) > 50:
                data = kwargs.get("data", {}) or {}
                data[param_name] = param_vals
                kwargs["params"].pop(param_name)
                kwargs["data"] = data

        try:
            return super(Houston, self).request(
                method, url, *args, headers=self.headers, **kwargs)
        except requests.ConnectionError as error:
            if "Failed to establish a new connection" not in str(error):
                raise

            parsed = six.moves.urllib.parse.urlparse(error.request.url)

            if parsed.hostname == "houston" and parsed.port in (None, 80):
                # don't do anything special within containers
                raise

            if parsed.port == 443:
                raise CannotConnectToHouston(CANNOT_CONNECT_TO_HOUSTON_ERROR_CLOUD.format(
                    error=error,
                    scheme=parsed.scheme,
                    netloc=parsed.netloc
                ))


            raise CannotConnectToHouston(CANNOT_CONNECT_TO_HOUSTON_ERROR_LOCAL.format(