from fastapi import HTTPException, status

from gallery.easel.core import AppRequest
from gallery.sketch.api import API


def api_resolver(api_type: type[API]):
    def get_api(request: AppRequest, provider: str) -> API:
        providers = request.app.state.api.get_api_providers(api_type)
        if provider not in providers:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail={"provider": f"'{provider}' not in {providers}"},
            )
        return request.app.state.api.get_api(api_type, provider)

    return get_api
