# Path: craft_ai/errors.py
# class CraftAiCredentialsError(CraftAiError):
#     """A credentials error occured."""
# 
# class CraftAiBadRequestError(CraftAiError):
#     """An unvalid request was send to craft ai's API."""
# 
# class CraftAiNotFoundError(CraftAiError):
#     """A Not Found Error (404) occured on craft ai's side."""
# 
# class CraftAiUnknownError(CraftAiError):
#     """An unknown error occured in the craft ai client."""
# 
# class CraftAiInternalError(CraftAiError):
#     """An Internal Server Error (500) ocurred on craft ai's side."""


import websockets
import asyncio
import json
from .errors import (
    CraftAiCredentialsError,
    CraftAiBadRequestError,
    CraftAiNotFoundError,
    CraftAiUnknownError,
    CraftAiInternalError,
)
    return err


def initialize_websocket(url, headers):
    web_socket_event_loop = asyncio.get_event_loop()
    try:
        [protocol, uri] = url.split("://")
    except ValueError:
        raise CraftAiBadRequestError("Invalid url provided")
    ws_protocol = ("wss://", "ws://")[protocol == "http"]
    ws_url = ws_protocol + uri + "/api/ws"
    try:
        websocket = web_socket_event_loop.run_until_complete(
            websockets.connect(
                ws_url, subprotocols=["echo-protocol"], extra_headers=headers
            )
        )
    except websockets.exceptions.InvalidStatusCode as e:
        raise CraftAiCredentialsError(e)
    return websocket


def send_payload_websocket(websocket, payload):
    web_socket_event_loop = asyncio.get_event_loop()
    try:
        web_socket_event_loop.run_until_complete(websocket.send(payload))
    except websockets.exceptions.ConnectionClosedError as e:
        try:
            error_message = web_socket_event_loop.run_until_complete(websocket.recv())
        except websockets.exceptions.ConnectionClosedError as error:

# Path: craft_ai/errors.py
# class CraftAiCredentialsError(CraftAiError):
#     """A credentials error occured."""
# 
# class CraftAiBadRequestError(CraftAiError):
#     """An unvalid request was send to craft ai's API."""
# 
# class CraftAiNotFoundError(CraftAiError):
#     """A Not Found Error (404) occured on craft ai's side."""
# 
# class CraftAiUnknownError(CraftAiError):
#     """An unknown error occured in the craft ai client."""
# 
# class CraftAiInternalError(CraftAiError):
#     """An Internal Server Error (500) ocurred on craft ai's side."""


# Path: craft_ai/websocket_client.py
import websockets
import asyncio
import json
from .errors import (
    CraftAiCredentialsError,
    CraftAiBadRequestError,
    CraftAiNotFoundError,
    CraftAiUnknownError,
    CraftAiInternalError,
)

    return err


def initialize_websocket(url, headers):
    web_socket_event_loop = asyncio.get_event_loop()
    try:
        [protocol, uri] = url.split("://")
    except ValueError:
        raise CraftAiBadRequestError("Invalid url provided")
    ws_protocol = ("wss://", "ws://")[protocol == "http"]
    ws_url = ws_protocol + uri + "/api/ws"
    try:
        websocket = web_socket_event_loop.run_until_complete(
            websockets.connect(
                ws_url, subprotocols=["echo-protocol"], extra_headers=headers
            )
        )
    except websockets.exceptions.InvalidStatusCode as e:
        raise CraftAiCredentialsError(e)
    return websocket


def send_payload_websocket(websocket, payload):
    web_socket_event_loop = asyncio.get_event_loop()
    try:
        web_socket_event_loop.run_until_complete(websocket.send(payload))
    except websockets.exceptions.ConnectionClosedError as e:
        try:
            error_message = web_socket_event_loop.run_until_complete(websocket.recv())
        except websockets.exceptions.ConnectionClosedError as error:

            raise CraftAiUnknownError(error.reason)