Skip to content

Commit

Permalink
Implement conversations.list API
Browse files Browse the repository at this point in the history
Read the channels list from a file in the slack server
Add a handler that returns the list from the slack server
Register the handler
Add a setting to set the location of the channels file
Avoid circular import

Signed-off-by: Ygal Blum <[email protected]>
  • Loading branch information
ygalblum committed Jul 10, 2024
1 parent dd8616f commit 27e70b0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
22 changes: 22 additions & 0 deletions slack_server_mock/servers/http/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from slack_server_mock.actor.actor import Actor
from slack_server_mock.injector.di import global_injector
from slack_server_mock.slack_server.slack_server import SlackServer
from slack_server_mock.servers.base_http_handlers import load_json_from_body
from slack_server_mock.settings.settings import Settings

Expand Down Expand Up @@ -121,3 +122,24 @@ def post(self):
}
}
)


class ConversationsListHandler(BaseSlackHandler): # pylint: disable=W0223
""" Handler for conversations.list endpoint """
def _handle(self):
self.write(
{
"ok": True,
"channels": global_injector.get(SlackServer).channels
}
)

def get(self):
""" Handle GET request """
self._handle()

def post(self):
""" Handle POST request.
While the documentation states that this is a GET command, the SDK calls PUT
"""
self._handle()
8 changes: 5 additions & 3 deletions slack_server_mock/servers/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

from slack_server_mock.settings.settings import Settings
from slack_server_mock.servers.base_http_server import BaseHTTPServer
from slack_server_mock.servers.http import handler


@singleton
class SlackHTTPServer(BaseHTTPServer):
class SlackHTTPServer(BaseHTTPServer): # pylint: disable=C0415
""" Mock Implementation of the Slack HTTP server """
@inject
def __init__(self, settings: Settings) -> None:
from slack_server_mock.servers.http import handler

app = Application(
[
(r"/auth.test", handler.AuthTestHandler),
(r"/apps.connections.open", handler.AppsConnectionsOpenHandler),
(r"/api.test", handler.ApiTestHandler),
(r"/chat.postMessage", handler.ChatPostMessageHandler)
(r"/chat.postMessage", handler.ChatPostMessageHandler),
(r"/conversations.list", handler.ConversationsListHandler),
]
)
super().__init__(app, settings.slack_server.http_port)
5 changes: 5 additions & 0 deletions slack_server_mock/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class SlackServer(BaseModel):
description="HTTP Server Listening port"
)

channels_path: str = Field(
None,
description="Path to a file containing the list of conversations"
)


class Actor(BaseModel):
""" Settings for the Actor """
Expand Down
26 changes: 25 additions & 1 deletion slack_server_mock/slack_server/slack_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
""" Slack Mock Server """
import json

from injector import inject, singleton

from slack_server_mock.injector.di import global_injector
from slack_server_mock.settings.settings import Settings
from slack_server_mock.servers.http.server import SlackHTTPServer
from slack_server_mock.servers.websocket.server import SlackWebSocketServer

Expand All @@ -10,9 +13,20 @@
class SlackServer():
""" Class to hold and manage all Slack server related objects and operations """
@inject
def __init__(self, http_server: SlackHTTPServer, websocket_server: SlackWebSocketServer) -> None:
def __init__(
self,
settings: Settings,
http_server: SlackHTTPServer,
websocket_server: SlackWebSocketServer
) -> None:
self._http_server = http_server
self._websocket_server = websocket_server
self._channels = self._load_channels(settings.slack_server.channels_path)

@property
def channels(self):
""" Return the channels list """
return self._channels

def start(self):
""" Start the Slack server """
Expand All @@ -24,6 +38,16 @@ def stop(self):
self._http_server.stop()
self._websocket_server.stop()

@staticmethod
def _load_channels(path):
channels = []
if path:
with open(path, "r", encoding="utf-8") as f:
channels = json.load(f)
if not isinstance(channels, list):
raise ValueError("The content of the channels file is not a JSON array")
return channels


def start_slack_server():
""" Start the Slack server """
Expand Down

0 comments on commit 27e70b0

Please sign in to comment.