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

46
Dr. Sabin Buraga www.purl.org/net/busaco Python “sinners” must R ST

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

Page 1: REST and some Python (or 'Python "sinners" must REST')

Dr. Sabin Buraga – www.purl.org/net/busaco

Python “sinners” must

R⚗ST

Page 2: REST and some Python (or 'Python "sinners" must REST')

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

Page 3: REST and some Python (or 'Python "sinners" must REST')

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/

Page 4: REST and some Python (or 'Python "sinners" must REST')

…Oh, by the way, what the Web is?

Page 5: REST and some Python (or 'Python "sinners" must REST')

…Oh, by the way, what the Web is?

content ≅ sexy <mark-ups/> + stylish CSS heartaches

http://www.funnypicss.com/humor/i-know-html/

Page 6: REST and some Python (or 'Python "sinners" must REST')

…Oh, by the way, what the Web is?

interaction ≅ URI + hypermedia(HTTP delights?)

http://httpstatusdogs.com/

Page 7: REST and some Python (or 'Python "sinners" must REST')

…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/

Page 8: REST and some Python (or 'Python "sinners" must REST')

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

Page 9: REST and some Python (or 'Python "sinners" must REST')

Web servicea piece of s…oftware providing a s…pecific functionality

http://wallpaperstop.co/wallpapers/2013/07/Cats-Funny-Meme-272x480.jpg

Page 10: REST and some Python (or 'Python "sinners" must REST')

Web servicea piece of s…oftware providing a s…pecific functionality

access to resources of (dis)interest – Instagram, Pinterest, Tumblrboring news aggregators – Digg, Reddit

(“surreal”) maps – Bing Maps, Google Maps, Nokia HEREinstant 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

Page 11: REST and some Python (or 'Python "sinners" must REST')

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)…

Page 12: REST and some Python (or 'Python "sinners" must REST')

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

Page 13: REST and some Python (or 'Python "sinners" must REST')

generic architecture of a traditional Web application(N-tier application)

Client Web application Storage

(user interface) server/framework (data persistence)

Internet

(Web)

Page 14: REST and some Python (or 'Python "sinners" must REST')

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/

Page 15: REST and some Python (or 'Python "sinners" must REST')

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

Page 16: REST and some Python (or 'Python "sinners" must REST')

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/

Page 17: REST and some Python (or 'Python "sinners" must REST')

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

Page 18: REST and some Python (or 'Python "sinners" must REST')

REST (REpresentational State Transfer)

Web development focused on the representation(s) of a resource

Roy Fielding – PhD Thesis (2000)

Page 19: REST and some Python (or 'Python "sinners" must REST')

REST (REpresentational State Transfer)

Web development focused on the representation(s) of a resource

e.g., user, blog, article, photo, source-code,…

Page 20: REST and some Python (or 'Python "sinners" must REST')

REST (REpresentational State Transfer)

Web development focused on the representation(s) of a resource

HTML, PNG, SVG, JSON,…

Page 21: REST and some Python (or 'Python "sinners" must REST')

REST (REpresentational State Transfer)

Web development focused on the representation(s) of a resource

denoted by MIME types:text/html, application/json,…

Page 22: REST and some Python (or 'Python "sinners" must REST')

each resource can have multiple representations accessed via HTTP requests

Page 23: REST and some Python (or 'Python "sinners" must REST')

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)

Page 24: REST and some Python (or 'Python "sinners" must REST')

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)

Page 25: REST and some Python (or 'Python "sinners" must REST')

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)

Page 26: REST and some Python (or 'Python "sinners" must REST')

So, some practical advises about buildingRESTful Web services?

http://www.funpedia.net/imgs/may11/very-funny-signs-06.jpg

Page 27: REST and some Python (or 'Python "sinners" must REST')

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,…

Page 28: REST and some Python (or 'Python "sinners" must REST')

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

Page 29: REST and some Python (or 'Python "sinners" must REST')

SoundCloud API

types of resources:

tracks, users, current user (me),

playlists, groups, comments

http://developers.soundcloud.com/

Page 30: REST and some Python (or 'Python "sinners" must REST')

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

Page 31: REST and some Python (or 'Python "sinners" must REST')

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

Page 32: REST and some Python (or 'Python "sinners" must REST')

Choose the representation(s) of the responseto be sent to client

consider existing standardized data formatse.g., HTML, Atom, JSON,…

Accept: application/json (Digg)/venue.json (Foursquare)

http://api.sciencemuseum.org.uk/exhibitions/?output=json

Page 33: REST and some Python (or 'Python "sinners" must REST')

interactive access to the data provided by a public Web service

http://developer.nytimes.com/

JSON response

Page 34: REST and some Python (or 'Python "sinners" must REST')

Integrate resources via URLs + Web forms

Page 35: REST and some Python (or 'Python "sinners" must REST')

Create use cases

specify exception/error conditionsvia HTTP status codes + additional information

200 OK, 303 See Other, 400 Bad Request, 404 Not Found,…http://httpstatus.es/

Page 36: REST and some Python (or 'Python "sinners" must REST')

“Never release an API without a versionand make the version mandatory.”—Brian Mulloy (2012)

http://feeds.delicious.com/v2/{format}/{username}?v=1.0 (Facebook)

Page 37: REST and some Python (or 'Python "sinners" must REST')

Some (programming) tools for developers?

http://www.weirdasianews.com/category/engrish/

Page 38: REST and some Python (or 'Python "sinners" must REST')

Python HTTP clients

classical built-in modules:httplib (Python 2)

http.client (Python 3)

Page 39: REST and some Python (or 'Python "sinners" must REST')

Python HTTP clients

a specific library:Unirest for Python – http://unirest.io/python.html

Page 40: REST and some Python (or 'Python "sinners" must REST')

Developing RESTful Web services in Python

one of our favorite frameworks:Bottle, CheeryPy, Django REST, Flask, web.py

Page 41: REST and some Python (or 'Python "sinners" must REST')

using a Python wrapper for a specific Web service/API:

www.pythonapi.com

Page 42: REST and some Python (or 'Python "sinners" must REST')

Useful Web resources for learning & making experiments?

Page 43: REST and some Python (or 'Python "sinners" must REST')

building a REST service:

Learn REST: A RESTful Tutorialwww.restapitutorial.com

Thoughts on RESTful API Designhttps://restful-api-design.readthedocs.org

Implementing RESThttps://code.google.com/p/implementing-rest/

Page 44: REST and some Python (or 'Python "sinners" must REST')

testing our/others APIs:

Make HTTP Requestshttp://www.hurl.it/

HTTP Request & Responsehttp://httpbin.org/

Apigee Consolehttps://apigee.com/console/

Page 45: REST and some Python (or 'Python "sinners" must REST')

don’t forget to consult:

ProgrammableWebhttp://www.programmableweb.com/

The Web engineer’s online toolboxivanzuzak.info/2012/11/18/the-web-engineers-online-toolbox.html

Page 46: REST and some Python (or 'Python "sinners" must REST')

R⚗ST(and some Python)

http://photos1.blogger.com/blogger/3043/2944/1600/woman-snake-1280.jpg