Frank: The Web API DSL


Published on

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.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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<HttpResponseMessage> 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<HttpResponseMessage> 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

    1. 1. FrankThe Web API DSL
    2. 2. Ryan RileyO Logos Bible SoftwareO Community for F#O F# MVPO OSS: FSharpx, Fracture, FrankO @panesofglass
    3. 3. HTTP PartsO Request/Response Lines O Methods, URIs, Status CodesO Headers O General, Request, Response, ContentO ResourcesO RepresentationsO Hypermedia
    4. 4. FrankO F# DSL using System.Net.HttpO Headers compositionO Follows the natural composition of HTTPO Frank Resources == HTTP ResourcesO Define your own conventions!
    5. 5. Function CompositionO Functional programming offers solutions to these problems O Filter O Map O Reduce
    6. 6. Simplest HTTP Application HttpRequestMessage -> HttpResponseMessage
    7. 7. Simplest HTTP Frank Application HttpRequestMessage -> HttpResponseMessageHttpRequestMessage -> Async<HttpResponseMessage>
    8. 8. ResourcesGET /item/1+ POST /item/1+ PUT /item/1+ OPTIONS /item/1=Resource at / with GET, POST, PUT, &OPTIONS
    9. 9. Define a Method Handler// handlerlet echo request = async { let! body = request.Content.AsyncReadAsString() return HttpResponseMessage.ReplyTo(request, body)}// method handlerget echo
    10. 10. Define a Resourcelet helloworld request = async { … }let echo request = async { … }let resource = route “/” (get helloworld <|> post echo)
    11. 11. HTTP Applications/+ /items+ /item/{itemId}+ /help=Typical HTTP “application”
    12. 12. Define an Applicationlet todoListResource = route “/” (get todoList <|> …)let todoItemResource = route “/item/{1}” (put …)let app = merge [ todoListResource; todoItemResource ]
    13. 13. 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
    14. 14. Leverage Connegval negotiateMediaType = formatters -> HttpRequestMessage -> string -> Async<HttpResponseMessage>let echo = negotiateMediaType formatters <| fun request -> request.Content.AsyncReadAsString())
    15. 15. Summary
    16. 16. 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
    17. 17. Thank you!O Please rate this talk: