import requests
from loguru import logger


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


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

    def load_big_small_resource(self, filename: str, directory: str) -> LoadResult:
        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))

    def upload_big_small_resource(self, content: bytes, filename: str, directory: str) -> LoadResult:
        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))

    def stop(self):
        pass
