module Facebook.FQL
( fqlQuery
, FQLTime(..)
, FQLList(..)
, FQLObject(..)
) where
import Control.Applicative((<$>))
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Monoid (mempty)
import Data.Text (Text)
import Data.Time (UTCTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import qualified Data.Aeson as A
import qualified Data.Conduit as C
import qualified Data.HashMap.Strict as HMS
import Facebook.Types
import Facebook.Monad
import Facebook.Base
import Facebook.Graph
import Facebook.Pager
fqlQuery :: (C.MonadResource m, MonadBaseControl IO m, A.FromJSON a) =>
Text
-> Maybe (AccessToken anyKind)
-> FacebookT anyAuth m (Pager a)
fqlQuery fql mtoken =
runResourceInFb $ do
let query = ["q" #= fql]
asJson =<< fbhttp =<< fbreq "/fql" mtoken query
newtype FQLTime = FQLTime { unFQLTime :: UTCTime }
deriving (Eq, Ord, Show)
instance A.FromJSON FQLTime where
parseJSON = fmap ( FQLTime
. posixSecondsToUTCTime
. fromInteger)
. A.parseJSON
newtype FQLList a = FQLList { unFQLList :: [a] }
deriving (Eq, Ord, Show)
instance A.FromJSON a => A.FromJSON (FQLList a) where
parseJSON (A.Object o) = FQLList <$> mapM A.parseJSON (HMS.elems o)
parseJSON v = FQLList <$> A.parseJSON v
newtype FQLObject a = FQLObject { unFQLObject :: a }
deriving (Eq, Ord, Show)
instance A.FromJSON a => A.FromJSON (FQLObject a) where
parseJSON (A.Array a) | a == mempty = FQLObject <$> A.parseJSON (A.Object mempty)
parseJSON v = FQLObject <$> A.parseJSON v