-
Notifications
You must be signed in to change notification settings - Fork 39
Open
Labels
Description
A nice way for doing sanity checking and also for checking what the formatted output of for example the request uri is. Like this, in the Request
module, but fully featured and property-tested:
let asCurl (req : Request) : string =
let cmd _ = Some [ "curl" ]
let printHeaders _ =
Some [ "-i" ]
let method req =
if req.method <> Get then ["-X"; req.method.ToString()] |> Some
else None
let uri (req : Request) =
let ub = UriBuilder req.url
ub.Query <- Impl.getQueryString Encoding.UTF8 req
Some [ ub.Uri.ToString() ]
let form (req : Request) =
match req.body with
| BodyForm form ->
form
|> List.choose (function
| NameValue (name, value) ->
Some (name, value)
| _ ->
None)
|> List.map (fun (name, value) ->
[ "-d"; sprintf "\"%s=%s\"" name value ]
)
|> List.concat
|> Some
| _ ->
None
let basic (req: Request) =
match req.headers |> Map.tryFind "Authorization" with
| None -> None
| Some (Authorization value) when value.StartsWith("Basic") ->
match value.Split(' ') with
| [| kind; base64Value |] -> // horrible hack; not calculating the base64 encoding until last responsible moment is the right fix
let converted = UTF8.toString (Convert.FromBase64String base64Value)
Some [ "-u"; sprintf "\"%s\"" converted ]
| _ ->
None
| _ ->
None
[ cmd
printHeaders
method
uri
form
basic
]
|> List.map (fun f -> f req)
|> List.filter Option.isSome
|> List.map Option.get
|> List.concat
|> String.concat " "