2 people like it.

F# snippet formatting service

Do you want to format F# snippets, but cannot easily run the F# Formatting library as part of your documentation processing? We just added F# Formatting as a service (FAAS!) API to the F# Snippets. This code snippet shows how to call the service.

Making the request

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
open System
open FSharp.Data

// To call the service, send POST request to http://fssnip.net/api/format
let sample = 
  Http.RequestString
    ( "http://fssnip.net/api/format", 
      httpMethod="POST", 
      // Use "Content-Type" header to specify we're using first version of the API
      headers=[HttpRequestHeaders.ContentType "application/vnd.fssnip-v1+json"],
      // Provide a list of snippets (later snippets can use values from earlier)
      // Additional (optional) fields let you specify NuGet packages to reference,
      // prefix to use in generated HTML (to make IDs from multiple requests unique)
      // and disable line numbers (enabled by default)
      body=HttpRequestBody.TextRequest """{ 
        "snippets": [
          "let hello = FSharp.Data.Http.RequestString(\"http://fssnip.net\")",
          "printfn \"Got %d characters\" hello.Length" ],
        "packages": [ "FSharp.Data" ],
        "prefix": "fs",
        "lineNumbers":false
      }""")

Parsing the response

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
// The response from the API is a JSON object with two fields:
//  - snippets is a list with HTML (same length as length of the input collection)
//  - tips is a string with generated tooltips (used by JavaScript)
//
// { "snippets": [
//      "<pre class=\"fssnip highlighted\">...</pre>\r\n",
//      "<pre class=\"fssnip highlighted\">...</pre>\r\n" ]
//   "tips": "<div class=\"tip\" id=\"fs1\">val hello : string (...)" }

type FsSnipResponse = 
  JsonProvider<"""{ "snippets":["html", "more html"], "tips":"divs" }""">

let formatted = FsSnipResponse.Parse(sample)
for snip in formatted.Snippets do printfn "%s" snip
printfn "%s" formatted.Tips

// To use this, you'll also need to add the ".js" and ".css" files from F# Formatting to your site:
// https://github.com/tpetricek/FSharp.Formatting/tree/master/docs/files/content
namespace System
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
val sample : string

Full name: Script.sample
type Http =
  private new : unit -> Http
  static member private AppendQueryToUrl : url:string * query:(string * string) list -> string
  static member AsyncRequest : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?responseEncodingOverride:string * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> Async<HttpResponse>
  static member AsyncRequestStream : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> Async<HttpResponseWithStream>
  static member AsyncRequestString : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?responseEncodingOverride:string * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> Async<string>
  static member private InnerRequest : url:string * toHttpResponse:(string -> int -> string -> string -> string -> 'a0 option -> Map<string,string> -> Map<string,string> -> Stream -> Async<'a1>) * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?responseEncodingOverride:'a0 * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> Async<'a1>
  static member Request : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?responseEncodingOverride:string * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> HttpResponse
  static member RequestStream : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> HttpResponseWithStream
  static member RequestString : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:CookieContainer * ?silentHttpErrors:bool * ?responseEncodingOverride:string * ?customizeHttpRequest:(HttpWebRequest -> HttpWebRequest) -> string

Full name: FSharp.Data.Http
static member Http.RequestString : url:string * ?query:(string * string) list * ?headers:seq<string * string> * ?httpMethod:string * ?body:HttpRequestBody * ?cookies:seq<string * string> * ?cookieContainer:Net.CookieContainer * ?silentHttpErrors:bool * ?responseEncodingOverride:string * ?customizeHttpRequest:(Net.HttpWebRequest -> Net.HttpWebRequest) -> string
module HttpRequestHeaders

from FSharp.Data
val ContentType : contentType:string -> string * string

Full name: FSharp.Data.HttpRequestHeaders.ContentType
type HttpRequestBody =
  | TextRequest of string
  | BinaryUpload of byte []
  | FormValues of seq<string * string>

Full name: FSharp.Data.HttpRequestBody
union case HttpRequestBody.TextRequest: string -> HttpRequestBody
type FsSnipResponse = JsonProvider<...>

Full name: Script.FsSnipResponse
type JsonProvider

Full name: FSharp.Data.JsonProvider


<summary>Typed representation of a JSON document.</summary>
       <param name='Sample'>Location of a JSON sample file or a string containing a sample JSON document.</param>
       <param name='SampleIsList'>If true, sample should be a list of individual samples for the inference.</param>
       <param name='RootName'>The name to be used to the root type. Defaults to `Root`.</param>
       <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param>
       <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param>
       <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
       <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
          (e.g. 'MyCompany.MyAssembly, resource_name.json'). This is useful when exposing types generated by the type provider.</param>
       <param name='InferTypesFromValues'>If true, turns on additional type inference from values.
          (e.g. type inference infers string values such as "123" as ints and values constrained to 0 and 1 as booleans.)</param>
val formatted : JsonProvider<...>.Root

Full name: Script.formatted
JsonProvider<...>.Parse(text: string) : JsonProvider<...>.Root


Parses the specified JSON string
val snip : string
property JsonProvider<...>.Root.Snippets: string []
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property JsonProvider<...>.Root.Tips: string
Raw view Test code New version

More information

Link:http://fssnip.net/7QA
Posted:8 years ago
Author:Tomas Petricek
Tags: f# , formatting