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

API Reliability Guide

API Reliability Guide

Insights in to creating a robust, flexible and reliable API.

Nick DeNardis

June 11, 2012
Tweet

More Decks by Nick DeNardis

Other Decks in Technology

Transcript

  1. Nick DeNardis Associate Director of Web Communications at Wayne State

    University http://wayne.edu/ Host of EDU Checkup http://educheckup.com/ Curator of EDU Snippits http://edusnippits.com/ Writer for .eduGuru http://doteduguru.com/
  2. The API’s job is to make the developer as successful

    as possible http://knowyourmeme.com/memes/i-hate-sandcastles-success-kid
  3. Mobile isn’t going anywhere 1.45 Million devices per day 371,000

    births per day http://www.lukew.com/ff/entry.asp?1506
  4. Mobile Data Traffic Expected To Rise 40- Fold Over Next

    Five Years http://techcrunch.com/2010/03/30/mobile-data-traffic-rise-40-fold/
  5. 250 kb - Avg page weight 2.5 pages - Avg

    number per visit 625 kb - Bandwidth per visit Desktop 50 kb - Avg page weight 25 pages - Avg number per visit 1.25 mb - Bandwidth per visit Mobile 0 325 650 975 1300 Data Desktop Mobile
  6. 0 10 20 30 40 2009 2010 2011 2012 2013

    2014 2015 2016 2017 Mobile Desktop Millions of visitors http://wayne.edu/
  7. Time Cell Latency Initial HTML Javascript Images CSS You can’t

    blame the network for everything Time Cell Latency New Content Images Second Request First Request
  8. One size != fit all Think versioning from the start

    https://api.twitter.com/1/ https://us2.api.mailchimp.com/1.3/ https://api.foursquare.com/v2/ https://api.instagram.com/v1/ https://www.salesforce.com/services/Soap/c/18.0 https://api.wayne.edu/v1/
  9. SOAP The request: GET /StockPrice HTTP/1.1 Host: example.org Content-Type: application/soap+xml;

    charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <env:Envelope xmlns:env="http://www.w3.org/ 2003/05/soap-envelope" xmlns:s="http://www.example.org/stock-service"> <env:Body> <s:GetStockQuote> <s:TickerSymbol>IBM</s:TickerSymbol> </s:GetStockQuote> </env:Body> </env:Envelope> The response: HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <env:Envelope xmlns:env="http://www.w3.org/ 2003/05/soap-envelope" xmlns:s="http://www.example.org/stock-service"> <env:Body> <s:GetStockQuoteResponse> <s:StockPrice>45.25</s:StockPrice> </s:GetStockQuoteResponse> </env:Body> </env:Envelope> The request: GET /StockPrice/IBM HTTP/1.1 Host: example.org Accept: text/xml Accept-Charset: utf-8 The response: HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <s:Quote xmlns:s="http://example.org/stock- service"> <s:TickerSymbol>IBM</s:TickerSymbol> <s:StockPrice>45.25</s:StockPrice> </s:Quote> REST 4 kb vs 2 kb Round Trip
  10. Your best friend JSON Easy to encode: $arr = array('a'

    => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); json_encode($arr); Easy to decode: $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; json_decode($json); Javascript: var myObject = eval('(' + json + ')'); Douglas Crockford
  11. Flattened highed = More interfaces CRM CMS iModules Events Course

    Schedule Parking Flickr YouTube Faculty Profiles Library Catalog
  12. Your data is everywhere CMS Events LDAP Banner Gather &

    Clean Shadow storage API Webserver Website Mobile Website Mobile App Digital Signage Third Party
  13. API is the glue CMS Events LDAP Banner Gather &

    Clean Shadow storage API Webserver Website Mobile Website Mobile App Digital Signage Third Party
  14. Making the glue Server space you control api.domain.edu or domain.edu/api

    Ingredients: Ability: • Database • Cronjobs • Server logs • Analytics • PHP accelerator • Xdebug
  15. Affordance The physical design will communicate how it is suppose

    to be used http://www.flickr.com/photos/hawkexpress/269032594/
  16. The devil is in the details Simple URL Response code

    Total count Data container Keep it lightweight 91 KB
  17. Static files <?php if ((is_file($_SERVER['SCRIPT_FILENAME'].'.json')) && (time()-filemtime($_SERVER['SCRIPT_FILENAME'].'.json') < 3600)) readfile($_SERVER['SCRIPT_FILENAME'].'.json');

    exit; } // (the php script itself goes here) echo $response; $fp = fopen($_SERVER['SCRIPT_FILENAME'].'.json', 'w'); fwrite($fp, $response); fclose($fp); ?>
  18. if (typeof(localStorage) == 'undefined' ) { alert('Your browser does not

    support HTML5 localStorage. Try upgrading.'); } else { try { localStorage.setItem("name", "Hello World!"); //saves to the database, } catch (e) { if (e == QUOTA_EXCEEDED_ERR) { alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error } } document.write(localStorage.getItem("name")); //Hello World! localStorage.removeItem("name"); //deletes the matching item from the database } http://paperkilledrock.com/2010/05/html5-localstorage-part-one/ HTML5 localStorage
  19. JSONP function handle_data(data) { // `data` is now the object

    representation of the JSON data } --- http://some.tld/web/service?callback=handle_data: --- handle_data({"data_1": "hello world", "data_2": ["the","sun","is","shining"]});
  20. Expires header <?php header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T',

    time() + 3600)); ?> Semesters Subjects Degrees Map Categories Map Locations 1 Month Courses Media Experts 1 Week Course Availability Parking Availability No Cache Events News 1 Day
  21. Our Stats Feb 2011 - In production 6.1 million requests

    48% from mobile 21% iOS 23% Android 66% Web 95% GET’s 16 ms average response time
  22. Resources • http://apigee.com/ • http://rubyonrails.org/ • http://cakephp.org/ • https://groups.google.com/group/api-craft/ •

    http://37signals.com/svn/posts/3018-api-design-for-humans • http://broadcast.oreilly.com/2011/06/the-good-the-bad-the-ugly-of-rest- apis.html • http://sixrevisions.com/html/introduction-web-storage/ • http://webcomm.fiu.edu/2011/11/json-as-an-api-tool-and-why-its-awesome/ • http://doteduguru.com/id4579-results-higher-ed-cms-usage.html