api_wrapper: more descriptive parameter names
also only decode the op_key once
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import ctypes
|
import ctypes
|
||||||
|
import functools
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
import functools
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
@@ -61,16 +61,17 @@ class Webview:
|
|||||||
def api_wrapper(
|
def api_wrapper(
|
||||||
self,
|
self,
|
||||||
api: MethodRegistry,
|
api: MethodRegistry,
|
||||||
wrap_method: Callable[..., Any],
|
|
||||||
method_name: str,
|
method_name: str,
|
||||||
|
wrap_method: Callable[..., Any],
|
||||||
seq: bytes,
|
op_key_bytes: bytes,
|
||||||
req: bytes,
|
request_data: bytes,
|
||||||
arg: int,
|
arg: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
op_key = op_key_bytes.decode()
|
||||||
|
|
||||||
def thread_task(stop_event: threading.Event) -> None:
|
def thread_task(stop_event: threading.Event) -> None:
|
||||||
try:
|
try:
|
||||||
args = json.loads(req.decode())
|
args = json.loads(request_data.decode())
|
||||||
|
|
||||||
log.debug(f"Calling {method_name}({args[0]})")
|
log.debug(f"Calling {method_name}({args[0]})")
|
||||||
# Initialize dataclasses from the payload
|
# Initialize dataclasses from the payload
|
||||||
@@ -86,7 +87,6 @@ class Webview:
|
|||||||
# from_dict really takes Anything and returns an instance of the type/class
|
# from_dict really takes Anything and returns an instance of the type/class
|
||||||
reconciled_arguments[k] = from_dict(arg_class, v)
|
reconciled_arguments[k] = from_dict(arg_class, v)
|
||||||
|
|
||||||
op_key = seq.decode()
|
|
||||||
reconciled_arguments["op_key"] = op_key
|
reconciled_arguments["op_key"] = op_key
|
||||||
# TODO: We could remove the wrapper in the MethodRegistry
|
# TODO: We could remove the wrapper in the MethodRegistry
|
||||||
# and just call the method directly
|
# and just call the method directly
|
||||||
@@ -99,11 +99,11 @@ class Webview:
|
|||||||
)
|
)
|
||||||
|
|
||||||
log.debug(f"Result for {method_name}: {serialized}")
|
log.debug(f"Result for {method_name}: {serialized}")
|
||||||
self.return_(seq.decode(), FuncStatus.SUCCESS, serialized)
|
self.return_(op_key, FuncStatus.SUCCESS, serialized)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(f"Error while handling result of {method_name}")
|
log.exception(f"Error while handling result of {method_name}")
|
||||||
result = ErrorDataClass(
|
result = ErrorDataClass(
|
||||||
op_key=seq.decode(),
|
op_key=op_key,
|
||||||
status="error",
|
status="error",
|
||||||
errors=[
|
errors=[
|
||||||
ApiError(
|
ApiError(
|
||||||
@@ -116,7 +116,7 @@ class Webview:
|
|||||||
serialized = json.dumps(
|
serialized = json.dumps(
|
||||||
dataclass_to_dict(result), indent=4, ensure_ascii=False
|
dataclass_to_dict(result), indent=4, ensure_ascii=False
|
||||||
)
|
)
|
||||||
self.return_(seq.decode(), FuncStatus.FAILURE, serialized)
|
self.return_(op_key, FuncStatus.FAILURE, serialized)
|
||||||
finally:
|
finally:
|
||||||
del self.threads[op_key]
|
del self.threads[op_key]
|
||||||
|
|
||||||
@@ -125,9 +125,7 @@ class Webview:
|
|||||||
target=thread_task, args=(stop_event,), name="WebviewThread"
|
target=thread_task, args=(stop_event,), name="WebviewThread"
|
||||||
)
|
)
|
||||||
thread.start()
|
thread.start()
|
||||||
self.threads[seq.decode()] = WebThread(
|
self.threads[op_key] = WebThread(thread=thread, stop_event=stop_event)
|
||||||
thread=thread, stop_event=stop_event
|
|
||||||
)
|
|
||||||
|
|
||||||
def __enter__(self) -> "Webview":
|
def __enter__(self) -> "Webview":
|
||||||
return self
|
return self
|
||||||
|
|||||||
Reference in New Issue
Block a user