# Path: sirbot/slack/errors.py
# class SlackConnectionError(SlackClientError):
#     """Connection to slack server error"""
# 
# class SlackServerError(SlackClientError):
#     """Internal slack server error"""
# 
# class SlackRedirectionError(SlackClientError):
#     """Redirection status code"""
# 
# class SlackAPIError(SlackClientError):
#     """Wrong use of slack API"""
# 
#     def __init__(self, response):
#         self.ok = response.get('ok')
#         self.error = response.get('error')
#         self.response = response
# 
# class SlackClientError(SlackError):
#     """Error with the slack API"""


import asyncio
import json
import logging
import aiohttp
from typing import Any, AnyStr, Dict, Optional
from sirbot.utils import ensure_future
from .errors import (
    SlackConnectionError,
    SlackServerError,
    SlackRedirectionError,
    SlackAPIError,
    SlackClientError
)


class RTMClient(APICaller):
    """
    Client for the slack RTM API (websocket based API).

    :param token: Slack API Token
    :param loop: Event loop to work in, optional.
    """

    def __init__(self, bot_token, callback,
                 *, loop: Optional[asyncio.BaseEventLoop] = None,
                 session: aiohttp.ClientSession = None):

        super().__init__(bot_token, loop=loop, session=session)
        self._ws = None
        self._closed = asyncio.Event(loop=self._loop)
        self._callback = callback

    @property
    def is_closed(self) -> bool:
        """bool: Indicates if the websocket connection is closed."""
        return self._closed.is_set()

    async def _negotiate_rtm_url(self):
        """
        Get the RTM url
        """
        data = await self._do_post(APIPath.RTM_CONNECT)
        if data.get('ok') is False:

# Path: sirbot/slack/errors.py
# class SlackConnectionError(SlackClientError):
#     """Connection to slack server error"""
# 
# class SlackServerError(SlackClientError):
#     """Internal slack server error"""
# 
# class SlackRedirectionError(SlackClientError):
#     """Redirection status code"""
# 
# class SlackAPIError(SlackClientError):
#     """Wrong use of slack API"""
# 
#     def __init__(self, response):
#         self.ok = response.get('ok')
#         self.error = response.get('error')
#         self.response = response
# 
# class SlackClientError(SlackError):
#     """Error with the slack API"""


# Path: sirbot/slack/api.py
import asyncio
import json
import logging
import aiohttp
from typing import Any, AnyStr, Dict, Optional
from sirbot.utils import ensure_future
from .errors import (
    SlackConnectionError,
    SlackServerError,
    SlackRedirectionError,
    SlackAPIError,
    SlackClientError
)



class RTMClient(APICaller):
    """
    Client for the slack RTM API (websocket based API).

    :param token: Slack API Token
    :param loop: Event loop to work in, optional.
    """

    def __init__(self, bot_token, callback,
                 *, loop: Optional[asyncio.BaseEventLoop] = None,
                 session: aiohttp.ClientSession = None):

        super().__init__(bot_token, loop=loop, session=session)
        self._ws = None
        self._closed = asyncio.Event(loop=self._loop)
        self._callback = callback

    @property
    def is_closed(self) -> bool:
        """bool: Indicates if the websocket connection is closed."""
        return self._closed.is_set()

    async def _negotiate_rtm_url(self):
        """
        Get the RTM url
        """
        data = await self._do_post(APIPath.RTM_CONNECT)
        if data.get('ok') is False:

            raise SlackConnectionError(