-
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.
Add
expose
API for handling dynamic plugin endpoints
- Loading branch information
Showing
3 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
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,50 @@ | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
from fastapi import Request | ||
|
||
from utils import stack | ||
|
||
_SUBSCRIBERS: Dict[str, Dict[str, | ||
Dict[str, Callable[[str, Request], Any]]]] = { | ||
'GET': {}, | ||
'PUT': {}, | ||
'POST': {}, | ||
'DELETE': {} | ||
} | ||
|
||
|
||
def subscribe_get(endpoint: str, callback: Callable[[str, Request], Any]) -> None: | ||
_subscribe(endpoint, _SUBSCRIBERS['GET'], callback) | ||
|
||
|
||
def subscribe_put(endpoint: str, callback: Callable[[str, Request], Any]) -> None: | ||
_subscribe(endpoint, _SUBSCRIBERS['PUT'], callback) | ||
|
||
|
||
def subscribe_post(endpoint: str, callback: Callable[[str, Request], Any]) -> None: | ||
_subscribe(endpoint, _SUBSCRIBERS['POST'], callback) | ||
|
||
|
||
def subscribe_delete(endpoint: str, callback: Callable[[str, Request], Any]) -> None: | ||
_subscribe(endpoint, _SUBSCRIBERS['DELETE'], callback) | ||
|
||
|
||
def _subscribe(endpoint: str, structure: Dict, callback: Callable[[str, Request], Any]) -> None: | ||
module_name = stack.get_caller(depth=3)[0] | ||
if not module_name.startswith('plugins.plugins.'): | ||
return | ||
|
||
plugin_name = module_name.split('.')[2] | ||
if plugin_name not in structure: | ||
structure[plugin_name] = {} | ||
|
||
structure[plugin_name][endpoint] = callback | ||
|
||
|
||
def fetch_callback(plugin: str, endpoint: str, method: str) -> Optional[Callable[[str, Request], Any]]: | ||
endpoints = _SUBSCRIBERS[method.upper()].get(plugin) | ||
if endpoints is None: | ||
return None | ||
if endpoint[-1] == '/': | ||
endpoint = endpoint[:-1] | ||
return endpoints.get(endpoint) |
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,13 @@ | ||
# pylint: disable=protected-access | ||
|
||
from typing import Tuple | ||
import sys | ||
|
||
|
||
# Note: The sys wiki states that "It (sys._getframe()) | ||
# is not guranteed to exist in all | ||
# implementations of Python" | ||
def get_caller(depth: int = 1) -> Tuple[str, str]: | ||
frame = sys._getframe(depth) | ||
return (frame.f_globals['__name__'], | ||
frame.f_code.co_name) |