首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Suave请求中聚合信息

从Suave请求中聚合信息
EN

Stack Overflow用户
提问于 2016-03-06 09:36:22
回答 1查看 227关注 0票数 4

我正在使用Suave构建一个经过身份验证的web API,而且我经常遇到通过不同功能聚合信息的问题。

代码语言:javascript
复制
pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session)

签名:

代码语言:javascript
复制
getGame : HttpContext -> Async<HttpContext option>
getInSession : HttpContext -> Async<HttpContext option> 
  • getGame从httpContext.request.querystring getInSession获取id
  • 从sessionId到httpContext.cookie

为了做到这一点,我发现的唯一方法就是在userDataDictionnary中存储信息:

代码语言:javascript
复制
Writers.setUserData "player"  { playerId= playersId; socialId=socialId; username = username}

并在另一个函数中检索它,但在我看来,它非常讨厌:

代码语言:javascript
复制
let player = x.userState.["player"] :?> PlayerSession
//do some other stuff now that we have the current player

还有别的办法吗?我想拥有像getGameId和get会话之类的纯函数。当我想要处理我的不同路线时,我能写出它们:

代码语言:javascript
复制
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)

恐怕我需要的是和一个真正的功能程序员谈一天。

EN

回答 1

Stack Overflow用户

发布于 2019-02-19 15:37:08

setUserData是一个纯函数-- src

不确定是否仍然是当前的,但它说pathScan>>=不能很好地链接在一起。然而,我认为您正在使用的Writers.setUserData可能能够完成它。

拿着一个物品袋把东西拿出来不是很好。

不如:

代码语言:javascript
复制
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中,则这并不完全有效,但您可以将其调整到它们所在的位置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35825290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档