# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_process.ipynb.

# %% auto 0
__all__ = ['ResponseProcess', 'ProcessFunction_ResponseTypeError', 'ProcessFunction_Error', 'process_function']

# %% ../nbs/00_process.ipynb 2
from dataclasses import dataclass, field
from typing import Any, Callable, List
from functools import wraps
import httpx

# %% ../nbs/00_process.ipynb 3
@dataclass
class ResponseProcess:
    function_name : str
    id : str
    message: List = field(default_factory = lambda: ['init'])
    response : Any = field(repr = False, default = None)
    is_success : bool = False,
    
    def to_json(self):
        return [ {** self.__dict__, 'message' : msg} for msg in self.message]

# %% ../nbs/00_process.ipynb 5
class ProcessFunction_ResponseTypeError(TypeError):
    def __init__(self, result):
        super().__init__(
            f"Expected function to return an instance of ResponseProcess got {type(result)} instead.  Refactor function to return ResponseGetData class"
        )

class ProcessFunction_Error(Exception):
    def __init__(self, log, auth):
        super().__init__(f"💀 | {log.function_name } | {id} | {message} | in {auth.domo_instance}")


def process_function(func: Callable[..., Any]) -> Callable[..., Any]:
    """
    Decorator for route functions to ensure they receive certain arguments.
    If these arguments are not provided, default values are used.

    Args:
        func (Callable[..., Any]): The function to decorate.

    Returns:
        Callable[..., Any]: The decorated function.

    The decorated function takes the following arguments:
        *args (Any): Positional arguments for the decorated function.
        parent_class (str, optional): The parent class. Defaults to None.
        debug_num_stacks_to_drop (int, optional): The number of stacks to drop for debugging. Defaults to 1.
        debug_api (bool, optional): Whether to debug the API. Defaults to False.
        session (httpx.AsyncClient, optional): The HTTPX client session. Defaults to None.
        **kwargs (Any): Additional keyword arguments for the decorated function.
    """

    @wraps(func)
    async def wrapper(
        *args: Any,
        auth,
        session : httpx.AsyncClient,
        debug_api : bool,
        log : List[ResponseProcess],
        **kwargs: Any,
    ) -> Any:
        result = await func(
            *args,
            auth = auth,
            debug_api=debug_api,
            session=session,
            log = log,
            **kwargs,
        )

        if not isinstance(result, ResponseProcess):
            raise ProcessFunction_ResponseTypeError(result)

        return result

    return wrapper
