Skip to content
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
6 changes: 0 additions & 6 deletions domaintools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ def iris_investigate(
data_updated_after=None,
expiration_date=None,
create_date=None,
active=None,
search_hash=None,
risk_score=None,
younger_than_date=None,
Expand Down Expand Up @@ -701,9 +700,6 @@ def iris_investigate(
if search_hash:
kwargs["search_hash"] = search_hash

if not (kwargs or domains):
raise ValueError("Need to define investigation using kwarg filters or domains")

if isinstance(domains, (list, tuple)):
domains = ",".join(domains)
if hasattr(data_updated_after, "strftime"):
Expand All @@ -712,8 +708,6 @@ def iris_investigate(
expiration_date = expiration_date.strftime("%Y-%m-%d")
if hasattr(create_date, "strftime"):
create_date = create_date.strftime("%Y-%m-%d")
if isinstance(active, bool):
kwargs["active"] = str(active).lower()

results = self._results(
"iris-investigate",
Expand Down
58 changes: 50 additions & 8 deletions domaintools/decorators.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,81 @@
import functools
import inspect

from typing import List, Union

from domaintools.docstring_patcher import DocstringPatcher
from domaintools.request_validator import RequestValidator


def api_endpoint(spec_name: str, path: str, methods: Union[str, List[str]]):
"""
Decorator to tag a method as an API endpoint.
Decorator to tag a method as an API endpoint AND validate inputs.

Args:
spec_name: The key for the spec in api_instance.specs
path: The API path (e.g., "/users")
methods: A single method ("get") or list of methods (["get", "post"])
that this function handles.
"""

def decorator(func):
func._api_spec_name = spec_name
func._api_path = path

# Always store the methods as a list
if isinstance(methods, str):
func._api_methods = [methods]
else:
func._api_methods = methods
# Normalize methods to a list
normalized_methods = [methods] if isinstance(methods, str) else methods
func._api_methods = normalized_methods

# Get the signature of the original function ONCE
sig = inspect.signature(func)

@functools.wraps(func)
def wrapper(self, *args, **kwargs):

try:
bound_args = sig.bind(*args, **kwargs)
except TypeError:
# If arguments don't match signature, let the actual func raise the error
return func(*args, **kwargs)

arguments = bound_args.arguments

# Robustly find 'self' (it's usually the first argument in bound_args)
# We look for the first value in arguments, or try to get 'self' explicitly.
instance = arguments.pop("self", None)
if not instance and args:
instance = args[0]

# Retrieve the Spec from the instance
# We assume 'self' has a .specs attribute (like DocstringPatcher expects)
spec = getattr(self, "specs", {}).get(spec_name)

if spec:
# Determine which HTTP method is currently being executed.
# If the function allows dynamic methods (e.g. method="POST"), use that.
# Otherwise, default to the first method defined in the decorator.
current_method = kwargs.get("method", normalized_methods[0])

# Run Validation
# This will raise a ValueError and stop execution if validation fails.
try:
RequestValidator.validate(
spec=spec,
path=path,
method=current_method,
parameters=arguments,
)
except ValueError as e:
print(f"[Validation Error] {e}")
raise e

# Proceed with the original function call
return func(*args, **kwargs)

# Copy all tags to the wrapper
# Copy tags to wrapper for the DocstringPatcher to find
wrapper._api_spec_name = func._api_spec_name
wrapper._api_path = func._api_path
wrapper._api_methods = func._api_methods

return wrapper

return decorator
Expand Down
Loading