-- | Use the curl session to fetch a possibly login restricted page.
getInternalPage :: YukariSettings -> Curl -> String -> IO (Maybe String)
getInternalPage ys curl url = do
  r <- do_curl_ curl url method_GET :: IO CurlResponse
  if respCurlCode r /= CurlOK
    then retryFetch r $ _connectionRetries ys
    else return . Just $ respBody r
  where
    retryFetch :: CurlResponse -> Integer -> IO (Maybe String)
    retryFetch r ret
      | ret <= 0 = do
          verbPrint Low ys [ "Failed to fetch:", url, ";"
                           , show $ respCurlCode r, "--"
                           , respStatusLine r
                           ]
          return Nothing
      | otherwise = do
          verbPrint High ys [ "Failed to fetch", url
                            , show $ ret - 1
                            , attemptFormat $ ret - 1
                            , "remaining."
                            ]
          getInternalPage (ys { _connectionRetries = ret - 1 }) curl url
      where
        attemptFormat 1 = "attempt"
        attemptFormat _ = "attempts"

-- | We check if we're banned first before we even try to log in.
-- While it is an extra GET, we don't POST account information needlessly
-- and this is only done once per log in anyway.
getInternalPage :: YukariSettings -> Curl -> String -> IO (Maybe String)
getInternalPage ys curl url = do
  r <- do_curl_ curl url method_GET :: IO CurlResponse
  if respCurlCode r /= CurlOK
    then retryFetch r $ _connectionRetries ys
    else return . Just $ respBody r
  where
    retryFetch :: CurlResponse -> Integer -> IO (Maybe String)
    retryFetch r ret
      | ret <= 0 = do
          verbPrint Low ys [ "Failed to fetch:", url, ";"
                           , show $ respCurlCode r, "--"
                           , respStatusLine r
                           ]
          return Nothing
      | otherwise = do
          verbPrint High ys [ "Failed to fetch", url
                            , show $ ret - 1
                            , attemptFormat $ ret - 1
                            , "remaining."
                            ]
          getInternalPage (ys { _connectionRetries = ret - 1 }) curl url
      where
        attemptFormat 1 = "attempt"
        attemptFormat _ = "attempts"

-- | We check if we're banned first before we even try to log in.
-- While it is an extra GET, we don't POST account information needlessly
-- and this is only done once per log in anyway.
getInternalPage ys curl url = do
  r <- do_curl_ curl url method_GET :: IO CurlResponse
  if respCurlCode r /= CurlOK
    then retryFetch r $ _connectionRetries ys
    else return . Just $ respBody r
  where
    retryFetch :: CurlResponse -> Integer -> IO (Maybe String)
    retryFetch r ret
      | ret <= 0 = do
          verbPrint Low ys [ "Failed to fetch:", url, ";"
                           , show $ respCurlCode r, "--"
                           , respStatusLine r
                           ]
          return Nothing
      | otherwise = do
          verbPrint High ys [ "Failed to fetch", url
                            , show $ ret - 1
                            , attemptFormat $ ret - 1
                            , "remaining."
                            ]
          getInternalPage (ys { _connectionRetries = ret - 1 }) curl url
      where
        attemptFormat 1 = "attempt"
        attemptFormat _ = "attempts"

-- | We check if we're banned first before we even try to log in.
-- While it is an extra GET, we don't POST account information needlessly
-- and this is only done once per log in anyway.