{-# LANGUAGE OverloadedStrings #-}
module Network.Greskell.WebSocket.Request.Common
( Operation(..),
SASLMechanism(..),
Base64(..)
) where
import Control.Applicative (empty)
import Data.Aeson (ToJSON(..), FromJSON(..), Object, Value(String))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64 as B64
import Data.Text (unpack, Text)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
class Operation o where
opProcessor :: o -> Text
opName :: o -> Text
opArgs :: o -> Object
instance (Operation a, Operation b) => Operation (Either a b) where
opProcessor e = either opProcessor opProcessor e
opName e = either opName opName e
opArgs e = either opArgs opArgs e
data SASLMechanism = SASLPlain
| SASLGSSAPI
deriving (Show,Eq,Ord,Enum,Bounded)
instance ToJSON SASLMechanism where
toJSON = toJSON . toText
where
toText :: SASLMechanism -> Text
toText SASLPlain = "PLAIN"
toText SASLGSSAPI = "GSSAPI"
instance FromJSON SASLMechanism where
parseJSON (String s) = case s of
"PLAIN" -> return SASLPlain
"GSSAPI" -> return SASLGSSAPI
_ -> fail ("Unknown SASLMechanism: " ++ unpack s)
parseJSON _ = empty
newtype Base64 = Base64 { unByte64 :: ByteString }
deriving (Show,Eq,Ord)
instance ToJSON Base64 where
toJSON (Base64 bs) = toJSON $ decodeUtf8 $ B64.encode bs
instance FromJSON Base64 where
parseJSON (String t) = either fail (return . Base64) $ B64.decode $ encodeUtf8 t
parseJSON _ = empty