module Aws.Sns.Commands.ListSubscriptionsByTopic
( Subscription(..)
, ListSubscriptionsByTopicNextToken
, ListSubscriptionsByTopic(..)
, ListSubscriptionsByTopicResponse(..)
, ListSubscriptionsByTopicErrors(..)
) where
import Aws.Core
import Aws.General
import Aws.Sns.Core
import Control.Applicative
import Control.Monad
import Control.Monad.Trans.Resource (throwM)
import Data.Maybe
import Data.Monoid
import Data.String
import qualified Data.Text as T
import qualified Data.Traversable as TR
import Data.Typeable
import Text.XML.Cursor (($//), ($/), (&/), (&|))
import qualified Text.XML.Cursor as CU
data Subscription = Subscription
{ subscriptionEndpoint :: !(Maybe SnsEndpoint)
, subscriptionOwner :: !(Maybe T.Text)
, subscriptionProtocol :: !(Maybe SnsProtocol)
, subscriptionSubscriptionArn :: !(Maybe Arn)
, subscriptionTopicArn :: !(Maybe Arn)
}
deriving (Show, Read, Eq, Ord)
subscriptionParseXml :: CU.Cursor -> Either String Subscription
subscriptionParseXml el = Subscription
<$> (pure . listToMaybe $ el $/ CU.laxElement "Endpoint" &/ CU.content)
<*> (pure . listToMaybe $ el $/ CU.laxElement "Owner" &/ CU.content)
<*> (TR.mapM fromText . listToMaybe $ el $/ CU.laxElement "Protocol" &/ CU.content)
<*> (subArnFromText . listToMaybe $ el $/ CU.laxElement "SubscriptionArn" &/ CU.content)
<*> (TR.mapM fromText . listToMaybe $ el $/ CU.laxElement "TopicArn" &/ CU.content)
where
subArnFromText :: Maybe T.Text -> Either String (Maybe Arn)
subArnFromText (Just "PendingConfirmation") = Right Nothing
subArnFromText t = TR.mapM fromText t
listSubscriptionsByTopicAction :: SnsAction
listSubscriptionsByTopicAction = SnsActionListSubscriptionsByTopic
newtype ListSubscriptionsByTopicNextToken = ListSubscriptionsByTopicNextToken
{ listSubscriptionsByTopicNextTokenText :: T.Text
}
deriving (Show, Read, Eq, Ord, Monoid, IsString)
data ListSubscriptionsByTopic = ListSubscriptionsByTopic
{ listSubscriptionsByTopicNextToken :: !(Maybe ListSubscriptionsByTopicNextToken)
, listSubscriptionsByTopicArn :: !Arn
}
deriving (Show, Read, Eq, Ord, Typeable)
data ListSubscriptionsByTopicResponse = ListSubscriptionsByTopicResponse
{ listSubscriptionsByTopicResponseNextToken :: !(Maybe ListSubscriptionsByTopicNextToken)
, listSubscriptionsByTopicResponseSubscriptions :: ![Subscription]
}
deriving (Show, Read, Eq, Ord, Typeable)
instance ResponseConsumer r ListSubscriptionsByTopicResponse where
type ResponseMetadata ListSubscriptionsByTopicResponse = SnsMetadata
responseConsumer _ = snsXmlResponseConsumer p
where
p el = ListSubscriptionsByTopicResponse (nextToken el) <$> subs el
subs el = do
let t = el
$// CU.laxElement "ListSubscriptionsByTopicResult"
&/ CU.laxElement "Subscriptions"
&/ CU.laxElement "member"
&| subscriptionParseXml
forM t $ \i -> case i of
Right a -> return a
Left e -> throwM $ SnsResponseDecodeError
$ "failed to parse subscriptions: " <> T.pack e
nextToken el = fmap ListSubscriptionsByTopicNextToken . listToMaybe $ el
$// CU.laxElement "ListSubscriptionsByTopicResult"
&/ CU.laxElement "NextToken"
&/ CU.content
instance SignQuery ListSubscriptionsByTopic where
type ServiceConfiguration ListSubscriptionsByTopic = SnsConfiguration
signQuery ListSubscriptionsByTopic{..} = snsSignQuery SnsQuery
{ snsQueryMethod = Get
, snsQueryAction = listSubscriptionsByTopicAction
, snsQueryParameters = nextToken <>
[ ("TopicArn", Just $ toText listSubscriptionsByTopicArn)
]
, snsQueryBody = Nothing
}
where
nextToken = case listSubscriptionsByTopicNextToken of
Nothing -> []
Just _ -> [("NextToken", tokParam)]
tokParam = listSubscriptionsByTopicNextTokenText <$> listSubscriptionsByTopicNextToken
instance Transaction ListSubscriptionsByTopic ListSubscriptionsByTopicResponse
instance AsMemoryResponse ListSubscriptionsByTopicResponse where
type MemoryResponse ListSubscriptionsByTopicResponse = ListSubscriptionsByTopicResponse
loadToMemory = return
instance ListResponse ListSubscriptionsByTopicResponse Subscription where
listResponse (ListSubscriptionsByTopicResponse _ subs) = subs
instance IteratedTransaction ListSubscriptionsByTopic ListSubscriptionsByTopicResponse where
nextIteratedRequest query ListSubscriptionsByTopicResponse{..} =
query
{ listSubscriptionsByTopicNextToken = listSubscriptionsByTopicResponseNextToken
}
<$ listSubscriptionsByTopicResponseNextToken
data ListSubscriptionsByTopicErrors
= ListSubscriptionsByTopicAuthorizationError
| ListSubscriptionsByTopicInternalError
| ListSubscriptionsByTopicInvalidParameter
| ListSubscriptionsByTopicNotFound
deriving (Show, Read, Eq, Ord, Enum, Bounded, Typeable)