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

# %% auto 0
__all__ = ['get_roles', 'get_role_grants', 'get_role_membership', 'create_role', 'set_default_role', 'update_role_grants',
           'role_membership_add_user']

# %% ../../nbs/routes/role.ipynb 2
import httpx

import domolibrary.client.get_data as gd
import domolibrary.client.ResponseGetData as rgd
import domolibrary.client.DomoAuth as dmda
import domolibrary.client.DomoError as de


# %% ../../nbs/routes/role.ipynb 5
async def get_roles(auth: dmda.DomoAuth,
                    debug_api: bool = False,
                    session: httpx.AsyncClient = None,
                    ) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/authorization/v1/roles'

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

    if len(res.response) == 0 or not res.is_success:
        raise Role_NotRetrieved(
            domo_instance=auth.domo_instance,
            function_name='get_roles',
            status=res.status,
            message = 'failed to retrieve roles')
    
    return res


# %% ../../nbs/routes/role.ipynb 8
async def get_role_grants(auth: dmda.DomoAuth,
                          role_id: str,
                          debug_api: bool = False,
                          session : httpx.AsyncClient = None
                          ) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/authorization/v1/roles/{role_id}/authorities'

    if debug_api:
        print(url)

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

    if len(res.response) == 0:
        raise Role_NotRetrieved(
            domo_instance= auth.domo_instance,
            function_name='get_role_grants',
            message=f"no grants returned for role {role_id}",
            status=res.status
        )

    return res

# %% ../../nbs/routes/role.ipynb 11
async def get_role_membership(auth: dmda.DomoAuth,
                              role_id: str,
                              debug_api: bool = False, 
                              session: httpx.AsyncClient = None,
                              return_raw : bool = False
                              ) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/authorization/v1/roles/{role_id}/users'

    if debug_api:
        print(url)

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

    if len(res.response.get('users')) == 0:
        raise Role_NotRetrieved(
            domo_instance=auth.domo_instance,
            function_name='get_role_membership',
            message=f"no users returned for role {role_id}",
            status=res.status
        )

    if return_raw:
        return res
    
    res.response = res.response.get('users')
    
    return res

# %% ../../nbs/routes/role.ipynb 15
async def create_role(auth: dmda.DomoAuth,
                      name: str,
                      description: str,
                      debug_api: bool = False) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/authorization/v1/roles'

    body = {
        'name': name,
        'description': description
    }

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

    return res

# %% ../../nbs/routes/role.ipynb 16
async def set_default_role(auth: dmda.DomoAuth,
                           role_id: str,
                           debug_api: bool = False,
                           session: httpx.AsyncClient = None) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/content/v1/customer-states/user.roleid.default'

    body = {
        'name': 'user.roleid.default',
        'value': role_id
    }

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='PUT',
        debug_api=debug_api,
        body=body,
        session=session
    )
    return res


# %% ../../nbs/routes/role.ipynb 20
async def update_role_grants(auth: dmda.DomoAuth,
                             role_id: str,
                             role_grant_list: list[str],
                             debug_api: bool = False) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/authorization/v1/roles/{role_id}/authorities'

    if debug_api:
        print(url)

    res = await gd.get_data(auth=auth,
                         url=url,
                         method='PUT',
                         debug_api=debug_api, body=role_grant_list)
    return res

# %% ../../nbs/routes/role.ipynb 21
async def role_membership_add_user(auth: dmda.DomoAuth,
                                   role_id: str,
                                   user_list: list[str],
                                   debug_api: bool = False) -> rgd.ResponseGetData:
    url = f'https://{auth.domo_instance}.domo.com/api/authorization/v1/roles/{role_id}/users'

    if debug_api:
        print(url)

    res = await gd.get_data(
        auth=auth,
        url=url,
        method='PUT',
        debug_api=debug_api,
        body=user_list
    )
    return res
