# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/routes/domo.ipynb.

# %% auto 0
__all__ = ['get_workflow', 'generate_trigger_workflow_body', 'trigger_workflow', 'get_workflow_trigger_history',
           'get_workflow_executions', 'get_fileset', 'get_fileset_files']

# %% ../../nbs/routes/domo.ipynb 1
import domolibrary.client.get_data as gd
import domolibrary.client.DomoError as dmde
import domolibrary.client.DomoAuth as dmda


# %% ../../nbs/routes/domo.ipynb 5
async def get_workflow(
    auth: dmda.DomoAuth, model_id, version_id, debug_api: bool = False
):

    url = f"https://{auth.domo_instance}.domo.com/api/workflow/v1/models/{model_id}/versions/{version_id}"
    res = await gd.get_data(auth=auth, method="GET", url=url, debug_api=debug_api)

    if not res.is_success:
        raise dmde.RouteError(res=res)

    return res

# %% ../../nbs/routes/domo.ipynb 8
def generate_trigger_workflow_body(
    starting_tile, model_id, version_id, execution_params: dict
):
    return {
        "messageName": starting_tile,
        "version": version_id,
        "modelId": model_id,
        "data": execution_params,
    }


async def trigger_workflow(
    auth: dmda.DomoAuth,
    starting_tile: str,
    model_id: str,
    version_id: str,
    execution_parameters: dict = None,
    debug_api: bool = False,
):
    body = generate_trigger_workflow_body(
        starting_tile=starting_tile,
        model_id=model_id,
        execution_params=execution_parameters,
        version_id=version_id,
    )

    url = f"https://{auth.domo_instance}.domo.com/api/workflow/v1/instances/message"

    res = await gd.get_data(
        method="POST", url=url, body=body, auth=auth, debug_api=debug_api
    )

    if not res.is_success:
        raise dmde.RouteError(res=res)

    return res

# %% ../../nbs/routes/domo.ipynb 12
async def get_workflow_trigger_history(auth : dmda.DomoAuth, model_id, debug_api : bool = False):

    url = f'https://{auth.domo_instance}.domo.com/api/workflow/v2/executions/{model_id}'

    res = await gd.get_data(auth=auth, method="GET", url=url, debug_api=debug_api)

    if not res.is_success:
        raise dmde.RouteError(res=res)
    
    return res


# %% ../../nbs/routes/domo.ipynb 14
async def get_workflow_executions(auth : dmda.DomoAuth,
                                  model_id: str,
                                  version_id: str,
                                  debug_api : bool = False
                                  ):
    params = {"modelId" : model_id,
            #   "triggerTypes" : "ALERT,API,APP_STUDIO,CUSTOM_APP,MANUAL,TIMER,WORKFLOW"
              "version" : version_id,
              # "status"  : "IN_PROGRESS"
    }
    
    url = f"https://{auth.domo_instance}.domo.com/api/workflow/v2/executions"

    res = await gd.get_data(auth=auth, method="GET", url=url, debug_api=debug_api, params = params)

    if not res.is_success:
        raise dmde.RouteError(res=res)
    
    return res

    

# %% ../../nbs/routes/domo.ipynb 18
async def get_fileset(auth: dmda.DomoAuth, fileset_id, debug_api: bool = False):
    url = (
        f"https://{auth.domo_instance}.domo.com/api/files/v1/filesets/{fileset_id}"
    )
    res = await gd.get_data(auth=auth, method="GET", url=url, debug_api=debug_api)

    if not res.is_success:
        raise dmde.RouteError(res=res, debug_api=debug_api)

    return res

# %% ../../nbs/routes/domo.ipynb 21
async def get_fileset_files(
        auth : dmda.DomoAuth, domo_fileset_id, body: dict = {}, debug_api: bool = False
):
    url = f'https://{auth.domo_instance}.domo.com/api/files/v1/filesets/{domo_fileset_id}/files/search?directoryPath=&immediateChildren=true'
    
    if not body:
        # default body will pull all files within the given fileset_id
        body = {"fieldSort":[{"field":"created","order":"DESC"}],"filters":[],"dateFilters":[]}

    res = await gd.get_data(auth=auth, method='POST', url=url, body=body, debug_api=debug_api)
    if not res.is_success:
        raise dmde.RouteError(res=res)
    
    return res

