module MediaWiki.API.Action.Login.Import where
import MediaWiki.API.Types
import MediaWiki.API.Utils
import MediaWiki.API.Action.Login
import Text.XML.Light.Types
import Control.Monad
import Data.Maybe
stringXml :: String -> Either (String,[String]) LoginResponse
stringXml s = parseDoc xml s
xml :: Element -> Maybe LoginResponse
xml e = do
guard (elName e == nsName "api")
let es1 = children e
p <- pNode "login" es1
let res = pAttr "result" p
let uid = pAttr "lguserid" p
let unm = pAttr "lgusername" p
let tok = pAttr "lgtoken" p
let coo = pAttr "cookieprefix" p
let ses = pAttr "sessionid" p
case res of
Nothing -> fail "missing 'result' api attribute"
Just "Success" ->
return LoginResponse{ lgSuccess=True
, lgSession=UserSession
{ sessUserId = fromMaybe "" uid
, sessUserName = fromMaybe "" unm
, sessPassword = Nothing
, sessCookiePrefix = coo
, sessSessionId = ses
, sessToken = fromMaybe "" tok
}}
Just x -> do
let det = pAttr "details" p
let wai = pAttr "wait" p
return LoginError
{ lgSuccess = False
, lgeError = x
, lgeDetails = det
, lgeWait = fromMaybe "" wai
}