{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UnicodeSyntax #-}
--------------------------------------------------------------------------------
-- File   : Info
-- Author : Alejandro Gómez Londoño
-- Date   : Sun Sep 14 01:56:28 2014
-- Description : Dropbox API info queries
--------------------------------------------------------------------------------
-- Change log :

--------------------------------------------------------------------------------

module MonadBox.Info where

import Control.Lens ((&), (.~), (^.), (?~))
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Text.IO as Text
import qualified MonadBox.URI as URI
import MonadBox.Types (Token,DBAcountInfo(display_name))
import Network.Wreq (auth, defaults, getWith, oauth2Bearer, responseBody)
import MonadBox.Util (asJSON)
import MonadBox.Exceptions (catchStatus)
import Control.Exception (catch)

doLogin ∷ Token → IO ()
doLogin token = (flip  catch) catchStatus $ do
  r ← _DBAcountInfo token
  putStr "Welcome " >> Text.putStrLn (display_name r)

_DBAcountInfo ∷ Token → IO DBAcountInfo
_DBAcountInfo token = do
  let opts = defaults & auth ?~ oauth2Bearer (encodeUtf8 token)
  r ← asJSON =<< getWith opts URI.info
  return $ r ^. responseBody
