Skip to content

Commit d5c9fab

Browse files
authored
Merge pull request #90 from serv-c/feature/blob
fix(http): use cleaner find type
2 parents d5f6b8b + 77ddba6 commit d5c9fab

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

servc/svc/com/http/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class HTTPInterface(Middleware):
4545

4646
_consumer: Process
4747

48-
_components: List[type[Middleware]]
48+
_components: List[Middleware]
4949

5050
_info: ServiceInformation
5151

@@ -57,7 +57,7 @@ def __init__(
5757
consumerthread: Process,
5858
resolvers: RESOLVER_MAPPING,
5959
eventResolvers: RESOLVER_MAPPING,
60-
components: List[type[Middleware]],
60+
components: List[Middleware],
6161
):
6262
super().__init__(config)
6363
self._port = int(config.get("port"))

servc/svc/com/http/blob.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from servc.svc.config import Config
1717
from servc.svc.io.output import ResponseArtifact, StatusCode
1818
from servc.svc.io.response import getErrorArtifact
19+
from servc.util import findType
1920

2021

2122
def returnError(message: str, error: StatusCode = StatusCode.METHOD_NOT_FOUND):
@@ -35,17 +36,13 @@ def __init__(
3536
consumerthread: Process,
3637
resolvers: RESOLVER_MAPPING,
3738
eventResolvers: RESOLVER_MAPPING,
38-
components: List[type[Middleware]],
39+
components: List[Middleware],
3940
):
4041
super().__init__(
4142
config, bus, cache, consumerthread, resolvers, eventResolvers, components
4243
)
4344

44-
blobs = [x for x in components if x.name == "blob"]
45-
if len(blobs) == 0:
46-
raise Exception("Blob storage component not found in components list")
47-
self._blobStorage = blobs[0] # type: ignore[assignment]
48-
45+
self._blobStorage = findType(components, BlobStorage)
4946
self._uploadcontainer = config.get("uploadcontainer") or "uploads"
5047

5148
def get_upload_file_path(self, extra_params: Dict, fname: str) -> Tuple[str, str]:

servc/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List, TypeVar
2+
3+
from servc.svc import Middleware
4+
5+
T = TypeVar("T", bound=Middleware)
6+
7+
8+
def findType(c: List[Middleware], type: type[T]) -> T:
9+
cf = [x for x in c if isinstance(x, type)]
10+
if len(cf) == 0:
11+
raise ValueError(f"Middleware {type} not found")
12+
return cf[0]

0 commit comments

Comments
 (0)