from urllib.parse import urljoin

import httpx

API_BASE_URL = "https://api.librus.pl"
API_TOKEN = "Mjg6ODRmZGQzYTg3YjAzZDNlYTZmZmU3NzdiNThiMzMyYjE="

async def base_post_rq(endpoint, body=None, access_token=None, client: httpx.AsyncClient | None = None):
    if body is None:
        body = {}

    headers = {"Authorization": f"Basic {API_TOKEN}"}
    if access_token is not None:
        headers = {"Authorization": f"Bearer {access_token}"}

    if client is None:
        async with httpx.AsyncClient(timeout=30.0) as temp_client:
            return await temp_client.post(
                urljoin(API_BASE_URL, endpoint),
                data=body,
                headers=headers,
            )
    else:
        return await client.post(
            urljoin(API_BASE_URL, endpoint),
            data=body,
            headers=headers,
        )

async def base_get_rq(endpoint, access_token=None, client: httpx.AsyncClient | None = None):
    headers = {"Authorization": f"Basic {API_TOKEN}"}
    if access_token is not None:
        headers = {"Authorization": f"Bearer {access_token}"}

    if client is None:
        async with httpx.AsyncClient(timeout=30.0) as temp_client:
            return await temp_client.get(
                urljoin(API_BASE_URL, endpoint),
                headers=headers,
            )
    else:
        return await client.get(
            urljoin(API_BASE_URL, endpoint),
            headers=headers,
        )
