Frank: The Web API DSL

Post on 04-Jul-2015

1.218 views 0 download

description

Most .NET web applications today use ASP.NET WebForms or MVC. However, several F# libraries offer new ways to build web APIs and applications help reduce code and offer better abstractions, especially for single-page applications (SPA). How can F# improve the situation? Frank is a minimal, domain-specific library designed to leverage the power of HTTP using functions directly.

Transcript of Frank: The Web API DSL

FrankThe Web API DSL

Ryan RileyO Logos Bible Software

O Community for F#

O F# MVP

O OSS: FSharpx, Fracture, Frank

O @panesofglass

HTTP PartsO Request/Response Lines

O Methods, URIs, Status Codes

O Headers

O General, Request, Response, Content

O Resources

O Representations

O Hypermedia

FrankO F# DSL using System.Net.Http

O Headers composition

O Follows the natural composition of HTTP

O Frank Resources == HTTP Resources

O Define your own conventions!

Function CompositionO Functional programming offers solutions

to these problems

O Filter

O Map

O Reduce

Simplest HTTP Application

HttpRequestMessage -> HttpResponseMessage

Simplest HTTP Frank Application

HttpRequestMessage -> HttpResponseMessage

HttpRequestMessage -> Async<HttpResponseMessage>

ResourcesGET /item/1

+ POST /item/1

+ PUT /item/1

+ OPTIONS /item/1

=

Resource at / with GET, POST, PUT, &

OPTIONS

Define a Method Handler

// handler

let echo request = async {

let! body = request.Content.AsyncReadAsString()

return HttpResponseMessage.ReplyTo(request, body)

}

// method handler

get echo

Define a Resource

let helloworld request = async { … }

let echo request = async { … }

let resource = route “/” (get helloworld <|> post echo)

HTTP Applications/

+ /items

+ /item/{itemId}

+ /help

=

Typical HTTP “application”

Define an Application

let todoListResource = route “/” (get todoList <|> …)

let todoItemResource = route “/item/{1}” (put …)

let app = merge [ todoListResource; todoItemResource ]

Content Negotiation

Client Accepts Server Supports

application/xml;q=0.9

application/json;q=0.8

text/plain;q=0.5

application/json

text/plain

text/html

application/json

Leverage Conneg

val negotiateMediaType = formatters ->

HttpRequestMessage ->

string ->

Async<HttpResponseMessage>

let echo = negotiateMediaType formatters

<| fun request ->

request.Content.AsyncReadAsString())

Summary