run :: Config -> RunRequest -> IO (Either Error RunResult)
run Config{..} runRequest =
    let
        reqConfig =
            Req.defaultHttpConfig
                { Req.httpConfigRedirectCount = 0
                , Req.httpConfigCheckResponse = \_ _ _ -> Nothing
                , Req.httpConfigRetryJudge = \_ _ -> False
                , Req.httpConfigRetryJudgeException = \_ _ -> False
                }

        mkOptions :: Req.Option scheme -> Req.Option scheme
        mkOptions urlOptions =
            urlOptions
                <> Req.header "X-Access-Token" accessToken
                <> Req.responseTimeout (responseTimeout * 1000000)

        runReq httpOrHttpsUrl = do
            res <- Req.runReq reqConfig $ do
                case httpOrHttpsUrl of
                    Left (httpUrl, urlOptions) ->
                        Req.req Req.POST httpUrl (Req.ReqBodyJson runRequest) Req.bsResponse (mkOptions urlOptions)

                    Right (httpsUrl, urlOptions) ->
                        Req.req Req.POST httpsUrl (Req.ReqBodyJson runRequest) Req.bsResponse (mkOptions urlOptions)
            pure (handleResponse res)
    in do
    uri <- URI.mkURI (baseUrl <> "/run")
    case Req.useURI uri of
        Nothing -> do
            pure $ Left ParseUrlError

        Just httpOrHttpsUrl -> do
            eitherResult <- Exception.try (runReq httpOrHttpsUrl)
            case eitherResult of
                Left exception ->
                    pure (Left $ HttpException exception)

                Right e ->
                    pure e
run :: Config -> RunRequest -> IO (Either Error RunResult)
run Config{..} runRequest =
    let
        reqConfig =
            Req.defaultHttpConfig
                { Req.httpConfigRedirectCount = 0
                , Req.httpConfigCheckResponse = \_ _ _ -> Nothing
                , Req.httpConfigRetryJudge = \_ _ -> False
                , Req.httpConfigRetryJudgeException = \_ _ -> False
                }

        mkOptions :: Req.Option scheme -> Req.Option scheme
        mkOptions urlOptions =
            urlOptions
                <> Req.header "X-Access-Token" accessToken
                <> Req.responseTimeout (responseTimeout * 1000000)

        runReq httpOrHttpsUrl = do
            res <- Req.runReq reqConfig $ do
                case httpOrHttpsUrl of
                    Left (httpUrl, urlOptions) ->
                        Req.req Req.POST httpUrl (Req.ReqBodyJson runRequest) Req.bsResponse (mkOptions urlOptions)

                    Right (httpsUrl, urlOptions) ->
                        Req.req Req.POST httpsUrl (Req.ReqBodyJson runRequest) Req.bsResponse (mkOptions urlOptions)
            pure (handleResponse res)
    in do
    uri <- URI.mkURI (baseUrl <> "/run")
    case Req.useURI uri of
        Nothing -> do
            pure $ Left ParseUrlError

        Just httpOrHttpsUrl -> do
            eitherResult <- Exception.try (runReq httpOrHttpsUrl)
            case eitherResult of
                Left exception ->
                    pure (Left $ HttpException exception)

                Right e ->
                    pure e
run Config{..} runRequest =
    let
        reqConfig =
            Req.defaultHttpConfig
                { Req.httpConfigRedirectCount = 0
                , Req.httpConfigCheckResponse = \_ _ _ -> Nothing
                , Req.httpConfigRetryJudge = \_ _ -> False
                , Req.httpConfigRetryJudgeException = \_ _ -> False
                }

        mkOptions :: Req.Option scheme -> Req.Option scheme
        mkOptions urlOptions =
            urlOptions
                <> Req.header "X-Access-Token" accessToken
                <> Req.responseTimeout (responseTimeout * 1000000)

        runReq httpOrHttpsUrl = do
            res <- Req.runReq reqConfig $ do
                case httpOrHttpsUrl of
                    Left (httpUrl, urlOptions) ->
                        Req.req Req.POST httpUrl (Req.ReqBodyJson runRequest) Req.bsResponse (mkOptions urlOptions)

                    Right (httpsUrl, urlOptions) ->
                        Req.req Req.POST httpsUrl (Req.ReqBodyJson runRequest) Req.bsResponse (mkOptions urlOptions)
            pure (handleResponse res)
    in do
    uri <- URI.mkURI (baseUrl <> "/run")
    case Req.useURI uri of
        Nothing -> do
            pure $ Left ParseUrlError

        Just httpOrHttpsUrl -> do
            eitherResult <- Exception.try (runReq httpOrHttpsUrl)
            case eitherResult of
                Left exception ->
                    pure (Left $ HttpException exception)

                Right e ->
                    pure e