Frank: The Web API DSL
Upcoming SlideShare
Loading in...5
×
 

Frank: The Web API DSL

on

  • 1,490 views

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 ...

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.

Statistics

Views

Total Views
1,490
Slideshare-icon Views on SlideShare
1,488
Embed Views
2

Actions

Likes
1
Downloads
13
Comments
0

1 Embed 2

http://speakerrate.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • HTTP defines several components that allow distributed, loosely-coupled agents to communicate. The first lines of requests and responses are standardized to ensure that loosely-coupled components can understand one another, yet they allow for extension either by standards bodies or for internal, proprietary use.
  • For those with a stronger bent towards building web sites, Mauricio Scheffer’s Figment may be a closer match to your taste.
  • Routing is nothing more than filtering down a set of handlers to select the appropriate one and return a response.A Response is nothing more than a map function over a request, though it is true that some HTTP methods are destructive (e.g. POST).Pulling handlers into resources or applications is nothing more than a reduce function. Frank uses merge; WebSharper uses Sum.
  • Compare the Frank application signature with the simplest app signature we used above. The only addition is that we are stating that an application is non-blocking. This is the same idea as is used in node.js. It’s also sort-of required since Frank sits on System.Net.Http via MessageHandlers. The signature for the MessageHandler.SendAsync is the same but for Task rather than Async. This signature extends all the way through all layers of composition.
  • This may be new to some, but this resource is not the same as the collection of items at /items.
  • A resource is the only current location that does not conform solely to the Frank application signature. A resource holds both a Uri Template (its name) as well as a HttpRequestMessage -> Async option. Why the option? The resource will respond with a 405 Method Not Allowed if it does not respond to the method thrown its way. Thus the option allows the programmer to define the methods for the resource without having to manually hook up the 405 response.
  • The aggregate of many resources is generally what we refer to as an HTTP, or Web, Application. A single resource is enough, as is a single method.
  • Similar to resource, merge is a function of HttpRequestMessage -> Async option. When a resource uri is not found, a 404 Not Found response is returned. The result, however, is a Frank application.
  • Resources may be directly returned in a response. In the beginning, when most HTTP applications served static files, this was common. However, Resources are not equivalent to the representation returned to the client, even if they appear identical. Unlike editing files locally, if you were to edit a file returned from an HTTP application, you would not be editing the original file. You would need to re-submit the file to the server, if allowed, and then the server would need to take the content you supplied and update the original Resource.It is thus reasonable to understand that a Resource can be represented in more than one way. A JSON representation is just as valid as XML, HTML, or even an image.Content negotiation, or “conneg,” is a cross-cutting concern that allows HTTP applications to select an appropriate representation to send in the response message. The actual execution of this selection could occur either at the top-level or at the lowest-level of a composed HTTP application.
  • Frank currently uses Mauricio Scheffer’sFsConneg library, now in FSharpx.Http. System.Net.Http has a similar mechanism, but it’s currently still a little heavy as they work out the kinks in preparation for the RC. FsConneg provides a mechanism for submitting the Accept headers and a list of supported formats and returns the best match. negotiateMediaType wraps that with the selection of the appropriate formatter, of type System.Net.Http.Formatting.MediaTypeFormatter, and returns the formatted content.
  • Using F# for web projects isn’t just about code size. Yes, you’ll save some lines of code, but more importantly, you’ll work closer to the guts of HTTP. You can leverage the power of composition to build applications that will be more maintainable and much clearer to understand.

Frank: The Web API DSL Frank: The Web API DSL Presentation Transcript

  • FrankThe Web API DSL
  • Ryan RileyO Logos Bible SoftwareO Community for F#O F# MVPO OSS: FSharpx, Fracture, FrankO @panesofglass
  • HTTP PartsO Request/Response Lines O Methods, URIs, Status CodesO Headers O General, Request, Response, ContentO ResourcesO RepresentationsO Hypermedia
  • FrankO F# DSL using System.Net.HttpO Headers compositionO Follows the natural composition of HTTPO Frank Resources == HTTP ResourcesO 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 -> HttpResponseMessageHttpRequestMessage -> 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// handlerlet echo request = async { let! body = request.Content.AsyncReadAsString() return HttpResponseMessage.ReplyTo(request, body)}// method handlerget echo
  • Define a Resourcelet 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 Applicationlet todoListResource = route “/” (get todoList <|> …)let todoItemResource = route “/item/{1}” (put …)let app = merge [ todoListResource; todoItemResource ]
  • Content NegotiationClient Accepts Server Supportsapplication/xml;q=0.9 application/jsonapplication/json;q=0.8 text/plaintext/plain;q=0.5 text/html application/json
  • Leverage Connegval negotiateMediaType = formatters -> HttpRequestMessage -> string -> Async<HttpResponseMessage>let echo = negotiateMediaType formatters <| fun request -> request.Content.AsyncReadAsString())
  • Summary
  • ResourcesO ASP.NET Web Forms in F#O ASP.NET MVC in F#O Figment DSL for MVCO Frank DSL for Web APIO WebSharperO Pit FW
  • Thank you!O Please rate this talk: http://speakerrate.com/talks/9334-frank-the-web-api-dsl