KEMBAR78
REST and some Python (or 'Python "sinners" must REST') | PDF
Dr. Sabin Buraga – www.purl.org/net/busaco
Python “sinners” must
R⚗ST
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
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/
…Oh, by the way,
what the Web is?

…Oh, by the way,
what the Web is?
content ≅ sexy <mark-ups/>
+ stylish CSS heartaches
http://www.funnypicss.com/humor/i-know-html/
…Oh, by the way,
what the Web is?
interaction ≅ URI + hypermedia
(HTTP delights?)
http://httpstatusdogs.com/
…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/
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
Web service
a piece of s…oftware providing a s…pecific functionality
http://wallpaperstop.co/wallpapers/2013/07/Cats-Funny-Meme-272x480.jpg
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
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)
…
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
generic architecture of a traditional Web application
(N-tier application)
Client Web application Storage
(user interface) server/framework (data persistence)
Internet
(Web)
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/
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
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/
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
REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
Roy Fielding – PhD Thesis (2000)
REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
e.g., user, blog, article,
photo, source-code,…
REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
HTML, PNG, SVG, JSON,…
REST (REpresentational State Transfer)
Web development focused on
the representation(s) of a resource
denoted by MIME types:
text/html, application/json,…
each resource can have multiple representations
accessed via HTTP requests
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)
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)
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)
So, some practical
advises about building
RESTful Web services?
http://www.funpedia.net/imgs/may11/very-funny-signs-06.jpg
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,…
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
SoundCloud API
types of resources:
tracks, users, current user (me),
playlists, groups, comments
http://developers.soundcloud.com/
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
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
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
interactive access to the data provided by a public Web service
http://developer.nytimes.com/
JSON response
Integrate resources via URLs + Web forms
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/
“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)
Some (programming)
tools for developers?
http://www.weirdasianews.com/category/engrish/
Python HTTP clients
classical built-in modules:
httplib (Python 2)
http.client (Python 3)
Python HTTP clients
a specific library:
Unirest for Python – http://unirest.io/python.html
Developing RESTful Web services in Python
one of our favorite frameworks:
Bottle, CheeryPy, Django REST, Flask, web.py
using a Python wrapper for
a specific Web service/API:
www.pythonapi.com
Useful Web resources
for learning & making
experiments?
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/
testing our/others APIs:
Make HTTP Requests
http://www.hurl.it/
HTTP Request & Response
http://httpbin.org/
Apigee Console
https://apigee.com/console/
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
R⚗ST
(and some Python)
http://photos1.blogger.com/blogger/3043/2944/1600/woman-snake-1280.jpg

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 peoplemight 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 Webapplication 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.
    …Oh, by theway, what the Web is? 
  • 5.
    …Oh, by theway, what the Web is? content ≅ sexy <mark-ups/> + stylish CSS heartaches http://www.funnypicss.com/humor/i-know-html/
  • 6.
    …Oh, by theway, what the Web is? interaction ≅ URI + hypermedia (HTTP delights?) http://httpstatusdogs.com/
  • 7.
    …Oh, by theway, what the Web is? development ≅ client and/or server-side programming %#^&! paradigms/languages/frameworks/magic/… http://stressandanger.wordpress.com/funny-pictures/
  • 8.
    In order tosatisfy 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 pieceof s…oftware providing a s…pecific functionality http://wallpaperstop.co/wallpapers/2013/07/Cats-Funny-Meme-272x480.jpg
  • 10.
    Web service a pieceof 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 classicalimplementation: 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 ofa traditional Web application (N-tier application) Client Web application Storage (user interface) server/framework (data persistence) Internet (Web)
  • 14.
    Web service goal: processingthe 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ïvesolution: 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 severalPython 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 aninteresting* paradigm of Web service development? * aka pragmatic http://commons.wikimedia.org/wiki/File:France_Paris_Notre-Dame-Adam_and_Eve.jpg
  • 18.
    REST (REpresentational StateTransfer) Web development focused on the representation(s) of a resource Roy Fielding – PhD Thesis (2000)
  • 19.
    REST (REpresentational StateTransfer) Web development focused on the representation(s) of a resource e.g., user, blog, article, photo, source-code,…
  • 20.
    REST (REpresentational StateTransfer) Web development focused on the representation(s) of a resource HTML, PNG, SVG, JSON,…
  • 21.
    REST (REpresentational StateTransfer) Web development focused on the representation(s) of a resource denoted by MIME types: text/html, application/json,…
  • 22.
    each resource canhave 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)
  • 24.
  • 25.
  • 26.
    So, some practical advisesabout building RESTful Web services? http://www.funpedia.net/imgs/may11/very-funny-signs-06.jpg
  • 27.
    Use nouns foreach 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 foreach 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 ofresources: tracks, users, current user (me), playlists, groups, comments http://developers.soundcloud.com/
  • 30.
    Name each resourceby 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 representationsto 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 tothe data provided by a public Web service http://developer.nytimes.com/ JSON response
  • 34.
    Integrate resources viaURLs + Web forms
  • 35.
    Create use cases specifyexception/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 anAPI without a version and make the version mandatory.”—Brian Mulloy (2012) http://feeds.delicious.com/v2/{format}/{username} ?v=1.0 (Facebook)
  • 37.
    Some (programming) tools fordevelopers? http://www.weirdasianews.com/category/engrish/
  • 38.
    Python HTTP clients classicalbuilt-in modules: httplib (Python 2) http.client (Python 3)
  • 39.
    Python HTTP clients aspecific library: Unirest for Python – http://unirest.io/python.html
  • 40.
    Developing RESTful Webservices in Python one of our favorite frameworks: Bottle, CheeryPy, Django REST, Flask, web.py
  • 41.
    using a Pythonwrapper for a specific Web service/API: www.pythonapi.com
  • 42.
    Useful Web resources forlearning & making experiments?
  • 43.
    building a RESTservice: 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.
    testing our/others APIs: MakeHTTP Requests http://www.hurl.it/ HTTP Request & Response http://httpbin.org/ Apigee Console https://apigee.com/console/
  • 45.
    don’t forget toconsult: ProgrammableWeb http://www.programmableweb.com/ The Web engineer’s online toolbox ivanzuzak.info/2012/11/18/the-web-engineers-online-toolbox.html
  • 46.