-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from 0hsn/refactor/fetch-mod-general-refactor
Refactor: General refactor Fetch module
- Loading branch information
Showing
12 changed files
with
913 additions
and
877 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,123 @@ | ||
""" | ||
Fetch module | ||
""" | ||
import sys | ||
from collections import abc | ||
|
||
from chk.infrastructure.document import VersionedDocumentSupport | ||
from chk.infrastructure.file_loader import ExecuteContext, FileContext | ||
from chk.infrastructure.logging import debug, error_trace, with_catch_log | ||
from chk.infrastructure.symbol_table import ExecResponse, ExposeManager, \ | ||
VariableTableManager, \ | ||
Variables | ||
from chk.infrastructure.view import PresentationService, die_with_error | ||
from chk.modules.fetch.entities import FetchTask | ||
from chk.modules.fetch.services import FetchPresenter, HttpDocumentSupport | ||
|
||
|
||
@with_catch_log | ||
def call(file_ctx: FileContext, exec_ctx: ExecuteContext) -> ExecResponse: | ||
"""Call a http document""" | ||
|
||
debug(file_ctx) | ||
debug(exec_ctx) | ||
|
||
r_exception: Exception | None = None | ||
variable_doc = Variables() | ||
output_data = Variables() | ||
exposed_data = {} | ||
|
||
try: | ||
http_doc = HttpDocumentSupport.from_file_context(file_ctx) | ||
debug(http_doc.model_dump_json()) | ||
|
||
VersionedDocumentSupport.validate_with_schema( | ||
HttpDocumentSupport.build_schema(), http_doc | ||
) | ||
|
||
VariableTableManager.handle(variable_doc, http_doc, exec_ctx) | ||
debug(variable_doc.data) | ||
|
||
HttpDocumentSupport.process_request_template(http_doc, variable_doc) | ||
debug(http_doc.model_dump_json()) | ||
|
||
# try: | ||
response = HttpDocumentSupport.execute_request(http_doc) | ||
|
||
output_data = Variables({"_response": response.model_dump()}) | ||
debug(output_data.data) | ||
|
||
exposed_data = ExposeManager.get_exposed_replaced_data( | ||
http_doc, | ||
{**variable_doc.data, **output_data.data}, | ||
) | ||
debug(exposed_data) | ||
|
||
except Exception as ex: | ||
r_exception = ex | ||
error_trace(exception=sys.exc_info()).error(ex) | ||
|
||
# TODO: instead if sending specific report items, and making presentable in other | ||
# module, we should prepare and long and short form of presentable that can be | ||
# loaded via other module | ||
|
||
request_method, request_url = "", "" | ||
|
||
if "request" in file_ctx.document: | ||
if "method" in file_ctx.document["request"]: | ||
request_method = file_ctx.document["request"]["method"] | ||
if "url" in file_ctx.document["request"]: | ||
request_url = file_ctx.document["request"]["url"] | ||
|
||
return ExecResponse( | ||
file_ctx=file_ctx, | ||
exec_ctx=exec_ctx, | ||
variables_exec=output_data, | ||
variables=variable_doc, | ||
exception=r_exception, | ||
exposed=exposed_data, | ||
report={ | ||
"is_success": r_exception is None, | ||
"request_method": request_method, | ||
"request_url": request_url, | ||
}, | ||
) | ||
|
||
|
||
@with_catch_log | ||
def execute( | ||
ctx: FileContext, exec_ctx: ExecuteContext, cb: abc.Callable = lambda *args: ... | ||
) -> None: | ||
"""Call with a http document | ||
Args: | ||
ctx: FileContext object to handle | ||
exec_ctx: ExecuteContext | ||
cb: Callable | ||
""" | ||
|
||
try: | ||
exr = call(file_ctx=ctx, exec_ctx=exec_ctx) | ||
cb({ctx.filepath_hash: exr.variables_exec.data}) | ||
PresentationService.display(exr, exec_ctx, FetchPresenter) | ||
except Exception as ex: | ||
error_trace(exception=sys.exc_info()).error(ex) | ||
die_with_error(ex, FetchPresenter, exec_ctx.options["format"]) | ||
|
||
|
||
@with_catch_log | ||
def task_fetch(**kwargs: dict) -> ExecResponse: | ||
"""Task impl""" | ||
|
||
if not (doc := kwargs.get("task", {})): | ||
raise ValueError("Wrong task format given.") | ||
|
||
_task = FetchTask(**doc) | ||
|
||
return call( | ||
FileContext.from_file(_task.file), | ||
ExecuteContext( | ||
options={"dump": True, "format": True}, | ||
arguments=_task.arguments | {"variables": _task.variables}, | ||
), | ||
) |
Oops, something went wrong.