Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENHANCEMENT]: Custom report #1380

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 36 additions & 40 deletions jac-cloud/jac_cloud/plugin/jaseci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections import OrderedDict
from contextlib import suppress
from dataclasses import Field, MISSING, fields
from dataclasses import Field, MISSING, fields, is_dataclass
from functools import wraps
from os import getenv
from re import compile
Expand Down Expand Up @@ -115,36 +115,38 @@ def populate_apis(cls: Type[WalkerArchitype]) -> None:
as_query += PATH_VARIABLE_REGEX.findall(path)

hintings = get_type_hints(cls)
if excluded != "*":
if isinstance(excluded, str):
excluded = [excluded]

for f in fields(cls):
if f.name in excluded:
if f.default is MISSING and not callable(f.default_factory):
raise AttributeError(
f"{cls.__name__} {f.name} should have default or default_factory."
)
continue

f_name = f.name
f_type = hintings[f_name]
if f_type in FILE_TYPES:
files[f_name] = gen_model_field(f_type, f, True)
else:
consts = gen_model_field(f_type, f)

if as_query == "*" or f_name in as_query:
query[f_name] = consts
if is_dataclass(cls):
if excluded != "*":
if isinstance(excluded, str):
excluded = [excluded]

for f in fields(cls):
if f.name in excluded:
if f.default is MISSING and not callable(f.default_factory):
raise AttributeError(
f"{cls.__name__} {f.name} should have default or default_factory."
)
continue

f_name = f.name
f_type = hintings[f_name]
if f_type in FILE_TYPES:
files[f_name] = gen_model_field(f_type, f, True)
else:
body[f_name] = consts
elif any(
f.default is MISSING and not callable(f.default_factory)
for f in fields(cls)
):
raise AttributeError(
f"{cls.__name__} fields should all have default or default_factory."
)
consts = gen_model_field(f_type, f)

if as_query == "*" or f_name in as_query:
query[f_name] = consts
else:
body[f_name] = consts
elif any(
f.default is MISSING and not callable(f.default_factory)
for f in fields(cls)
):
raise AttributeError(
f"{cls.__name__} fields should all have default or default_factory."
)

payload: dict[str, Any] = {
"query": (
Expand Down Expand Up @@ -197,6 +199,9 @@ def api_entry(
Jac.spawn_call(wlk.architype, jctx.entry_node.architype)
jctx.close()

if jctx.custom is not MISSING:
return jctx.custom

resp = jctx.response(wlk.returns)
log_exit(resp, log)

Expand Down Expand Up @@ -236,7 +241,7 @@ def api_root(

settings: dict[str, Any] = {
"tags": ["walker"],
"response_model": ContextResponse[ret_types],
"response_model": ContextResponse[ret_types] | Any,
}
if auth:
settings["dependencies"] = cast(list, authenticator)
Expand Down Expand Up @@ -620,20 +625,11 @@ def decorator(cls: Type[Architype]) -> Type[Architype]:
on_entry=on_entry,
on_exit=on_exit,
)
populate_apis(cls)
populate_apis(cls) # type: ignore[arg-type]
return cls

return decorator

@staticmethod
@hookimpl
def report(expr: Any) -> None: # noqa:ANN401
"""Jac's report stmt feature."""
if not FastAPI.is_enabled():
return JacFeatureImpl.report(expr=expr)

JaseciContext.get().reports.append(expr)

@staticmethod
@hookimpl
def get_root() -> Root:
Expand Down
Loading
Loading