import requests
from loguru import logger


cdef class LoadResult:
    def __init__(self, err, data=None):
        self.err = err
        self.data = data


cdef class LoaderHttpClient:
    def __init__(self, base_url: str):
        self.base_url = base_url.rstrip("/")

    cdef LoadResult load_big_small_resource(self, str filename, str directory):
        try:
            response = requests.post(
                f"{self.base_url}/load/{filename}",
                json={"filename": filename, "folder": directory},
                stream=True,
            )
            response.raise_for_status()
            return LoadResult(None, response.content)
        except Exception as e:
            logger.error(f"LoaderHttpClient.load_big_small_resource failed: {e}")
            return LoadResult(str(e))

    cdef LoadResult upload_big_small_resource(self, bytes content, str filename, str directory):
        try:
            response = requests.post(
                f"{self.base_url}/upload/{filename}",
                files={"data": (filename, content)},
                data={"folder": directory},
            )
            response.raise_for_status()
            return LoadResult(None)
        except Exception as e:
            logger.error(f"LoaderHttpClient.upload_big_small_resource failed: {e}")
            return LoadResult(str(e))

    cdef stop(self):
        pass
