Services Events
Return to Homepage
  • X
    blog post
    Hypermedia Workflow Questions
    This blog post was published more than a year ago. In the fast-moving world of APIs, it is likely that some of the technical details and product specifications discussed will have changed since then. Please note that the API Academy was founded by Layer 7, which was acquired by CA Technologies in April 2013.
    Written by: Mike Amundsen | June 7, 2013 - 11:45 AM

    I fairly often get emails following up on the workshops, articles, webinars and online tutorials I take part in. I can’t always answer these questions directly and sometimes deal with them in blog posts or online articles. Following my recent API Tech Talk on hypermedia, I got some questions from Abiel Woldu on how to handle hypermedia support when the same backend operation is called from different workflows. Here’s part of the Abiel’s email:

    “Say you have an end point for validating address; call it /validateAddress. Now this endpoint is called from two work flows.

    • When a user updates his account settings (changes a new address)
    • When a user tries to buy a product and enters the shipment address

    In both cases the /validateAddress should give different set of links and forms as part of the response of validation (next step affordances) because the flow is different. In this case what is the set of the next links and forms returned from the endpoint? Is it the union of the two workflows and the client knows how to get what it needs? Or does the client send information of which flow it is in and the server uses the information to figure out what response to give it?”

    Decoupling Backend Processes from Public URIs
    This kind of question comes up frequently. Essentially, there are a couple assumptions here that are worth exploring. The first is the idea that a backend operation (e.g. “validateAddress()”) is exposed over HTTP as a single endpoint, no matter the calling context. This is not a requirement. In fact, it is advantageous to decouple public addresses (URI) from private operations on the server. HTTP (whether using HTTP-CRUD, Hypermedia-REST or some other model) offers the advantage of using multiple public URIs to point to the same backend operation. For example, it is perfectly correct to publish both /validateExistingAddress and /validateNewAddress URIs each of which points to the same  “validateAddress()” operation on the server.

    Not Everything Needs a URI
    Just because the backend server has an operation such as “validateAddress()” does not mean there has to be a URI associated with that operation. For example, the “user updates his account settings” workflow need not have a direct URI call to “validateAddress()”. Instead, there could be an account settings resource (/account-settings/) that supports the HTTP.PUT method and accepts a body containing (among other things) a modified address. Executing this client-side operation (PUT /account-settings/) passes data to the server and – along with other operations – the server calls the “validateAddress()” operation itself and reports the results to the client.

    The same can be done in the case of “user tries to buy a product and enters the shipment address”. This address validation could be a small part of the server-side operation and processing of an HTTP.POST to a /check-out/ resource.

    Mapping Actions to URI & Method
    In the HTTP-CRUD model, the focus is on using URIs to identify entities and/or operations and using the protocol methods to perform actions. For example, an /addresses/ resource that supports adding (POST), modifying (PUT), removing (DELETE) and retrieving (GET) addresses associated with a context (logged in user, check-out processing etc.) In this case, POSTing or PUTing a resource body to the server allows the server to call the “validateAddress()” operation (among other things) and report results to the client.

    Mapping Actions to Hypermedia Controls
    In the hypermedia model, actions are described using a hypermedia control such as a link or form. The URI is not important in this model. Instead the control has an identifier (e.g. “validate“), indicates a protocol action (“POST“) and lists state data to include in the payload.

    In Siren it might look like this:

    "actions": [
         "name": "validate",
         "title": "Validate an Address",
         "method": "POST",
         "href": "...",
         "type": "application/x-www-form-urlencoded",
         "fields": [
               { "name" : "Street", "type" : "text", "value" : "123 Main Street" },
               { "name" : "City",   "type" : "text", "value" : "Byteville"},
               { "name" : "State",  "type" : "text", "value" : "MD" },
               { "name" : "ZIP",    "type" : "text", "value" : "12345"}

    Note that I didn’t bother to enter a value for the href in this example. It could be any valid URL; I just left it out.

    Tracking Workflow Progress Within Messages
    Here’s another question from Abiel Woldu’s email:

    “The concept of which work flow the client is going through – is it code that should reside in the API code itself or it’s something that sits outside in some other gateway or something?”

    When implementing processes over HTTP, it’s wise not to rely on stateful multi-request chains. In other words, don’t expect either the client or server to keep track of where some request belongs in a workflow. Instead, include that information in the request and response bodies themselves. This pattern of including all the important context information with each request and response not only assures that the request can be handled independently (e.g. in a load-balanced cluster), it also helps clients and servers to do work within varying time-spans (e.g. a clients can cache the last request to disk and pick things up a day later). In the REST model, Fielding described this as making messages “self-descriptive”.

    For example, there might be a use case that prompts human users to provide quite a lot of information (across various UI tabs) before finally submitting this completed set of work to the server for final validation and processing. One way to support this over HTTP is to allow clients to store “work-in-progress” (WIP) records on the server. As each “tab” (or other UI affordance) is completed, the client app is free to execute a POST or PUT operation with the payload to a URI supplied by the server. The stored data would include a value that indicates how far along in the workflow the user has progressed. This same client app could also recall stored WIP records, inspect the workflow indicator and prompt the user to pick up where she left off. Once all the required elements were supplied, the work could be forwarded for final validation and processing.

    Dynamic Workflow via Hypermedia
    Finally, in some cases, the series of steps in a workflow might vary greatly at runtime. For example, a service might support a multi-tenant model where each instance of “supply all the details for this work” has different steps or the same steps appear in differing order. The “next step” need not be memorized by the client code. Instead, hypermedia servers can inspect the current server-side configuration, check the current progress by the user and then supply the correct “next step” for this particular instance.

    In this way, the client app can support a wide range of workflow details without needing custom code ahead of time (or even downloaded code-on-demand). Instead, the client app only needs to be able to recognize the “next step” link and navigate to that resource.

    In Summary
    In general, when using HTTP:

    • There is no rule that you must expose internal methods as public URIs
    • You may use more than one URI for the same backend operation
    • In the HTTP-CRUD model, you usually map operations by linking URIs and methods
    • In the hypermedia model, you usually map operations by linking controls and state variables
    • It is best to use “self-descriptive” messages to track workflow progress statelessly
    • The hypermedia model supports dynamic workflow progress using the “next step” link pattern

    Thanks to Abiel for his questions and his generous permission for me to use his email and name in this blog post. If you’ve got a question that I haven’t answered online before, feel free to ping me via twitter (@mamund) and fire away.

    Click to get the link