44
USABLE REST APIs { "_links":{ "author": {"href":"http://javier-ramirez.com"}, "work": {"href":"https://teowaki.com"}, "twitter": {"href":"https://twitter.com/supercoco9"} } }

Usable REST APIs. BCNdevcon edition

Embed Size (px)

DESCRIPTION

With the adoption of REST, the proliferation of smartphones and tablets, and the second coming of JavaScript, exposing our applications as a service is now more important than ever. Lots of libraries and frameworks make really easy to create a (kinda) RESTful API but, in many occassions, these APIs are designed without really thinking on the developers that will have to use them. I want to talk about some of the points that can help making your API more developer-friendly. Some of the areas I’ll cover will be discoverability, authentication, headers, formats, parameters, documentation and tools.

Citation preview

Page 1: Usable REST APIs. BCNdevcon edition

USABLE REST APIs{ "_links":{ "author": {"href":"http://javier-ramirez.com"}, "work": {"href":"https://teowaki.com"}, "twitter": {"href":"https://twitter.com/supercoco9"}

} }

Page 2: Usable REST APIs. BCNdevcon edition

Bedale, North of England

Page 3: Usable REST APIs. BCNdevcon edition

a randompostbox in Bedale

Page 4: Usable REST APIs. BCNdevcon edition

a usabilityproblem

Page 5: Usable REST APIs. BCNdevcon edition

everybody agrees web usability is a good investment

Page 6: Usable REST APIs. BCNdevcon edition

API usability? meh

http://docs.aws.amazon.com/AWSECommerceService/2011-08-01/DG/rest-signature.html

Page 7: Usable REST APIs. BCNdevcon edition
Page 8: Usable REST APIs. BCNdevcon edition
Page 9: Usable REST APIs. BCNdevcon edition
Page 10: Usable REST APIs. BCNdevcon edition
Page 11: Usable REST APIs. BCNdevcon edition
Page 12: Usable REST APIs. BCNdevcon edition
Page 13: Usable REST APIs. BCNdevcon edition

Web usability is an approach to make web sites

easy to use for an end-user, without the requirement that any specialized training be

undertaken.

Page 14: Usable REST APIs. BCNdevcon edition

LearnabilityEfficiencyMemorabilityErrorsSatisfaction

Usability 101, the 5 quality components by Jakob Nielsen

Page 15: Usable REST APIs. BCNdevcon edition
Page 16: Usable REST APIs. BCNdevcon edition

EFFECTIVE IMMEDIATELY!! NO MORE TYPEWRITERS ARE TO BE PURCHASED, LEASED, etc., etc. Apple is an innovative company. We must believe and lead in all areas. If word processing is so neat, then let's all use it!

Mike Scott, Apple President, 1980

Page 17: Usable REST APIs. BCNdevcon edition
Page 18: Usable REST APIs. BCNdevcon edition

Succeed consistently

Fail consistently

Page 19: Usable REST APIs. BCNdevcon edition

huddle200 OK 201 Created202 Accepted400 Bad Request401 Unauthorized403 Forbidden404 Not Found410 Gone

twitter200 OK Success!304 Not Modified400 Bad Request401 Unauthorized403 Forbidden404 Not Found406 Not Acceptable420 Enhance Your Calm500 Internal Server Error502 Bad Gateway503 Service Unavailable

Useful Status429 Too many requests204 No Content

teowaki200 OK Success!201 Created304 Not Modified401 Unauthorized403 Forbidden404 Not Found422 Unprocessable Entity406 Not Acceptable500 Internal Server Error

Page 20: Usable REST APIs. BCNdevcon edition

speed or at least asynchronous operations

Page 21: Usable REST APIs. BCNdevcon edition

different users

different nerds needs

Page 22: Usable REST APIs. BCNdevcon edition

netflix REST apiresource based

Page 23: Usable REST APIs. BCNdevcon edition

netflix REST apiexperience based

http://www.slideshare.net/danieljacobson/api-revolutions-16755403

Page 24: Usable REST APIs. BCNdevcon edition

all your format

are belong to us

*even native formats

Page 25: Usable REST APIs. BCNdevcon edition

formats

Page 26: Usable REST APIs. BCNdevcon edition

CORS: Cross origin resource sharing

Page 27: Usable REST APIs. BCNdevcon edition

Don't expose your implementations details Resources are not models

Page 28: Usable REST APIs. BCNdevcon edition

Easier to understand

Change the internals without breaking the contract

Resources based on business objects are more resistant to versioning

More opacity means more security

Page 29: Usable REST APIs. BCNdevcon edition

REST

Page 30: Usable REST APIs. BCNdevcon edition

REST Highlights you should know about but not necessarily implement

client-server,stateless,layered,cacheable Resources

Resource IdentifiersResource metadata

Uniform interfaceoperationsRepresentationsRepresentation metadata

HATEOAS (Hypermedia)Optionally: code on demand

Page 31: Usable REST APIs. BCNdevcon edition

Hypermedia

Navigable APIs

Page 32: Usable REST APIs. BCNdevcon edition

API pagination

Page 33: Usable REST APIs. BCNdevcon edition

API links------Next steps

Page 34: Usable REST APIs. BCNdevcon edition

Several resources in a single request

Page 35: Usable REST APIs. BCNdevcon edition

Versioning without versions

Page 36: Usable REST APIs. BCNdevcon edition

HAL:HypermediaApplicationLanguage

extra ball => http://stedolan.github.io/jq/

Page 37: Usable REST APIs. BCNdevcon edition

empty new resources> curl “https://api.teowaki.com/teams/5677-dev”

Page 38: Usable REST APIs. BCNdevcon edition

Partial Responses

teowaki's approach

api.teowaki.com/people/tw?api_quiet=true&api_links=false

Google's approach

www.googleapis.com/youtube/v3/videos?part=snippet&fields=items(id,snippet(title,position))

Page 39: Usable REST APIs. BCNdevcon edition

Where to put your metadata In your HTTP Headers?

As request params and response fields?

Both?

Don't worry too much. Just choose one and stick to it

Page 40: Usable REST APIs. BCNdevcon edition
Page 41: Usable REST APIs. BCNdevcon edition
Page 42: Usable REST APIs. BCNdevcon edition

Don't just implement. ThinkShould the API allow asynchronous creation, update and deletion? => return-async

Should it return the created/deleted/updated object or just a status code? =>return-representation

Should it ignore extra params transparently or should it warn you? => api_strict_mode

Should it allow for bulk updates/deletions on collections? => PATCH /user/links

Think of your own questions. There are not good or bad answers

Page 43: Usable REST APIs. BCNdevcon edition

don't let your APIbe the poobox whereyour userspost theirrequests

Moral of this talk

Page 44: Usable REST APIs. BCNdevcon edition

Shameless self promotion If you enjoyed this presentation, please thank me by

registering on https://teowaki.com

It's a site for developers, you can hang around for free, and I think it's quite cool (but a bit buggy since we've just launched)

<3 <3 <3Javier Ramírez

@supercoco9