Skip to content

Commit 7fe7bc2

Browse files
Create Slack Client from Integration (#5)
* Fix up linting errors * Add slack_sdk * Fix typo in test script * Add workspace integration detail route * Mount local to build directory * Test get_client_for_workspace_integration * Add error for missing credentials * Add slack integration * Fix linting * Credentials come as a str
1 parent 4ec37d6 commit 7fe7bc2

File tree

14 files changed

+202
-26
lines changed

14 files changed

+202
-26
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7+
slack-sdk = "*"
78

89
[dev-packages]
910
pytest = "*"

Pipfile.lock

Lines changed: 121 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ services:
33
app:
44
build:
55
context: .
6-
working_dir: /opt/project
76
volumes:
8-
- .:/opt/project
7+
- .:/usr/local/src/project
98
environment:
109
- TWINE_USERNAME=__token__
1110
- TWINE_NON_INTERACTIVE=1

files/usr/local/bin/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
set -e
44
set -x
55

6-
pipe install -e .
6+
pip install -e .
77
pytest

src/wayscript/context.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from functools import lru_cache
32

43
from . import utils

src/wayscript/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
WayScript Errors
3+
"""
4+
5+
class MissingCredentialsError(Exception):
6+
"""Error thrown when a workspace integration does not have requisite credentials"""
7+
pass

src/wayscript/integrations/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
3+
from slack_sdk import WebClient
4+
5+
from wayscript import utils
6+
from wayscript.errors import MissingCredentialsError
7+
8+
9+
def get_client_for_workspace_integration(_id: str) -> WebClient:
10+
"""Get a slack client with credentials from a workspace integration"""
11+
wayscript_client = utils.WayScriptClient()
12+
response = wayscript_client.get_workspace_integration_detail(_id)
13+
response.raise_for_status()
14+
workspace_integration_data = response.json()
15+
16+
access_token = None
17+
credentials_str = workspace_integration_data["credentials"]
18+
19+
try:
20+
credentials = json.loads(credentials_str)
21+
access_token = credentials.get("access_token")
22+
except json.decoder.JSONDecodeError:
23+
access_token = None
24+
25+
if not access_token:
26+
raise MissingCredentialsError(f"No credentials found for {_id}")
27+
else:
28+
return WebClient(access_token)

src/wayscript/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
PROCESSES_ROUTE = "processes"
88
LAIRS_ROUTE = "lairs"
99
WORKSPACES_ROUTE = "workspaces"
10+
WORKSPACE_INTEGRATIONS_ROUTE = "workspace-integrations"
1011

1112

1213
ROUTES = {
1314
"lairs": {"detail": f"{LAIRS_ROUTE}/$id"},
1415
"processes": { "detail_expanded": f"{PROCESSES_ROUTE}/$id/detail"},
1516
"workspaces": {"detail": f"{WORKSPACES_ROUTE}/$id"},
17+
"workspace-integrations": {"detail": f"{WORKSPACE_INTEGRATIONS_ROUTE}/$id"}
1618
}
1719

1820
# origin is scheme + domain + port. explanation: https://stackoverflow.com/a/37366696/4293004

src/wayscript/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def get_process_detail_expanded(self, _id: str):
3939
response = self.session.get(url)
4040
return response
4141

42+
def get_workspace_integration_detail(self, _id: str):
43+
"""Request a workspace-integrations detail"""
44+
url = self._get_url(subpath="workspace-integrations", route="detail", template_args={"id": _id})
45+
response = self.session.get(url)
46+
return response
47+
4248
def get_lair_detail(self, _id: str):
4349
"""Request lair detail"""
4450
url = self._get_url(subpath="lairs", route="detail", template_args={"id": _id})

0 commit comments

Comments
 (0)