Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Designing Evolvable APIs for the Web

Designing Evolvable APIs for the Web

Slides for the Nordic APIs World Tour 2015 (http://nordicapis.com/tour/nordic-apis-world-tour-2015) session at Munich and London

Pedro Felix

May 13, 2015
Tweet

More Decks by Pedro Felix

Other Decks in Programming

Transcript

  1. whoami • Teacher at the Lisbon Polytechnic Institute • Software

    developer and consultant • Telecommunication and media industries • Focus on Web APIs, Identity and Access Management • Co-author of Designing Evolvable Web APIs with ASP.NET, O’Reilly, 2014 – freely available at http://chimera.labs.oreilly.com/books/1234000001708 2
  2. It’s resources all the way down Resource “The World Wide

    Web (WWW, or simply Web) is an information space in which the items of interest, referred to as resources” In http://www.w3.org/TR/2004/REC-webarch-20041215/ (bolds are mine) 4
  3. It’s resources all the way down Resource “The World Wide

    Web (WWW, or simply Web) is an information space in which the items of interest, referred to as resources” In http://www.w3.org/TR/2004/REC-webarch-20041215/ (bolds are mine) 5 • Anything identifiable is a resource • Resources aren’t database entities • Collections • Operations • Sensors and actuators • Example: Asynchronous task execution and monitoring • Task creation resource • Task status resource • Task result resource • See section 1.10 in Subbu Allamaraju, “RESTful Web Services Cookbook”
  4. Identification via URIs Resource “The World Wide Web (WWW, or

    simply Web) is an information space in which the items of interest, referred to as resources, are identified by global identifiers called Uniform Resource Identifiers (URI).” In http://www.w3.org/TR/2004/REC-webarch-20041215/ (bolds are mine) Uniform Resource Identifier Identification 6
  5. Identification via URIs Resource “The World Wide Web (WWW, or

    simply Web) is an information space in which the items of interest, referred to as resources, are identified by global identifiers called Uniform Resource Identifiers (URI).” In http://www.w3.org/TR/2004/REC-webarch-20041215/ (bolds are mine) Uniform Resource Identifier Identification 7 • Uniformly use URIs to identify the API exposed resources • No need to expose implementation dependent identifiers such as DB ids • No “GetInvoiceById” or “GetCustomerById” • Just “GetAnythingById”, i.e., the HTTP GET method • URIs should be opaque to the clients • Provide URIs via hypermedia • When clients need to build URIs, provide URI templates • https://tools.ietf.org/html/rfc6570 (e.g. http://example.com/search{?q,lang}) • “Cool” URIs helps the development experience • https://www.example.com/invoices/123
  6. Interaction via HTTP Resource Uniform Resource Identifier Identification client HTTP

    Interaction 9 • Provides an uniform interface, independent of the resource type • Uniform resource identification via URIs • Uniform set of methods: GET, POST, PUT, DELETE, PATCH, OPTIONS … • Not only for CRUD • Namely because resources can be operations and not “just” data • Uniform interface enables • Layered system style • Intermediaries • Caching, access control • API Gateways
  7. Interaction via HTTP Resource Uniform Resource Identifier Identification client HTTP

    Interaction 11 • HTTP is an application level protocol • Make good use of it • Status codes, headers, caching, concurrency, fault tolerance • Don’t use it just for transport purposes • Examples • Use standard status code to represent processing results • Use standard caching headers to represent timeliness and cacheability • Use the Authorization header to convey authentication and authorization
  8. Interaction via HTTP Resource Uniform Resource Identifier Identification client HTTP

    Interaction 12 • API for mobile devices • Mainly for media content consumption • Based on a backend with • High latency • Low availability • Solution • Caching • Synchronous revalidation was a problem • Fault tolerance was a problem
  9. Interaction via HTTP Resource Uniform Resource Identifier Identification client HTTP

    Interaction 13 • API for mobile devices • Mainly for media content consumption • Based on a backend with • High latency • Low availability • Solution • Caching • Synchronous revalidation was a problem • Fault tolerance was a problem • Solution 2 – RFC 5861 – “HTTP Cache-Control Extensions for Stale Content” • Stale-while-revalidate • Stale-if-error • Warning header for stale content
  10. Representation using formats Resource Uniform Resource Identifier Identification cliente HTTP

    Interaction Formats resource state 16 application/octet-stream text/plain application/json application/hal+json application/collection+json
  11. Representation using formats Resource Uniform Resource Identifier Identification client HTTP

    Interaction Formats resource state links related resources 17
  12. Hypermedia Resource Uniform Resource Identifier Identification client HTTP Interaction Formats

    resource state links related resources 18 “Hypermedia is defined by the presence of application control information embedded within, or as a layer above, the presentation of information.” In http://www.ics.uci.edu/~fielding/pubs/dissertation/web_arch_domain.htm
  13. Some hypermedia usages • Embed external resources • E.g. Break

    a representation into multiple parts • Relation between resources • E.g. Where is next page on the collection? • E.g. Where are the comments for the current issue • Workflow • E.g. How can I search this collection? • E.g. How to add an element to the collection? • Access control (hinting, not enforcement) • E.g. Can I edit the represented resource? Suggestion: http://vimeo.com/49484938 19 • Beware of the hidden contract • Hypermedia makes application flow possibilities explicit • Hypermedia enables evolvability
  14. Web Linking (RFC 5988) • What defines a link? •

    Target URI • Link relation • Other attributes • Ways to add links at the HTTP metadata level • Set of link relations • E.g. edit, author, next, previous, alternate, describedby 21 Context Target Relation Other attributes
  15. Hypermedia media types "Hypermedia Types are MIME media types that

    contain native hyper- linking semantics that induce application flow. For example, HTML is a hypermedia type; XML is not.“ Mike Amundsen (2010) (bolds are mine) • Hypermedia Application Language (HAL) • JSON:API • Siren • Collection+JSON • JSON-LD 23
  16. Representation Design • Do not use serialized internal objects as

    representations • Representation-first design • State information model • Links control types • Link relations • Beware of the “JSON blank canvas” effect • Use general purpose hypermedia enabled types • Use purpose specific media-types where applicable • application/pdf • application/calendar+json • application/problem+json • application/json-home 24
  17. HAL - Hypermedia Application Language { "_links": { "self": {

    "href": "/issues/123"}, "author" : {"href”: “/users/pmhsfelix"} }, "title": “NordicApi slides", "description": "Create slides for the Nordic APIs session", "_embedded": { "issues:comments": [ { "_links": { "self": {"href" : "/api/issues/123/comment/1"}}, "text": "don’t forget it’s only 25 minutes" },{ "_links": { "self": {"href" : "/api/issues/123/comment/2"}}, "text": "finally done" } ] }} 25
  18. HAL - Hypermedia Application Language { "_links": { "self": {

    "href": "/issues/123"}, "author" : {"href”: “/users/pmhsfelix"} }, "title": “NordicApi slides", "description": "Create slides for the Nordic APIs session", "_embedded": { "issues:comments": [ { "_links": { "self": {"href" : "/api/issues/123/comment/1"}}, "text": "don’t forget it’s only 25 minutes" },{ "_links": { "self": {"href" : "/api/issues/123/comment/2"}}, "text": "finally done" } ] }} 26
  19. HAL - Hypermedia Application Language { "_links": { "self": {

    "href": "/issues/123"}, "author" : {"href”: “/users/pmhsfelix"} }, "title": “NordicApi slides", "description": "Create slides for the Nordic APIs session", "_embedded": { "issues:comments": [ { "_links": { "self": {"href" : "/api/issues/123/comment/1"}}, "text": "don’t forget it’s only 25 minutes" },{ "_links": { "self": {"href" : "/api/issues/123/comment/2"}}, "text": "finally done" } ] }} 27
  20. Final remarks • Use URIs for identification • Take advantage

    of HTTP as an application level protocol • Emphasis on representation design • Not an after-thought • Focus of the documentation effort • Take advantage of new media types • Hypermedia types • Purpose specific types • Hypermedia is a key factor for evolvability • However, be prepared to pay the additional cost 29