Skip to content

Instantly share code, notes, and snippets.

@rflechner
Last active April 1, 2017 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rflechner/bcda98d2cc8c3563293498562ef40fb5 to your computer and use it in GitHub Desktop.
Save rflechner/bcda98d2cc8c3563293498562ef40fb5 to your computer and use it in GitHub Desktop.
Example of Suave.Swagger using in a Sample script
(*
In your shell
$ paket init
$ paket add nuget suave.swagger
$ paket install
$ paket generate-include-scripts
$ code .
*)
#load ".paket/load/main.group.fsx"
open Suave
open Suave.Filters
open Suave.Operators
open Suave.Swagger
open Rest
open FunnyDsl
open Swagger
open Suave
open System
[<CLIMutable>]
type SubtractionResult = { Result:int }
and [<CLIMutable>] SubtractionRequest =
{ First:int
Second:int
}
let now : WebPart =
fun (x : HttpContext) ->
async {
// The MODEL helper checks the "Accept" header
// and switches between XML and JSON format
return! MODEL DateTime.Now x
}
let subtractObj =
JsonBody<SubtractionRequest>(fun {First=a;Second=b} -> MODEL {Result=(a-b)})
let api1 =
swagger {
// syntax 1
for route in getting (simpleUrl "/time" |> thenReturns now) do
yield description Of route is "What time is it ?"
for route in posting <| simpleUrl "/subtract" |> thenReturns subtractObj do
yield description Of route is "Subtracts two numbers"
yield route |> addResponse 200 "Subtraction result" (Some typeof<SubtractionResult>)
yield parameter "subtraction request" Of route (fun p -> { p with Type = (Some typeof<SubtractionRequest>); In=Body })
yield route |> tag "maths"
}
startWebServer defaultConfig api1.App
// goto http://localhost:8080/swagger/v2/ui/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment