From 5f71167a945023b6fda8046449e86e537dbe99ca Mon Sep 17 00:00:00 2001 From: Yusuf Ali Date: Sat, 24 May 2025 08:22:50 -0400 Subject: [PATCH] fix(http): use cleaner find type --- servc/svc/com/http/__init__.py | 4 ++-- servc/svc/com/http/blob.py | 9 +++------ servc/util.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 servc/util.py diff --git a/servc/svc/com/http/__init__.py b/servc/svc/com/http/__init__.py index 7cb7b05..c5f3a06 100644 --- a/servc/svc/com/http/__init__.py +++ b/servc/svc/com/http/__init__.py @@ -45,7 +45,7 @@ class HTTPInterface(Middleware): _consumer: Process - _components: List[type[Middleware]] + _components: List[Middleware] _info: ServiceInformation @@ -57,7 +57,7 @@ def __init__( consumerthread: Process, resolvers: RESOLVER_MAPPING, eventResolvers: RESOLVER_MAPPING, - components: List[type[Middleware]], + components: List[Middleware], ): super().__init__(config) self._port = int(config.get("port")) diff --git a/servc/svc/com/http/blob.py b/servc/svc/com/http/blob.py index ce0348c..21a2d21 100644 --- a/servc/svc/com/http/blob.py +++ b/servc/svc/com/http/blob.py @@ -16,6 +16,7 @@ from servc.svc.config import Config from servc.svc.io.output import ResponseArtifact, StatusCode from servc.svc.io.response import getErrorArtifact +from servc.util import findType def returnError(message: str, error: StatusCode = StatusCode.METHOD_NOT_FOUND): @@ -35,17 +36,13 @@ def __init__( consumerthread: Process, resolvers: RESOLVER_MAPPING, eventResolvers: RESOLVER_MAPPING, - components: List[type[Middleware]], + components: List[Middleware], ): super().__init__( config, bus, cache, consumerthread, resolvers, eventResolvers, components ) - blobs = [x for x in components if x.name == "blob"] - if len(blobs) == 0: - raise ValueError("Blob storage component not found in components list") - self._blobStorage = blobs[0] # type: ignore[assignment] - + self._blobStorage = findType(components, BlobStorage) self._uploadcontainer = config.get("uploadcontainer") or "uploads" def get_upload_file_path(self, extra_params: Dict, fname: str) -> Tuple[str, str]: diff --git a/servc/util.py b/servc/util.py new file mode 100644 index 0000000..e926a14 --- /dev/null +++ b/servc/util.py @@ -0,0 +1,12 @@ +from typing import List, TypeVar + +from servc.svc import Middleware + +T = TypeVar("T", bound=Middleware) + + +def findType(c: List[Middleware], type: type[T]) -> T: + cf = [x for x in c if isinstance(x, type)] + if len(cf) == 0: + raise ValueError(f"Middleware {type} not found") + return cf[0]