{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
module OpenTok.Session
( Session
, SessionOptions(_mediaMode, _archiveMode, _location)
, MediaMode(Relayed, Routed)
, create
, sessionOpts
)
where
import Prelude ( )
import Prelude.Compat
import Data.Aeson
import Data.Aeson.Casing ( snakeCase )
import Data.Aeson.Types
import Data.Aeson.TH
import Data.Data
import Data.Strings ( strToLower )
import GHC.Generics
import OpenTok.Client
import OpenTok.Types
data MediaMode = Relayed | Routed deriving (Data, Generic, Typeable)
instance Show MediaMode where
show = strToLower . showConstr . toConstr
deriveJSON defaultOptions { constructorTagModifier = strToLower } ''MediaMode
data ArchiveMode = Manual | Always deriving (Data, Generic, Typeable)
instance Show ArchiveMode where
show = strToLower . showConstr . toConstr
deriveJSON defaultOptions { constructorTagModifier = strToLower } ''ArchiveMode
data SessionOptions = SessionOptions {
_mediaMode :: MediaMode,
_archiveMode :: ArchiveMode,
_location :: Maybe IPAddress
} deriving(Show, Generic)
instance ToJSON SessionOptions where
toJSON = genericToJSON defaultOptions
{ omitNothingFields = True, fieldLabelModifier = drop 1 }
sessionOpts :: SessionOptions
sessionOpts = SessionOptions Relayed Manual Nothing
data Session = Session {
apiKey :: String,
sessionId :: String,
mediaMode :: MediaMode,
archiveMode :: ArchiveMode
} deriving (Show)
data SessionProperties = SessionProperties {
_sessionId :: String,
_projectId :: String,
_createDt :: String,
_mediaServerUrl :: Maybe String
} deriving (Generic, Show)
instance FromJSON SessionProperties where
parseJSON = genericParseJSON $ defaultOptions { fieldLabelModifier = snakeCase . drop 1 }
fromProps :: SessionOptions -> SessionProperties -> Session
fromProps opts props = Session
{ apiKey = _projectId props
, sessionId = _sessionId props
, mediaMode = _mediaMode opts
, archiveMode = _archiveMode opts
}
create :: Client -> SessionOptions -> IO (Either OTError Session)
create client opts = do
response <- postWithBody client "/session/create/" (Just opts) :: IO (Either ClientError [SessionProperties])
case response of
Right propsArray -> pure $ Right $ (fromProps opts . head) propsArray
Left e -> pure $ Left $ message e