get some rest best restful api practices about me
play

Get Some REST: Best RESTful API Practices About Me Lorna Jane - PowerPoint PPT Presentation

Get Some REST: Best RESTful API Practices About Me Lorna Jane Mitchell PHP Consultant/Developer API Specialist Author of PHP Master Twitter: @lornajane Website: http://lornajane.net 2 About REST REpresentational State


  1. Get Some REST: Best RESTful API Practices

  2. About Me • Lorna Jane Mitchell • PHP Consultant/Developer • API Specialist • Author of PHP Master • Twitter: @lornajane • Website: http://lornajane.net 2

  3. About REST • REpresentational State Transfer • Everything is a resource • Representations get passed around • Great for data-driven applications 3

  4. Not RESTful Clues that your service isn’t RESTful: • It has a single endpoint 4

  5. Not RESTful Clues that your service isn’t RESTful: • It has a single endpoint • All requests are POSTs 4

  6. Not RESTful Clues that your service isn’t RESTful: • It has a single endpoint • All requests are POSTs • Response metadata is in the body, not header 4

  7. Not RESTful Clues that your service isn’t RESTful: • It has a single endpoint • All requests are POSTs • Response metadata is in the body, not header • There are verbs in the URL 4

  8. Not RESTful Clues that your service isn’t RESTful: • It has a single endpoint • All requests are POSTs • Response metadata is in the body, not header • There are verbs in the URL • The URL includes method names 4

  9. RESTful != Useful

  10. Best Practices

  11. Use Existing Skills/Tools

  12. Existing Skills 8

  13. MVC Applications Model-View-Controller becomes Model-Controller-Output Handler 9

  14. MVC Applications Model-View-Controller becomes Model-Controller-Output Handler index output controller model 10

  15. Output Handlers: JSON class JsonView extends ApiView { public function render($content) { header('Content-Type: application/json; charset=utf8'); echo $this->buildOutput($content); return true; } /** Function to build output, can be used by JSON and JSONP * */ protected function buildOutput ($content) { $content = $this->addCount($content); $retval = json_encode($content, JSON_NUMERIC_CHECK); return $retval; } } 11

  16. Meaningful URL Structure

  17. Meaningful URLs What would you expect to find at: http://api.joind.in/v2.1/events?filter=upcoming 13

  18. Meaningful URLs What would you expect to find at: http://api.joind.in/v2.1/events?filter=upcoming http://api.joind.in/v2.1/events/853 13

  19. Meaningful URLs What would you expect to find at: http://api.joind.in/v2.1/events?filter=upcoming http://api.joind.in/v2.1/events/853 http://api.joind.in/v2.1/events/853/talks 13

  20. Meaningful URLs What would you expect to find at: http://api.joind.in/v2.1/events?filter=upcoming http://api.joind.in/v2.1/events/853 http://api.joind.in/v2.1/events/853/talks http://api.joind.in/v2.1/talks/6232 13

  21. Meaningful URLs What would you expect to find at: http://api.joind.in/v2.1/events?filter=upcoming http://api.joind.in/v2.1/events/853 http://api.joind.in/v2.1/events/853/talks http://api.joind.in/v2.1/talks/6232 http://api.joind.in/v2.1/talks/6232/comments 13

  22. Verbs and URLs These URLs give us representations of either resources or collections of resources We use HTTP verbs to operate on these resources Verb Collection Resource GET fetch resources fetch resource POST create a resource in this collection PUT create or update a resource here PATCH update part of a resource DELETE delete a resource 14

  23. Identifying Resources What is a "resource"? • Maybe a database row • Maybe an item with some information from other tables • Talks also show speaker names, for example • Maybe a sub-resource 15

  24. Considering Sub-Resources Common pitfall: http://example.com/articles/42/activate 16

  25. Considering Sub-Resources Common pitfall: http://example.com/articles/42/activate We have two options GET http://example.com/articles/42 , change the "active" flag, and PUT it back 16

  26. Considering Sub-Resources Common pitfall: http://example.com/articles/42/activate We have two options GET http://example.com/articles/42 , change the "active" flag, and PUT it back Make a subresource, then you can just work with http://example.com/articles/42/active 16

  27. Hypermedia

  28. Hypermedia GET http://api.joind.in/v2.1/events/853 18

  29. Hypermedia • Consumers can follow links rather than construct them • Providers can change link structures $event_url = 'http://api.joind.in/v2.1/events/853'; $event_data = json_decode( file_get_contents($event_url), TRUE); // get event talks $talks = json_decode( file_get_contents($event_data['events'][0]['talks_uri']), TRUE ); print_r($talks['talks'][0]); 19

  30. Content Negotiation

  31. Content Negotiation We use the Accept and Content-Type headers to agree what formats to use. Common examples: • application/json • application/xml • text/html Next stage: use Media Types for custom/versioned representations 21

  32. Content Negotiation Confession The joind.in API also allows ?format=json as an override 22

  33. Error Handling

  34. Simple Error Rules for Service Providers • Use expected format • Give meaningful messages • Be consistent 24

  35. Exceptions Help Consistency index output controller model 25

  36. Exceptions Help Consistency function handle_exception($e) { // pull the correct format before we bail global $request; header("Status: " . $e->getCode(), false, $e->getCode()); $request->view->render(array($e->getMessage())); } set_exception_handler('handle_exception'); 26

  37. When Things Go Wrong

  38. Crisis Strategies • Lots of logging! • Logging rather than debug output if the client expects JSON • Proxy or network analyser • Inspects traffic without application changes • e.g. Wireshark or Charles proxy • Drop back to last known good outcome • Increase complexity in "baby steps" 28

  39. Protecting Yourself

  40. Isolate External Dependencies Never call an API from your own code - have a class that wraps it. You can: • cache results if it is slow/flaky • replace it in one place later, if needed • write tests to check the API is still doing what you expect • write a mock object to replace it with for your own tests 30

  41. Respect the Danger Website Mobile Application logic layer Digital Identity API Product API Database 31

  42. Respect the Danger Website Mobile Application logic layer abyss Digital Identity API Product API Database 32

  43. Deliver on Time How to estimate for API integration? • You can’t estimate an unknown task • Invest time in a prototype • In agile speak, a "spike" 33

  44. Best Practice for RESTful Applications • Use existing tools, skills and conventions • Deliver what’s useful to your users • Plan defensively 34

  45. Questions?

  46. Thanks! @lornajane http://lornajane.net 36

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend