3 Mental Models of APIs

Viewing this by yourself? Hit 's' to see my speaker's notes.

API as...

Persistence layer ("REST"/GraphQL/Graph APIs generally)

Namespace of functions (RPC, ie gRPC)

State machine (REST)

Ok, so, what's the best model?

"Essentially, all models are wrong, but some are useful." — Box, George E. P.; Norman R. Draper (1987). Empirical Model-Building and Response Surfaces, p. 424, Wiley. ISBN 0471810339.

🤔

When building an API, what abstraction level should we expose to the client(s) for this piece of functionality?

Well, what does the client want? Or what do we want the client to want? Or will we have multiple clients, with competing "wants"?

Personal Opinion Alert

Let's put the "Application" back in Application Programming Interface.

Reality: your API is probably gonna do a bit of all 3.

  • If you're serving multiple, potentially thick, clients: you likely want some generic data exposure (like "REST" or GraphQL).
  • If you're providing unsequenced commands clients can choose how to present but you implement (and you can't just release as a local module): you need some RPC.
  • If you're providing sequenced workflows (like, y'know, business processes): you want REST with HATEOAS.

Ok dude. Why do you keep putting REST in scare quotes sometimes?

REST has a maturity model.

😒 So..?

So fully "mature" REST has to obey the hypermedia constraint.

⬇️ 🏭 resource state

✨✨ implicit program state ✨✨

application state 🎮 ⬆️

TL;DR: try state machines instead of entity-relations

etsy: data integrity vs user path

Navigation is based on category.

But what if a product lacks a category? 😬

6

3

hypermedia process

  1. write out all input/output between the client and server
  2. circle inputs and outputs into nodes as appropriate
  3. draw edges between every node where an interaction must happen
  4. label every edge (input/output function) you just created
API specification is in conflict with hypermedia.
application/vnd.siren+json
"name": "item",
"id": 22,
"actions":[
      {
         "name":"add-item",
         "title":"Add Item",
         "method":"POST",
         "href":"http://api.x.io/orders/42/items",
         "type":"application/x-www-form-urlencoded",
         "fields":[
            {
               "name":"orderNumber",
               "type":"hidden",
               "value":"42"
            },
            {
               "name":"productCode",
               "type":"text"
            },
            {
               "name":"quantity",
               "type":"number"
            }
         ]
      }
   ]

affordance

noun af·ford·ance \ə-ˈfȯr-dəns\
the qualities or properties of an object that define its possible uses or make clear how it can or should be used

"We sit or stand on a chair because those affordances are fairly obvious." — Scott Lafee, San Diego Union-Tribune, 15 Aug. 1993

affordance (hypermedia)

the qualities or properties of a representation that define its possible state changing transitions or make clear how it can or should be used to move to other program states

the simultaneous presentation of information and controls such that the information becomes the affordance through which the user (or automaton) obtains choices and selects actions — Roy T. Fielding

item → add-item → purchase: 2 clicks!

"name": "item",
"actions":[
      {
         "name":"add-item",
	}
]
"name": "add-item",
"actions":[
      {
         "name":"purchase",
	}
]

data with affordances > data without affordances

💺 > 🌲

(Humans prefer to sit on chairs.)

The child begins, no doubt, by perceiving the affordances of things for her, for her own personal behavior. She walks and sits and grasps relative to her own legs and body and hands. But she must learn to perceive the affordances of things for other observers as well as for herself. — James J. Gibson, The Ecological Approach to Visual Perception

alex moore - niemi

© 2016