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

# %% auto 0
__all__ = ['Client_GET_Error', 'get_api_clients', 'Client_RevokeError', 'revoke_client_id', 'Client_CRUD_Error',
           'create_api_client']

# %% ../../nbs/routes/instance_config_client_id.ipynb 2
from typing import List
import datetime as dt

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 dmde

from domolibrary.utils.convert import convert_string_to_bool

# %% ../../nbs/routes/instance_config_client_id.ipynb 6
class Client_GET_Error(dmde.RouteError):
    def __init__(self, res: rgd.ResponseGetData, message=None):

        super().__init__(res=res, message=message)


@gd.route_function
async def get_api_clients(
    auth: dmda.DomoAuth,
    debug_api: bool = False,
    debug_num_stacks_to_drop=1,
    parent_class=None,
    session: httpx.AsyncClient = None,
    return_raw: bool = False,
) -> rgd.ResponseGetData:

    url = f"https://{auth.domo_instance}.domo.com/api/identity/v1/developer-tokens"

    res = await gd.get_data(
        url=url,
        method="GET",
        auth=auth,
        debug_api=debug_api,
        parent_class=parent_class,
        num_stacks_to_drop=debug_num_stacks_to_drop,
        session=session,
    )
    if not res.is_success:
        raise Client_GET_Error(res=res)

    if return_raw:
        return res

    res.response = res.response["entries"]

    return res

# %% ../../nbs/routes/instance_config_client_id.ipynb 8
class Client_RevokeError(dmde.RouteError):
    def __init__(
        self,
        res : rgd.ResponseGetData,
        client_id :str = None,
        message: str = None,
    ):
        super().__init__(
            res = res, 
            message = message or f"error revoking client {client_id}",
        )


@gd.route_function
async def revoke_client_id(
    auth: dmda.DomoAuth,
    client_id: str,
    debug_api: bool = False,
    debug_num_stacks_to_drop=1,
    parent_class=None,
    session: httpx.AsyncClient = None
):

    url = f"https://{auth.domo_instance}.domo.com/api/identity/v1/developer-tokens/{client_id}"

    res = await gd.get_data(
        url=url,
        method="DELETE",
        auth=auth,
        debug_api=debug_api,
        parent_class=parent_class,
        num_stacks_to_drop=debug_num_stacks_to_drop,
        session=session
    )
    if not res.is_success:
        if res.status == 400:
            raise Client_RevokeError(
                message = f"error revoking client {client_id}, validate that it exists.",
                res = res
            )
        raise Client_RevokeError(
            client_id=client_id,
            res = res
        )

    return res

# %% ../../nbs/routes/instance_config_client_id.ipynb 10
class Client_CRUD_Error(dmde.RouteError):
    def __init__(self, res : rgd.ResponseGetData, message : str = None ):
        super().__init__(res = res , message = message)

@gd.route_function
async def create_api_client(
    auth: dmda.DomoAuth,
    client_name: str,
    client_description : str= f"generated via DL {dt.date.today()}",
    scope : List[str]  = None, # defaults to [data, audit]
    debug_api: bool = False,
    debug_num_stacks_to_drop=1,
    parent_class=None,
    session: httpx.AsyncClient = None,
) -> rgd.ResponseGetData:
    
    scope = scope or ["data", "audit"]

    url = f"https://api.domo.com/clients"
    
    headers = {"X-DOMO-CustomerDomain": f"{auth.domo_instance}.domo.com"}

    res = await gd.get_data(
        url=url,
        method="POST",
        auth=auth,
        body={
            "name": client_name,
            "description": client_description,
            "scope": scope
        },
        headers = headers,
        debug_api=debug_api,
        parent_class=parent_class,
        num_stacks_to_drop=debug_num_stacks_to_drop,
        session = session
    )
    
    if not res.is_success:
        if res.status == 403:
            raise Client_CRUD_Error(res , message = f"{res.response} -- does the client already exist?")

        raise Client_CRUD_Error(res)

    return res

