module Flowdock.REST.Source ( -- * Types SourceType(..) , Source(..) , Sources(..) , Feed(..) , TwitterFollower(..) , TwitterKeyword(..) -- * API calls , listSources , getSource , createSource , deleteSource ) where import Control.Applicative import Control.Monad import Data.Aeson import Data.Aeson.TH import Data.Aeson.Types import Data.Text (Text) import Data.Vector (toList) import Network.Http.Client (Method(..)) import Flowdock.Internal import Flowdock.REST -- ----------------------------------------------------------------------------- -- Deserialized Types data Feed = Feed { feedURL :: !Text, feedTitle :: !(Maybe Text)} deriving (Eq, Show) instance FromJSON Feed where parseJSON (Object o) = do url <- o .: "url" title <- o .: "title" return $ Feed url title parseJSON _ = mzero $(deriveToJSON defaultOptions{fieldLabelModifier = underscoreCase . drop 4} ''Feed) data TwitterKeyword = TwitterKeyword { keywordParam :: !Text , keywordReplies :: !Bool , keywordRetweets :: !Bool } deriving (Eq, Show) instance FromJSON TwitterKeyword where parseJSON (Object o) = do param <- o .: "param" replies <- o .: "replies" retweets <- o .: "retweets" return $ TwitterKeyword param replies retweets parseJSON _ = mzero $(deriveToJSON defaultOptions{fieldLabelModifier = underscoreCase . drop 7} ''TwitterKeyword) data TwitterFollower = TwitterFollower { followerTwitterUserId :: !Text , followerParam :: !Text , followerName :: !Text , followerReplies :: !Bool , followerRetweets :: !Bool } deriving (Eq, Show) instance FromJSON TwitterFollower where parseJSON (Object o) = do tid <- o .: "twitter_user_id" param <- o .: "param" name <- o .: "name" replies <- o .: "replies" retweets <- o .: "retweets" return $ TwitterFollower tid param name replies retweets parseJSON _ = mzero $(deriveToJSON defaultOptions{fieldLabelModifier = underscoreCase . drop 8} ''TwitterFollower) data SourceType = Atom Feed | Follower TwitterFollower | Keyword TwitterKeyword deriving (Eq, Show) instance ToJSON SourceType where toJSON (Atom feed) = object ["type" .= ("feed" :: Text), "config" .= toJSON feed] toJSON (Follower f) = object ["type" .= ("twitter_user" :: Text), "config" .= toJSON f] toJSON (Keyword kw) = object ["type" .= ("twitter_keyword" :: Text), "config" .= toJSON kw] data Source = Source { sourceId :: !Text , sourceType :: !SourceType , sourceURL :: !Text } deriving (Eq, Show) instance FromJSON Source where parseJSON (Object o) = do sid <- o .: "id" stype <- o .: "type" config <- case (stype :: Text) of "feed" -> Atom <$> o .: "config" "twitter_keyword" -> Keyword <$> o .: "config" "twitter_user" -> Follower <$> o .: "config" _ -> mzero url <- o .: "url" return $ Source sid config url parseJSON _ = mzero newtype Sources = Sources { sources :: [Source] } deriving (Eq, Show) instance FromJSON Sources where parseJSON (Array as) = do sources <- mapM parseJSON $ toList as :: Parser [Source] return $ Sources sources parseJSON _ = mzero -- ----------------------------------------------------------------------------- -- API Calls listSources :: Text -- ^ Organization paramaterized name -> Text -- ^ Flow parameterized name -> RestAPI (Either Error Sources) listSources organization flow = request GET ["flows", organization, flow, "sources"] [] Nothing getSource :: Text -- ^ Organization paramaterized name -> Text -- ^ Flow parameterized name -> Text -- ^ Source ID -> RestAPI (Either Error Source) getSource organization flow sid = request GET ["flows", organization, flow, "source", sid] [] Nothing createSource :: Text -- ^ Organization paramaterized name -> Text -- ^ Flow parameterized name -> SourceType -- ^ The source to create -> RestAPI (Either Error Source) createSource organization flow stype = request POST ["flows", organization, flow, "sources"] [] (Just $ encode stype) deleteSource :: Text -- ^ Organization paramaterized name -> Text -- ^ Flow parameterized name -> Text -- ^ Source ID -> RestAPI (Either Error Success) deleteSource organization flow sid = request DELETE ["flows", organization, flow, "source", sid] [] Nothing