def _dict()

in bonsaicli2/bonsai_cli/api.py [0:0]


def _dict(response: Any, request_id: str):
    """
    Translates the response from the server into a dictionary. The implication
    is that the server should send back a JSON response for every REST API
    request, and if for some reason, that response is missing, it should be
    treated as an empty JSON message rather than an error. This method will
    change empty responses into empty dictionaries. Responses that are not
    formatted as JSON will raise an exception.
    :param response: The response from the server.
    :return: Dictionary form the JSON text in the response.
    """

    response_dict: Any = {}
    if response and response.text and response.text.strip():
        try:
            if isinstance(response.json(), dict):
                response_dict = response.json()
            else:
                response_dict = {"value": response.json()}
        except ValueError:
            pass

    try:
        status = "Succeeded" if response.ok else "Failed"
        response_dict["status"] = status
        response_dict["statusCode"] = response.status_code
        response_dict["statusMessage"] = ""
        response_dict["elapsed"] = response.elapsed
        response_dict["timeTaken"] = response.headers["x-ms-response-time"]
    except KeyError:
        pass

    return response_dict