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

# %% auto 0
__all__ = ['get_boards', 'get_board_epics', 'get_epic', 'get_issue_by_id', 'get_board_issues', 'get_issue_links',
           'get_issue_comments']

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

import domolibrary_extensions.client as gd
import domolibrary_extensions.jira.auth as ja

# %% ../../nbs/jira/routes.ipynb 5
async def get_boards(
    auth: ja.JiraAuth,
    params: dict = None,
    debug_api: bool = False,
    debug_loop: bool = False,
    return_raw: bool = False,
    client: httpx.AsyncClient = None,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    timeout: int = 30,
):
    url = f"https://{auth.instance}/rest/agile/1.0/board"

    def arr_fn(res):
        return res.response["values"]

    client = client or httpx.AsyncClient()

    res = await gd.looper(
        url=url,
        auth=auth,
        arr_fn=arr_fn,
        params=params,
        method="GET",
        debug_loop=debug_loop,
        debug_api=debug_api,
        client=client,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        timeout=timeout,
    )

    if return_raw:
        return res

    return res.response

# %% ../../nbs/jira/routes.ipynb 11
async def get_board_epics(
    auth: ja.JiraAuth,
    board_id: int,
    params: dict = None,
    debug_api: bool = False,
    debug_loop: bool = False,
    return_raw: bool = False,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    client: httpx.AsyncClient = None,
    timeout: int = 30,
):
    url = f"https://{auth.instance}/rest/agile/1.0/board/{board_id}/epic"

    def arr_fn(res):
        return res.response["values"]

    res = await gd.looper(
        method="get",
        url=url,
        auth=auth,
        params=params,
        debug_api=debug_api,
        debug_loop=debug_loop,
        arr_fn=arr_fn,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        client=client,
        timeout=timeout,
    )

    if return_raw:
        return res

    return res.response

# %% ../../nbs/jira/routes.ipynb 14
async def get_epic(
    epic_id,
    auth: ja.JiraAuth,
    debug_api: bool = False,
    return_raw: bool = False,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    client: httpx.AsyncClient = None,
    timeout: int = 30,
):
    url = f"https://{auth.instance}/rest/agile/1.0/epic/{epic_id}"

    res = await gd.get_data(
        method="get",
        url=url,
        auth=auth,
        debug_api=debug_api,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        client=client,
        timeout=timeout,
    )

    if return_raw:
        return res

    return res.response

# %% ../../nbs/jira/routes.ipynb 18
async def get_issue_by_id(
    auth: ja.JiraAuth,
    issue_id: int,
    debug_api: bool = False,
    return_raw: bool = False,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    client: httpx.AsyncClient = None,
    timeout: int = 30,
):
    url = f"https://{auth.instance}/rest/api/2/issue/{issue_id}"

    res = await gd.get_data(
        method="get",
        url=url,
        auth=auth,
        debug_api=debug_api,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        client=client,
        timeout=timeout,
    )

    if return_raw:
        return res

    return res.response

# %% ../../nbs/jira/routes.ipynb 19
async def get_board_issues(
    auth: ja.JiraAuth,
    board_id: int,
    params: dict = None,
    debug_api: bool = False,
    debug_loop: bool = False,
    return_raw: bool = False,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    client: httpx.AsyncClient = None,
    timeout: int = 30,
):
    url = f"https://{auth.instance}/rest/agile/1.0/board/{board_id}/issue"

    def arr_fn(res):
        return res.response["issues"]

    res = await gd.looper(
        method="get",
        url=url,
        auth=auth,
        params=params,
        offset_params={"offset": "startAt", "limit": "maxResults"},
        offset_params_is_header=True,
        debug_api=debug_api,
        debug_loop=debug_loop,
        arr_fn=arr_fn,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        client=client,
        timeout=timeout,
    )

    if return_raw:
        return res

    return res.response

# %% ../../nbs/jira/routes.ipynb 20
async def get_issue_links(
    auth: ja.JiraAuth,
    issue_id: int,
    debug_api: bool = False,
    return_raw: bool = False,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    client: httpx.AsyncClient = None,
    timeout: int = 30,
):
    # url = f'https://{auth.instance}/rest/api/2/issueLink'
    url = f"https://{auth.instance}/rest/api/2/issue/{issue_id}/remotelink"

    res = await gd.get_data(
        method="get",
        url=url,
        auth=auth,
        debug_api=debug_api,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        client=client,
        timeout=timeout,
    )

    if return_raw:
        return res

    return res.response

# %% ../../nbs/jira/routes.ipynb 21
async def get_issue_comments(
    issue_id,
    auth: ja.JiraAuth,
    debug_api: bool = False,
    return_raw: bool = False,
    is_ignore_cache: bool = False,
    cache_path: str = None,
    client: httpx.AsyncClient = None,
    timeout: int = 30,
):
    url = (
        f"https://{auth.instance}/rest/api/2/issue/{issue_id}/comment?orderBy=-created&maxResults=0&expand=renderedBody"
        if auth.is_datacenter
        else f"https://{auth.instance}/rest/api/2/issue/{issue_id}/comment?orderBy=-created&maxResults=0&expand=renderedBody"
    )

    def arr_fn(res):
        return res.response["comments"]

    res = await gd.looper(
        method="get",
        url=url,
        auth=auth,
        arr_fn=arr_fn,
        offset_params={"offset": "startAt", "limit": "maxResults"},
        offset_params_is_header=True,
        debug_api=debug_api,
        is_ignore_cache=is_ignore_cache,
        cache_path=cache_path,
        client=client,
        tmeout=timeout,
    )

    if return_raw:
        return res

    return res.response
