Skip to content

Commit c3327e1

Browse files
committed
add 'webhook' integration model
1 parent 2c6d777 commit c3327e1

5 files changed

Lines changed: 45 additions & 13 deletions

File tree

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ taskbadger.init(
3131
#### API Example
3232

3333
```python
34-
from taskbadger import Task, Action, EmailIntegration
34+
from taskbadger import Task, Action, EmailIntegration, WebhookIntegration
3535

3636
# create a new task with custom data and an action definition
3737
task = Task.create(
@@ -40,10 +40,8 @@ task = Task.create(
4040
"custom": "data"
4141
},
4242
actions=[
43-
Action(
44-
"*/10%,success,error",
45-
integration=EmailIntegration(to="me@example.com")
46-
)
43+
Action("*/10%,success,error", integration=EmailIntegration(to="me@example.com")),
44+
Action("cancelled", integration=WebhookIntegration(id="webhook:demo")),
4745
]
4846
)
4947

taskbadger/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .integrations import Action, EmailIntegration
1+
from .integrations import Action, EmailIntegration, WebhookIntegration
22
from .internal.models import StatusEnum
33
from .safe_sdk import create_task_safe, update_task_safe
44
from .sdk import Task, create_task, get_task, init, update_task

taskbadger/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ def __init__(self, **kwargs):
44

55
def __str__(self):
66
return f"Missing configuration parameters: {', '.join(self.missing)}"
7+
8+
9+
class TaskbadgerException(Exception):
10+
pass

taskbadger/integrations.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from enum import Enum
33
from typing import Any, Dict
44

5+
from taskbadger.exceptions import TaskbadgerException
56
from taskbadger.internal.models import ActionRequest, ActionRequestConfig
67

78

@@ -17,7 +18,12 @@ def from_config(integration: Integrations, config: str):
1718

1819

1920
class Integration:
20-
name: str
21+
type: str
22+
id: str
23+
24+
def __post_init__(self):
25+
if not self.id.startswith(self.type):
26+
raise TaskbadgerException(f"Expected integration ID '{self.id}' to start with '{self.type}'")
2127

2228
def request_config(self):
2329
raise NotImplementedError
@@ -29,13 +35,23 @@ class Action:
2935
integration: Integration
3036

3137
def to_dict(self) -> Dict[str, Any]:
32-
return ActionRequest(self.trigger, self.integration.name, self.integration.request_config()).to_dict()
38+
return ActionRequest(self.trigger, self.integration.id, self.integration.request_config()).to_dict()
3339

3440

3541
@dataclasses.dataclass
3642
class EmailIntegration(Integration):
37-
name = "email"
43+
type = "email"
44+
id = "email"
3845
to: str # custom type
3946

4047
def request_config(self) -> ActionRequestConfig:
4148
return ActionRequestConfig.from_dict({"to": self.to})
49+
50+
51+
@dataclasses.dataclass
52+
class WebhookIntegration(Integration):
53+
type = "webhook"
54+
id: str
55+
56+
def request_config(self) -> ActionRequestConfig:
57+
return ActionRequestConfig.from_dict({})

tests/test_sdk.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import pytest
55

6-
from taskbadger import Action, EmailIntegration, StatusEnum
6+
from taskbadger import Action, EmailIntegration, StatusEnum, WebhookIntegration
7+
from taskbadger.exceptions import TaskbadgerException
78
from taskbadger.internal.models import PatchedTaskRequest, PatchedTaskRequestData, TaskRequest, TaskRequestData
89
from taskbadger.internal.types import UNSET, Response
910
from taskbadger.sdk import Badger, Task, init
@@ -132,17 +133,30 @@ def test_add_actions(settings, patched_update):
132133

133134
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, api_task)
134135

135-
action = Action("*/10%,success,error", integration=EmailIntegration(to="me@example.com"))
136-
task.add_actions([action])
136+
task.add_actions(
137+
[
138+
Action("*/10%,success,error", integration=EmailIntegration(to="me@example.com")),
139+
Action("cancelled", integration=WebhookIntegration(id="webhook:123")),
140+
]
141+
)
137142

138143
# expected request
139144
_verify_update(
140145
settings,
141146
patched_update,
142-
actions=[{"trigger": "*/10%,success,error", "integration": "email", "config": {"to": "me@example.com"}}],
147+
actions=[
148+
{"trigger": "*/10%,success,error", "integration": "email", "config": {"to": "me@example.com"}},
149+
{"trigger": "cancelled", "integration": "webhook:123", "config": {}},
150+
],
143151
)
144152

145153

154+
def test_action_validation():
155+
WebhookIntegration(id="webhook:123")
156+
with pytest.raises(TaskbadgerException):
157+
WebhookIntegration(id="email:123")
158+
159+
146160
def _verify_update(settings, patched_update, **kwargs):
147161
actions = kwargs.pop("actions", None)
148162
request_params = {

0 commit comments

Comments
 (0)