首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套Suave WebPart

嵌套Suave WebPart
EN

Stack Overflow用户
提问于 2017-07-19 12:12:42
回答 1查看 112关注 0票数 2

我第一次在Suave周围玩,很明显有些东西我不明白。我想要实现的是实现一个简单的Rest:

  • 用户可以获得有关金融工具的信息。
  • 此外,每种仪器都有一个价格清单。

现在,为了简单起见,我只关注GET方法。

我的非常基本代码如下所示:

代码语言:javascript
复制
[<AutoOpen>]
module RestFul =    

    let JSON v =     
        let jsonSerializerSettings = new JsonSerializerSettings()
        jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()

        JsonConvert.SerializeObject(v, jsonSerializerSettings)
        |> OK 
        >=> Writers.setMimeType "application/json; charset=utf-8"

    let fromJson<'a> json =
        JsonConvert.DeserializeObject(json, typeof<'a>) :?> 'a    

    let getResourceFromReq<'a> (req : HttpRequest) = 
        let getString rawForm = System.Text.Encoding.UTF8.GetString(rawForm)
        req.rawForm |> getString |> fromJson<'a>

    type RestResource<'a> = {
        GetById : int -> 'a option
        GetPricesById : int -> 'a option
    }

    let rest resource =

        let handleResource requestError = function
            | Some r -> r |> JSON
            | _ -> requestError

        let getResourceById = 
            resource.GetById >> handleResource (NOT_FOUND "Resource not found")

        let getPricesById = 
            resource.GetPricesById >> handleResource (NOT_FOUND "Resource not found")

        choose [
            GET >=> pathScan "/instrument/%d" getResourceById
            GET >=> pathScan "/instrument/%d/prices" getPricesById
        ]


module Main =
    [<EntryPoint>]
    let main argv = 

        let webPart = rest {
                GetById = fun i -> Some i // placeholder
                GetPricesById = fun i -> Some i // placeholder, it'll be a list eventually
            }

        startWebServer defaultConfig webPart
        0

当我以这种方式定义WebPart时:

代码语言:javascript
复制
choose [
    GET >=> pathScan "/instrument/%d" getResourceById // Returns the instrument static data
    GET >=> pathScan "/instrument/%d/prices" getPricesById // Returns price list for the instrument
]

那一切都很好。我想知道是否有一种方法来嵌套这些way部件,例如:

代码语言:javascript
复制
// My idea about the code - doesn't compile
choose [
    pathScan "/instrument/%d" getResourceById >=> choose [
        GET // Returns the instrument static data
        GET >=> path "/prices" >=> (somehow refer to the previous id and get prices)  // Returns price list for the instrument
    ]
]

此外,由于我正在学习RestAPIs,我的推理中可能会有一个空白。我认为以这种方式嵌套价格端点可以简单地说明价格被认为是一种工具的属性(如果我错了,请随时纠正我)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-19 12:59:52

对,所以访问之前的请求有点不礼貌;)我们希望无论刚刚发生了什么,事情都能独立地发生。因此,解决这一问题的一个更好的思路或许就是在这条道路的尽头附加价格?

代码语言:javascript
复制
choose [
    GET >=> pathScan "/instrument/%d" getResourceById 
    GET >=> pathScan "/instrument/%d/prices" getPricesById
]
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45190215

复制
相关文章

相似问题

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