"""Prepared request."""
import json


class PreparedRequest(str):
    """
    Encapsulate and validate a request prior to sending.

    This object can be passed around subsequent methods. Subclasses of Client
    can add to it, or set the log format/extra info to include.

    Converts some non-string requests to string.
    """

    def __new__(cls, request):
        # Convert a list of strings, to one string
        if isinstance(request, list) and all(
            isinstance(i, str) for i in request
        ):
            request = "[{}]".format(", ".join(request))
        # Convert a json-serializable object (dict or list) to a string
        if not isinstance(request, str):
            request = json.dumps(request)
        # Should end up with a string
        assert isinstance(request, str)
        return str.__new__(cls, request)

    def __init__(self, request):
        super().__init__()
        #: Extra details used in log entry, can be set by clients in
        #: prepare_request
        self.log_extra = None
        self.log_format = None
