clan-app: change ApiBridge ABC class to Protocol
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
from contextlib import ExitStack
|
from contextlib import ExitStack
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any, Protocol
|
||||||
|
|
||||||
from clan_lib.api import ApiError, ApiResponse, ErrorDataClass
|
from clan_lib.api import ApiError, ApiResponse, ErrorDataClass
|
||||||
from clan_lib.api.tasks import WebThread
|
from clan_lib.api.tasks import WebThread
|
||||||
@@ -30,16 +29,13 @@ class BackendResponse:
|
|||||||
_op_key: str
|
_op_key: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class ApiBridge(Protocol):
|
||||||
class ApiBridge(ABC):
|
|
||||||
"""Generic interface for API bridges that can handle method calls from different sources."""
|
"""Generic interface for API bridges that can handle method calls from different sources."""
|
||||||
|
|
||||||
middleware_chain: tuple["Middleware", ...]
|
middleware_chain: tuple["Middleware", ...]
|
||||||
threads: dict[str, WebThread] = field(default_factory=dict)
|
threads: dict[str, WebThread]
|
||||||
|
|
||||||
@abstractmethod
|
def send_api_response(self, response: BackendResponse) -> None: ...
|
||||||
def send_api_response(self, response: BackendResponse) -> None:
|
|
||||||
"""Send response back to the client."""
|
|
||||||
|
|
||||||
def process_request(self, request: BackendRequest) -> None:
|
def process_request(self, request: BackendRequest) -> None:
|
||||||
"""Process an API request through the middleware chain."""
|
"""Process an API request through the middleware chain."""
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass, field
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from clan_lib.api import dataclass_to_dict
|
from clan_lib.api import dataclass_to_dict
|
||||||
@@ -9,6 +9,8 @@ from clan_lib.api.tasks import WebThread
|
|||||||
from clan_app.api.api_bridge import ApiBridge, BackendRequest, BackendResponse
|
from clan_app.api.api_bridge import ApiBridge, BackendRequest, BackendResponse
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from clan_app.api.middleware import Middleware
|
||||||
|
|
||||||
from .webview import Webview
|
from .webview import Webview
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@@ -19,7 +21,8 @@ class WebviewBridge(ApiBridge):
|
|||||||
"""Webview-specific implementation of the API bridge."""
|
"""Webview-specific implementation of the API bridge."""
|
||||||
|
|
||||||
webview: "Webview"
|
webview: "Webview"
|
||||||
threads: dict[str, WebThread] # Inherited from ApiBridge
|
middleware_chain: tuple["Middleware", ...]
|
||||||
|
threads: dict[str, WebThread] = field(default_factory=dict)
|
||||||
|
|
||||||
def send_api_response(self, response: BackendResponse) -> None:
|
def send_api_response(self, response: BackendResponse) -> None:
|
||||||
"""Send response back to the webview client."""
|
"""Send response back to the webview client."""
|
||||||
|
|||||||
Reference in New Issue
Block a user