A REST toolkit for OCaml
Module Webmachine . Make


module IO : IO
module Clock : CLOCK


type +'a io = 'a IO.t
type 'a result =
| Ok of 'a
| Error of int
type ('a, 'body) op = 'body Rd.t -> ('a result * 'body Rd.t) io
type 'body provider = ( 'body, 'body ) op
type 'body acceptor = ( bool, 'body ) op
type www_authenticate = {
scheme : string;
realm : string;
params : (string * string) list;
type auth = [
| `Authorized
| `Basic of string
| `Challenge of www_authenticate
| `Redirect of Uri.t
val continue : 'a -> ( 'a, 'body ) op

continue a rd is equivalent to IO.return (Ok x, rd)

val respond : ?body:'body -> int -> ( 'a, 'body ) op

respond ?body n rd is equivalent to IO.return (Error n, { rd with resp_body = body }

class virtual 'body resource : object ... end

The resource parent class.

val to_handler : ?dispatch_path:string -> ?path_info:(string * string) list -> resource:'body resource -> body:'body -> request:Cohttp.Request.t -> unit -> (Cohttp.Code.status_code * Cohttp.Header.t * 'body * string list) io

to_handler ~resource ~body ~request () runs the resource through the HTTP decision diagram given body and request. The result is a tuple that contains the status code, headers and body of the response. The final element of the tuple is a list of decision diagram node names that is useful for debugging.

val dispatch : ((Dispatch.tag * string) list * Dispatch.typ * ( unit -> 'body resource )) list -> body:'body -> request:Cohttp.Request.t -> (Cohttp.Code.status_code * Cohttp.Header.t * 'body * string list) option io

dispatch routes returns a request handler that will iterate through routes and dispatch the request to the first resources that matches the URI path. The form that the individal route entries takes this the following:

(pattern, exact, resource_constructor)

The pattern itself is a list of literal (`Lit) or variable matches (`Var) that the URI path should satify. For example, a route entry that will be associated with a particular user in the system would look like this:

([`Lit, "user"; `Var, "id"], `Exact, user_resource)

This would match a URI path such as "/user/10" but would not match a URI such as "/usr/10/preferences", since the exact component of the route tuple is `Exact.

val dispatch' : (string * ( unit -> 'body resource )) list -> body:'body -> request:Cohttp.Request.t -> (Cohttp.Code.status_code * Cohttp.Header.t * 'body * string list) option io

dispatch' routes ~body ~request works in the same way as dispatch' except the user can specify path patterns using a string shorthand. For example, the following route entry:

("/user/:id/*", user_resource)

translates to:

([`Lit, "user"; `Var "id"], `Prefix, user_resource)