doRequest :: (MonadThrow m, MonadIO m) => Bool -> Session -> Request -> (Request -> m (Response b)) -> (b -> m a) -> m a
doRequest allowRenew session req getResponse processBody = do
  authReq <- liftIO $ authorizeRequest session req
  resp <- getResponse authReq
  let
    responseStatus =
      getResponseStatus resp
    throwException =
      throwM $ HttpExceptionRequest req $ StatusCodeException (void resp) empty
  if responseStatus == ok200
    then
    processBody $ getResponseBody resp
    else
    if responseStatus == unauthorized401 && allowRenew
    then do
      newAccessToken <- liftIO $ tryRenewToken session
      case newAccessToken of
        Nothing ->
          throwException
        Just _ ->
          doRequest False session req getResponse processBody
    else
      throwException
doRequest :: (MonadThrow m, MonadIO m) => Bool -> Session -> Request -> (Request -> m (Response b)) -> (b -> m a) -> m a
doRequest allowRenew session req getResponse processBody = do
  authReq <- liftIO $ authorizeRequest session req
  resp <- getResponse authReq
  let
    responseStatus =
      getResponseStatus resp
    throwException =
      throwM $ HttpExceptionRequest req $ StatusCodeException (void resp) empty
  if responseStatus == ok200
    then
    processBody $ getResponseBody resp
    else
    if responseStatus == unauthorized401 && allowRenew
    then do
      newAccessToken <- liftIO $ tryRenewToken session
      case newAccessToken of
        Nothing ->
          throwException
        Just _ ->
          doRequest False session req getResponse processBody
    else
      throwException
doRequest allowRenew session req getResponse processBody = do
  authReq <- liftIO $ authorizeRequest session req
  resp <- getResponse authReq
  let
    responseStatus =
      getResponseStatus resp
    throwException =
      throwM $ HttpExceptionRequest req $ StatusCodeException (void resp) empty
  if responseStatus == ok200
    then
    processBody $ getResponseBody resp
    else
    if responseStatus == unauthorized401 && allowRenew
    then do
      newAccessToken <- liftIO $ tryRenewToken session
      case newAccessToken of
        Nothing ->
          throwException
        Just _ ->
          doRequest False session req getResponse processBody
    else
      throwException