-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82080ef
commit fa932b5
Showing
6 changed files
with
56 additions
and
27 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,37 @@ | ||
from threading import Thread | ||
from onany.manager import OnAny | ||
from typing import Any, Dict, Union | ||
from onany.manager import EventManager | ||
from onany.web import webhook_callback | ||
|
||
|
||
def clear_listeners(): | ||
EventManager.clear() | ||
|
||
|
||
def dispatch(event: str, *args, **kwargs) -> None: | ||
return OnAny.emit(event, *args, **kwargs) | ||
return EventManager.emit(event, *args, **kwargs) | ||
|
||
|
||
def disthread(event: str, *args, **kwargs) -> Thread: | ||
thread: Thread = Thread( | ||
target=OnAny.emit, | ||
target=EventManager.emit, | ||
args=(event, *args,), | ||
kwargs=kwargs) | ||
thread.start() | ||
|
||
return thread | ||
|
||
|
||
def listener(event: str, callback: callable = None) -> callable: | ||
if callback: | ||
return OnAny.listen(event, callback) | ||
def listener(event: str, callback: Union[callable, Dict[str, Any]] = None) -> callable: | ||
if callable(callback): | ||
return EventManager.listen(event, callback) | ||
|
||
if callback and isinstance(callback, dict): | ||
return EventManager.listen( | ||
event, | ||
webhook_callback(callback)) | ||
|
||
def wrapper(callback: callable) -> None: | ||
return OnAny.listen(event, callback) | ||
return EventManager.listen(event, callback) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Any, Dict | ||
import requests | ||
|
||
|
||
def webhook_callback(rules: Dict[str, Any]) -> callable: | ||
def wrapper(*args, **kwargs): | ||
response = requests.post( | ||
rules.get("route"), | ||
json=kwargs.get("data"), | ||
headers=kwargs.get("headers")) | ||
|
||
if rules.get("callback"): | ||
rules.get("callback")(response) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests==2.23.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters