我一直在玩第一次Suave,顯然有一些我不明白。 我試圖做到的,是實現一個簡單的REST API:嵌套和藹WebPart
- 用戶可以獲得有關金融工具的信息
- 此外,每個儀器具有
價格的列表現在簡單我只關注GET方法。
我非常的代碼基礎件是在這裏:
[<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:
choose [
GET >=> pathScan "/instrument/%d" getResourceById // Returns the instrument static data
GET >=> pathScan "/instrument/%d/prices" getPricesById // Returns price list for the instrument
]
然後一切工作正常。我想知道是否有辦法嵌套webparts,例如像這樣:
// 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時,我的推理可能存在差距。我認爲,嵌套價格終點就是這樣明確表示價格被認爲是工具的一個屬性(如果我錯了,隨時糾正我)。
這不是它的工作原理。你必須放棄「當前請求」的概念。 Suave沒有這樣的事情。這都是功能組成。 –