Successfully reported this slideshow.

REST and some Python (or 'Python "sinners" must REST')

3,063 views

Published on

A presentation -- prepared for the RoPython #1 workshop -- about general aspects regarding REST (REpresentational State Transfer) as a paradigm for developing Web services, plus several Python solutions and examples.

Published in: Technology
  • Be the first to comment

REST and some Python (or 'Python "sinners" must REST')

  1. 1. Dr. Sabin Buraga – www.purl.org/net/busaco Python “sinners” must R⚗ST
  2. 2. warning: some sensitive people might consider this presentation somewhat offensive in this situation, these persons are advised to immediately abandon all humour and better visit gore Websites for the real-life “fun” Thank you for understanding! 100% pure sarcasm & irony included
  3. 3. So, our Web application must provide certain functionalities* to be consumed by software (not only by humans)… * desired data requested via HTTP http://www.boredpanda.com/funny-chinese-translation-fails/
  4. 4. …Oh, by the way, what the Web is? 
  5. 5. …Oh, by the way, what the Web is? content ≅ sexy <mark-ups/> + stylish CSS heartaches http://www.funnypicss.com/humor/i-know-html/
  6. 6. …Oh, by the way, what the Web is? interaction ≅ URI + hypermedia (HTTP delights?) http://httpstatusdogs.com/
  7. 7. …Oh, by the way, what the Web is? development ≅ client and/or server-side programming %#^&! paradigms/languages/frameworks/magic/… http://stressandanger.wordpress.com/funny-pictures/
  8. 8. In order to satisfy some 〠 needs, the software must offer certain ☠ services* http://brog.engrish.com/ * the action of helping or doing work for someone
  9. 9. Web service a piece of s…oftware providing a s…pecific functionality http://wallpaperstop.co/wallpapers/2013/07/Cats-Funny-Meme-272x480.jpg
  10. 10. Web service a piece of s…oftware providing a s…pecific functionality access to resources of (dis)interest – Instagram, Pinterest, Tumblr boring news aggregators – Digg, Reddit (“surreal”) maps – Bing Maps, Google Maps, Nokia HERE instant mess(ages) – Jabber, Twitter, Twilio world-wide stupidity – see Facebook (Open Graph Protocol) (unsecure) data storage – Amazon S3, Dropbox, OneDrive …and many, many, many others
  11. 11. Web service developed & deployed by using W3C standards URI (Unicorn Ridiculous Identifier) – aka Web address access via HTTP (Hyper Trickiest Triviality Provider) data formats: JSON (JavaScript Orgiastic Needs) XML (Exquisite Manual Labor) …
  12. 12. Web service a classical implementation: using certain Web application servers/frameworks for Python, some popular choices are Django, Grok, TurboGears, web2py, Zope https://wiki.python.org/moin/WebFrameworks
  13. 13. generic architecture of a traditional Web application (N-tier application) Client Web application Storage (user interface) server/framework (data persistence) Internet (Web)
  14. 14. Web service goal: processing the answer (data) provided by a given Web site/application in order to (re)use it in our (client) programs http://nedhardy.com/2013/04/22/17-hilarious-ad-placement-fails/
  15. 15. Web service a naïve solution: Web scraping extracting data of interest by parsing (incorrect) HTML constructs www.winhdwallpapers.com/funny/funny-html-code-hd-wallpaper-1080p
  16. 16. Web service Web scraping several Python libraries: Beautiful Soup – www.crummy.com/software/BeautifulSoup/ html5lib-python – https://github.com/html5lib/html5lib-python lxml – http://lxml.de/ Scrappy – http://scrapy.org/
  17. 17. How about an interesting* paradigm of Web service development? * aka pragmatic http://commons.wikimedia.org/wiki/File:France_Paris_Notre-Dame-Adam_and_Eve.jpg
  18. 18. REST (REpresentational State Transfer) Web development focused on the representation(s) of a resource Roy Fielding – PhD Thesis (2000)
  19. 19. REST (REpresentational State Transfer) Web development focused on the representation(s) of a resource e.g., user, blog, article, photo, source-code,…
  20. 20. REST (REpresentational State Transfer) Web development focused on the representation(s) of a resource HTML, PNG, SVG, JSON,…
  21. 21. REST (REpresentational State Transfer) Web development focused on the representation(s) of a resource denoted by MIME types: text/html, application/json,…
  22. 22. each resource can have multiple representations accessed via HTTP requests
  23. 23. clients (browsers, robots, players,…) interact with the resource representations via HTTP verbs: create: POST, retrieve: GET, update: PUT, delete: DELETE representation2 (Atom) resource URI URLURL GET POST GET DELETE representation1 (HTML)
  24. 24. resource1 representation1 (HTML) http://blog.info/ http://blog.info/post http://blog.info/post/edit http://blog.info/news GET resource2 resource3 resource4 GET POST representation2 (HTML) representation3 (HTML) representation4 (Atom)
  25. 25. resource1 representation1 (HTML) http://blog.info/ http://blog.info/post http://blog.info/post/edit http://blog.info/news GET resource2 resource3 resource4 GET POST representation2 (HTML) representation3 (HTML) representation4 (Atom) HATEOAS (Hypermedia As The Engine Of Application State)
  26. 26. So, some practical advises about building RESTful Web services? http://www.funpedia.net/imgs/may11/very-funny-signs-06.jpg
  27. 27. Use nouns for each class of resources to be managed by your service User Document – alternatives: Photo, Product, Software Metadata – e.g., Comment, Format, Platform, Tag,…
  28. 28. Use nouns for each class of resources to be managed by your service collections of “things” (typically, plural) /snakes unique identifiers for every member of a collection /snakes/python versus /snakes/69
  29. 29. SoundCloud API types of resources: tracks, users, current user (me), playlists, groups, comments http://developers.soundcloud.com/
  30. 30. Name each resource by using URLs http://www.slideshare.net/busaco/presentations http://www.imdb.com/title/tt0401383/ http://www.last.fm/music/Moby http://vimeo.com/channels/731014
  31. 31. Design accepted representations to be sent by potential clients + optional parameters (useful for pagination, partial responses, filtering,…) /snakes?limit=3&offset=14 /snakes?fields=name,age,description
  32. 32. Choose the representation(s) of the response to be sent to client consider existing standardized data formats e.g., HTML, Atom, JSON,… Accept: application/json (Digg) /venue.json (Foursquare) http://api.sciencemuseum.org.uk/exhibitions/?output=json
  33. 33. interactive access to the data provided by a public Web service http://developer.nytimes.com/ JSON response
  34. 34. Integrate resources via URLs + Web forms
  35. 35. Create use cases specify exception/error conditions via HTTP status codes + additional information 200 OK, 303 See Other, 400 Bad Request, 404 Not Found,… http://httpstatus.es/
  36. 36. “Never release an API without a version and make the version mandatory.”—Brian Mulloy (2012) http://feeds.delicious.com/v2/{format}/{username} ?v=1.0 (Facebook)
  37. 37. Some (programming) tools for developers? http://www.weirdasianews.com/category/engrish/
  38. 38. Python HTTP clients classical built-in modules: httplib (Python 2) http.client (Python 3)
  39. 39. Python HTTP clients a specific library: Unirest for Python – http://unirest.io/python.html
  40. 40. Developing RESTful Web services in Python one of our favorite frameworks: Bottle, CheeryPy, Django REST, Flask, web.py
  41. 41. using a Python wrapper for a specific Web service/API: www.pythonapi.com
  42. 42. Useful Web resources for learning & making experiments?
  43. 43. building a REST service: Learn REST: A RESTful Tutorial www.restapitutorial.com Thoughts on RESTful API Design https://restful-api-design.readthedocs.org Implementing REST https://code.google.com/p/implementing-rest/
  44. 44. testing our/others APIs: Make HTTP Requests http://www.hurl.it/ HTTP Request & Response http://httpbin.org/ Apigee Console https://apigee.com/console/
  45. 45. don’t forget to consult: ProgrammableWeb http://www.programmableweb.com/ The Web engineer’s online toolbox ivanzuzak.info/2012/11/18/the-web-engineers-online-toolbox.html
  46. 46. R⚗ST (and some Python) http://photos1.blogger.com/blogger/3043/2944/1600/woman-snake-1280.jpg

×