frank: the web api dsl

17
Frank The Web API DSL

Upload: ryan-riley

Post on 04-Jul-2015

1.219 views

Category:

Technology


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

Page 1: Frank: The Web API DSL

FrankThe Web API DSL

Page 2: Frank: The Web API DSL

Ryan RileyO Logos Bible Software

O Community for F#

O F# MVP

O OSS: FSharpx, Fracture, Frank

O @panesofglass

Page 3: Frank: The Web API DSL

HTTP PartsO Request/Response Lines

O Methods, URIs, Status Codes

O Headers

O General, Request, Response, Content

O Resources

O Representations

O Hypermedia

Page 4: Frank: The Web API DSL

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!

Page 5: Frank: The Web API DSL

Function CompositionO Functional programming offers solutions

to these problems

O Filter

O Map

O Reduce

Page 6: Frank: The Web API DSL

Simplest HTTP Application

HttpRequestMessage -> HttpResponseMessage

Page 7: Frank: The Web API DSL

Simplest HTTP Frank Application

HttpRequestMessage -> HttpResponseMessage

HttpRequestMessage -> Async<HttpResponseMessage>

Page 8: Frank: The Web API DSL

ResourcesGET /item/1

+ POST /item/1

+ PUT /item/1

+ OPTIONS /item/1

=

Resource at / with GET, POST, PUT, &

OPTIONS

Page 9: Frank: The Web API DSL

Define a Method Handler

// handler

let echo request = async {

let! body = request.Content.AsyncReadAsString()

return HttpResponseMessage.ReplyTo(request, body)

}

// method handler

get echo

Page 10: Frank: The Web API DSL

Define a Resource

let helloworld request = async { … }

let echo request = async { … }

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

Page 11: Frank: The Web API DSL

HTTP Applications/

+ /items

+ /item/{itemId}

+ /help

=

Typical HTTP “application”

Page 12: Frank: The Web API DSL

Define an Application

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

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

let app = merge [ todoListResource; todoItemResource ]

Page 13: Frank: The Web API DSL

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

Page 14: Frank: The Web API DSL

Leverage Conneg

val negotiateMediaType = formatters ->

HttpRequestMessage ->

string ->

Async<HttpResponseMessage>

let echo = negotiateMediaType formatters

<| fun request ->

request.Content.AsyncReadAsString())

Page 15: Frank: The Web API DSL

Summary