f# – 在Suave中聚合来自请求的信息
发布时间:2020-12-14 04:47:32 所属栏目:百科 来源:网络整理
导读:我正在使用Suave构建经过身份验证的Web API,我经常偶然发现在不同功能中聚合信息的问题 pathScan Navigation.playersAvailables GetGame = getInSession = (fun (gameId,playerInSession) - //access to both gameId and player in session) 签名: getGame
我正在使用Suave构建经过身份验证的Web API,我经常偶然发现在不同功能中聚合信息的问题
pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session) 签名: getGame : HttpContext -> Async<HttpContext option> getInSession : HttpContext -> Async<HttpContext option> > getGame从httpContext.request.querystring getInSession获取id 我发现这样做的唯一方法是在userDataDictionnary中存储信息: Writers.setUserData "player" { playerId= playersId; socialId=socialId; username = username} 并在其他功能中检索它,但它看起来非常讨厌我: let player = x.userState.["player"] :?> PlayerSession //do some other stuff now that we have the current player 还有另一种方法吗?我想拥有像这样的纯粹功能 pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session) pathScan Navigation.otherRoute GetGame >>= (fun (gameId) -> //process gameId) pathScan Navigation.otherRoute2 getInSession >>= (fun (sessionId) -> //process sessionId to do some other stuff) 我担心我需要的是与一些真正的函数程序员进行一天的谈话.. 解决方法
setUserData是一个纯函数–
src.
不确定this是否仍然是最新的,但它表示pathScan和>> =不能很好地链接.但是我认为您正在使用的Writers.setUserData可能能够实现它. 进入一个物品袋以拉出东西并不可爱. 怎么样: let (|ParseInt|_|) = function | "" | null -> None | x -> match Int32.TryParse x with | true,i -> Some i | _ -> None let (|HasParam|_|) name (ctx:HttpContext) = ctx.request.queryParam name |> function |Choice1Of2 value -> Some value | _ -> None let playersAvailablePart:WebPart = function //access to both gameId and player in session |HasParam "inSession" playerInSession & HasParam "gameId" gameId as ctx -> // do your processing here,sample return: OK "we had all the required important parts" ctx // or an example of something more strongly typed | HasParam "inSession" (ParseInt playerInSession) & HasParam "gameId" (ParseInt gameId) as ctx -> // do your processing here,sample return: OK "we had all the required important parts" ctx | ctx -> never ctx 如果值不在queryParameters中,则这不完全有效,但您可以将其调整到它们所在的位置 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |