teach a dog to rest

73
Teach a Dog to REST Brian Mulloy @landlessness Apigee @apigee

Upload: landlessness

Post on 08-Sep-2014

136.096 views

Category:

Technology


16 download

DESCRIPTION

this is an old version. new version here: http://blog.apigee.com/detail/slides_for_restful_api_design_second_edition_webinar/ It's been 10 years since Fielding first defined REST. So, where are all the elegant REST APIs? While many claim REST has arrived, many APIs in the wild exhibit arbitrary, productivity-killing deviations from true REST. We'll start with a typical poorly-designed API and iterate it into a well-behaved RESTful API.

TRANSCRIPT

Page 1: Teach a Dog to REST

Teach a Dog to REST

Brian Mulloy@landlessness

Apigee@apigee

Page 2: Teach a Dog to REST

20%

Page 3: Teach a Dog to REST

Application developers are kingmakers.

Page 4: Teach a Dog to REST

Make your API easy to learn.

Page 5: Teach a Dog to REST

Good APIs are a design problem.

Page 6: Teach a Dog to REST
Page 7: Teach a Dog to REST
Page 8: Teach a Dog to REST

Idempotent methods

Page 9: Teach a Dog to REST

I won’t say things like that.

Page 10: Teach a Dog to REST

Let’s look at puppies.

Page 12: Teach a Dog to REST

.../getAllDogs/locationVerify/foodNeeded/createRecurringDogWalk/giveDirectOrder/healthCheck/getRecurringDogWalkSchedule/getLocation/getDog/massDogParty/getNewDogsSince/getRedDogs/getSittingDogs/dogStateChangesSearch/replaceSittingDogsWithRunningDogs/saveDog...

Page 13: Teach a Dog to REST
Page 14: Teach a Dog to REST

A puppy’s world is big.

Page 16: Teach a Dog to REST

...

/getAllLeashedDogs

/verifyVeterinarianLocation

/feedNeededFood

/createRecurringMedication

/doDirectOwnerDiscipline

/doExpressCheckupWithVeterinarian

/getRecurringFeedingSchedule

/getHungerLevel

/getSquirrelChasingPuppies

/newDogForOwner

/getNewDogsAtKennelSince

/getRedDogsWithoutSiblings

/getSittingDogsAtPark

/setLeashedDogStateTo

/replaceParkSittingDogsWithRunningDogs

/saveMommaDogsPuppies

...

...

/getAllDogs

/verifyLocation

/feedNeeded

/createRecurringWakeUp

/giveDirectOrder

/checkHealth

/getRecurringWakeUpSchedule

/getLocation

/getDog

/newDog

/getNewDogsSince

/getRedDogs

/getSittingDogs

/setDogStateTo

/replaceSittingDogsWithRunningDogs

/saveDog

...

Page 17: Teach a Dog to REST

We are on a slippery slope.

Page 18: Teach a Dog to REST

Keep the simple things simple.

Page 20: Teach a Dog to REST

We only need two URLs.

Page 21: Teach a Dog to REST

The first is for a collection.

Page 22: Teach a Dog to REST

/dogs

Page 23: Teach a Dog to REST

The second is for an element.

Page 24: Teach a Dog to REST

/dogs/bo

Page 25: Teach a Dog to REST

POSTGETPUTDELETE

Page 26: Teach a Dog to REST

CREATEREADUPDATEDELETE

Page 27: Teach a Dog to REST

POSTcreate

GETread

PUTupdate

DELETEdelete

create a new dog list dogs

replace dogs with new dogs

delete all dogs

treat as a collection create new dog in it

show Bo

if exists update Bo

if not create Bo

delete Bo

Resource

/dogs

/dogs/bo

Wikipedia

Page 28: Teach a Dog to REST

POSTcreate

GETread

PUTupdate

DELETEdelete

create a new dog list dogs

replace dogs with new dogs

delete all dogs

treat as a collection create new dog in it

show Bo

if exists update Bo

if not create Bo

delete Bo

Resource

/dogs

/dogs/bo

Wikipedia

Page 29: Teach a Dog to REST

POSTcreate

GETread

PUTupdate

DELETEdelete

create a new dog list dogs

bulk update dogs

delete all dogs

error show Bo

if exists update Bo

if noterror

delete Bo

Resource

/dogs

/dogs/bo

Wikipedia

Page 30: Teach a Dog to REST

Verbs are bad.

Page 31: Teach a Dog to REST

Nouns are good.

Page 32: Teach a Dog to REST

Plurals are better.

Page 33: Teach a Dog to REST

What about associations?

Page 34: Teach a Dog to REST

GET /owners/bob/dogs

POST /owners/bob/dogs

Page 35: Teach a Dog to REST

What about complex variations?

Page 37: Teach a Dog to REST

Sweep variations under the ‘?’

Page 38: Teach a Dog to REST

/dogs?color=red&state=running&location=park

Page 39: Teach a Dog to REST

What about pagination?

Page 40: Teach a Dog to REST

offsetlimit

Facebook

pagerpp

Twitter

startcount

LinkedIn

Page 41: Teach a Dog to REST

/dogs?limit=25&offset=50

Page 42: Teach a Dog to REST

What about searching?

Page 43: Teach a Dog to REST

/search?q=fluffy+dog

Global

/owners/bob/dogs/search?q=fluffy

Scoped

Page 44: Teach a Dog to REST

What about versioning?

Page 45: Teach a Dog to REST

/2010-04-01/Accounts/

Twilio

/services/data/v20.0/sobjects/Account

salesforce.com

?v=1.0

Facebook

Page 46: Teach a Dog to REST

/v1/dogs

Page 47: Teach a Dog to REST

Please give me exactly what I need.

Page 48: Teach a Dog to REST

/people:(id,first-name,last-name,industry)

LinkedIn

/joe.smith/friends?fields=id,name,picture

Facebook

?fields=title,media:group(media:thumbnail)

Google (partial response)

Page 49: Teach a Dog to REST

/dogs?fields=name,color,location

Page 50: Teach a Dog to REST

What about formats?

Page 51: Teach a Dog to REST

?alt=json

Google Data

/venue.json

Foursquare

Accept: application/json

Digg*

?type=json

* The type argument, if present, overrides the Accept header.

Page 52: Teach a Dog to REST

/dogs.json

/dogs/bo.json

Page 53: Teach a Dog to REST

What about partial updates?

Page 54: Teach a Dog to REST

What about errors?

Page 56: Teach a Dog to REST

Facebook{"type":"OAuthException","message":"(#803) Some

of the aliases you requested do not exist:

foo.bar"}

HTTP Status Code: 200

{"status":401,"message":"Authenticate","code":

20003,"more_info":"http://www.twilio.com/docs/

errors/20003"}

Twilio HTTP Status Code: 401

{"code":401,"message":"Authentication

Required"}

SimpleGeo HTTP Status Code: 401

Page 57: Teach a Dog to REST

200 - OK

{“message” : “Verbose, plain language description of the problem with hints about how to fix it.”“more_info” : “http://dev.teachdogrest.com/errors/12345”}

Code for code

Message for people

401 - Unauthorized

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Page 58: Teach a Dog to REST

What about the rest of the URL?

Page 59: Teach a Dog to REST

api.foursquare.comFoursquaredevelopers.foursquare.com

graph.facebook.comFacebook

developers.facebook.com

api.twitter.comTwittersearch.twitter.com

api.facebook.com

dev.twitter.com

stream.twitter.com

Page 60: Teach a Dog to REST

api.teachdogrest.com

dev.teachdogrest.com

API gateway

Developer connection

api ! dev

Do web redirects

developers ! devdeveloper ! dev

Page 61: Teach a Dog to REST

What about defaults?

Page 62: Teach a Dog to REST

json

Format

Pagination (depends on data size)

limit=10&offset=0

Page 63: Teach a Dog to REST

What about exceptional stuff?

Page 64: Teach a Dog to REST

Client intercepts HTTP error codes

Page 65: Teach a Dog to REST

{"error" : "Could not authenticate you." }

Twitter

HTTP Status Code: 200

/public_timelines.json?suppress_response_codes=true

Page 66: Teach a Dog to REST

200 - OK

Code for code ignoring

{“response_code” : “401”, “message” : “Verbose, plain language description of the problem with hints about how to fix it.”“more_info” : “http://dev.teachdogrest.com/errors/12345”, “code” : 12345}

Message for people code

/dogs?suppress_response_codes=true

Always returns OK

Page 67: Teach a Dog to REST

Client supports limited HTTP methods

Page 68: Teach a Dog to REST

Method Parameter

/dogs?method=postcreate

/dogs

read

/dogs/bo?method=put&location=parkupdate

/dogs/bo?method=deletedelete

Page 69: Teach a Dog to REST

Really? All of this? And iterate it?

Page 70: Teach a Dog to REST
Page 71: Teach a Dog to REST

API Virtualization Layer

Application

APIAPI API

Page 72: Teach a Dog to REST

Be RESTfulOnly 2 URLsNo verbsUse nouns as pluralsSweep complexity behind the ‘?’Borrow from leading APIsAccount for exceptional clientsAdd virtualization layer

Page 73: Teach a Dog to REST

THANK YOUQuestions and ideas to:

Brian Mulloy@[email protected]