-- | Runs a generic action
runAction
    :: (String -> NsRestletConfig -> IO RestletResponse) -- Restlet runner
    -> NsRestletConfig -- ^ Restlet configuration
    -> (NsRestletCode -> NsAction) -- ^ Partial action that requires
                                   -- restlet code
    -> IO (Either RestletError Value)
runAction runner cfg act = do
    code   <- restletCode
    let actJSON = reqJSON . act . NsRestletCode $ code
    result <- runner actJSON cfg
    return $ case result of
        RestletErrorResp{} -> Left $ interpretError result
        _                  -> Right $ responseToAeson result
  where
    reqJSON = bytesToString . BSL.unpack . encode
    restletCode = getDataFileName "Support/Restlet.js" >>= TextIO.readFile
runAction
    :: (String -> NsRestletConfig -> IO RestletResponse) -- Restlet runner
    -> NsRestletConfig -- ^ Restlet configuration
    -> (NsRestletCode -> NsAction) -- ^ Partial action that requires
                                   -- restlet code
    -> IO (Either RestletError Value)
runAction runner cfg act = do
    code   <- restletCode
    let actJSON = reqJSON . act . NsRestletCode $ code
    result <- runner actJSON cfg
    return $ case result of
        RestletErrorResp{} -> Left $ interpretError result
        _                  -> Right $ responseToAeson result
  where
    reqJSON = bytesToString . BSL.unpack . encode
    restletCode = getDataFileName "Support/Restlet.js" >>= TextIO.readFile
runAction runner cfg act = do
    code   <- restletCode
    let actJSON = reqJSON . act . NsRestletCode $ code
    result <- runner actJSON cfg
    return $ case result of
        RestletErrorResp{} -> Left $ interpretError result
        _                  -> Right $ responseToAeson result
  where
    reqJSON = bytesToString . BSL.unpack . encode
    restletCode = getDataFileName "Support/Restlet.js" >>= TextIO.readFile