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.
Exploring the Future Potential of AI-Enabled Smartphone Processors
REST and some Python (or 'Python "sinners" must REST')
1. Dr. Sabin Buraga – www.purl.org/net/busaco
Python “sinners” must
R⚗ST
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. 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/
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. …Oh, by the way,
what the Web is?
interaction ≅ URI + hypermedia
(HTTP delights?)
http://httpstatusdogs.com/
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. 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. Web service
a piece of s…oftware providing a s…pecific functionality
http://wallpaperstop.co/wallpapers/2013/07/Cats-Funny-Meme-272x480.jpg
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. 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. 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. generic architecture of a traditional Web application
(N-tier application)
Client Web application Storage
(user interface) server/framework (data persistence)
Internet
(Web)
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. 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. 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. 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. REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
Roy Fielding – PhD Thesis (2000)
19. REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
e.g., user, blog, article,
photo, source-code,…
20. REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
HTML, PNG, SVG, JSON,…
21. REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
denoted by MIME types:
text/html, application/json,…
22. each resource can have multiple representations
accessed via HTTP requests
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)
26. So, some practical
advises about building
RESTful Web services?
http://www.funpedia.net/imgs/may11/very-funny-signs-06.jpg
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. 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. SoundCloud API
types of resources:
tracks, users, current user (me),
playlists, groups, comments
http://developers.soundcloud.com/
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. 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. 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. interactive access to the data provided by a public Web service
http://developer.nytimes.com/
JSON response
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. “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)
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/
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