frank-fs / taliesin

OWIN routing middleware using F# Agents
Apache License 2.0
9 stars 3 forks source link

Share `RouteSpec` with client and server #4

Open panesofglass opened 10 years ago

panesofglass commented 10 years ago

The current ResourceManager is aimed at managing server-side resources. However, the provided RouteSpec could be shared just as other forms of API documentation are shared. The simplest method might be to either pass in an HttpClient instance or have the ResourceManager create and manage its own instance of HttpClient. In either case, it should pass this instance along to the Resource, and the Resource should expose a Send or Request member. The ResourceManager could then double as an HTTP client and server API.

// Create a ResourceManager
let resourceManager = ResourceManager<Resources>()
// Start the manager with the provided spec
use sub = resourceManager.Start(spec)
let responseBody =
    async {
        // Invoke the ResourceManager as a client
        let! response = resourceManager.[Home].[GET]()
        // Or with a POST:
        //let! response = resourceManager.[Echo].[POST](new MemoryStream())
        // Ideally would provide a mapping to expected type rather than raw `System.Net.Http.HttpResponseMessage`.
        let! body = Async.AwaitTask <| response.Content.ReadAsStreamAsync()
        return body
    }
    |> Async.RunSynchronously