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

Post on 16-Apr-2017

3.000 views 0 download

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

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 servicea piece of s…oftware providing a s…pecific functionality

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

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

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 buildingRESTful 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 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

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 conditionsvia 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 versionand 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 Tutorialwww.restapitutorial.com

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

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

testing our/others APIs:

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

HTTP Request & Responsehttp://httpbin.org/

Apigee Consolehttps://apigee.com/console/

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

R⚗ST(and some Python)

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