Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Code Camp - Consuming APIs

Python Code Camp - Consuming APIs

A demo on RESTful APIs and consuming Twitter API v1.1 and using requests library

Ronald Cheung

July 05, 2014
Tweet

More Decks by Ronald Cheung

Other Decks in Technology

Transcript

  1. Objectives • Quick introduction to API concepts • Interactive walkthrough

    to build a simple app to consume Twitter’s API • Launchpad to subsequent talks
  2. Workshop exercise / Demo 1. VirtualEnv 2. Python 2.7 3.

    https://github.com/sixohsix/twitter 4. requests 5. json
  3. API Basics • (API) Application Programming Interface • An interface

    that defines how software components interact with each other • Generically, this can refer to any software Function definition, Object Definition ( in OOP )
  4. API Basics • In web development APIs refer to “Web

    Services” with some or all of the following properties: - Request / Response messaging protocol - HTTP(s) - Object Serialization / message payload ( JSON, XML ) - Authentication ( Basic / OAuth / OAuth 2.0)
  5. API Basics • Trend as per “Web 2.0” movement away

    from SOAP (Simple Object Access Protocol) / SOA ( Service Oriented Architecture) • To RESTful ( Representational State Transfer ) services and Resource Oriented Architectures.
  6. RESTful web services • RESTful is an API design philosophy

    that adheres to the principles: 1. Statelessness - Each request is independent and contains all information for server to return a response - Server does not need to keep track of previous requests (state) in memory. Any state management is the responsibility of the client. - Simpler to implement and scale
  7. RESTful web services 2. Adheres to HTTP Methods - HTTP

    methods POST, GET, PUT, DELETE corresponds to CRUD ( Create, Read, Update, Delete ) POST → Create GET → Read PUT → Update DELETE → Delete In other web service designs GET and POST used interchangably to Create,Read, Update, Delete ; PUT and DELETE rarely used
  8. RESTful web services 3. URIs are designed like directories API

    endpoints are intuitive and human readable For Example http://api.devcon.ph/events/<year>/<month>/ GET http://api.devcon.ph/events/2014/07/ returns a list of devcon events for July 2014
  9. Endpoint Client RESTful web services 4. The state of the

    Resources are serialized to JSON or XML and transferred to the client User: Name: …? Address: …? Telephone: …? User: Name: Jane Address: Ortigas Telephone: 092398 {“Name”: “Jane”, “Address”: “Ortigas”, “Telephone”: “092398” }
  10. RESTful web services RESTful APIs allows modern web apps to

    be a “mashup” of combinations of different APIs My Awesome Application Faceb ook Google Maps Twitter Fours quare Twilio AWS S3 Instag ram
  11. Installing VirtualEnv ~$ sudo apt-get install build-essential ~$ sudo easy_install

    virtualenv ~$ sudo easy_install virtualenvwrapper ~$ sudo easy_install pip
  12. VirtualEnv Create VirtualEnv: ubuntu@ubuntu:~/workspace/devcon$ mkvirtualenv twitterenv New python executable in

    twitterenv/bin/python Installing setuptools............done. Installing pip............... done. virtualenvwrapper .user_scripts creating /home/ubuntu/Virtualenv/twitterenv/bin/predeactivate virtualenvwrapper .user_scripts creating /home/ubuntu/Virtualenv/twitterenv/bin/postdeactivate virtualenvwrapper .user_scripts creating /home/ubuntu/Virtualenv/twitterenv/bin/preactivate virtualenvwrapper .user_scripts creating /home/ubuntu/Virtualenv/twitterenv/bin/postactivate virtualenvwrapper .user_scripts creating /home/ubuntu/Virtualenv/twitterenv/bin/get_env_details (twitterenv)ubuntu @ubuntu:~/workspace/devcon$
  13. Authentication & OAuth 1.0 / 2.0 • Since Twitter API

    version 1.1 all requests require authentication • We will be using a client to avoid implementing the OAuth dance ourselves • https://dev.twitter.com/docs/api
  14. Install Twitter client (twitterenv)ubuntu@ubuntu :~/workspace/devcon$ pip install twitter Downloading/unpacking twitter

    Downloading twitter -1.14.3.tar.gz Running setup.py egg_info for package twitter Installing collected packages : twitter Running setup.py install for twitter Installing twitter -log script to /home/ubuntu/Virtualenv/twitterenv/bin Installing twitter script to /home/ubuntu/Virtualenv/twitterenv/bin Installing twitterbot script to /home/ubuntu/Virtualenv/twitterenv/bin Installing twitter -follow script to /home/ubuntu/Virtualenv/twitterenv/bin Installing twitter -stream-example script to /home/ubuntu/Virtualenv/twitterenv/bin Installing twitter -archiver script to /home/ubuntu/Virtualenv/twitterenv/bin Successfully installed twitter Cleaning up...
  15. Create an instance of the Twitter app Create a new

    Twitter App https://dev.twitter.com/apps/new This will authenticate and authorize your client to the Twitter API
  16. Running the client (twitterenv)ubuntu@ubuntu:~ /workspace/devcon$ python twitterclient.py Hi there! We're

    gonna get you all set up to use My App Name. In the web browser window that opens please choose to Allow access. Copy the PIN number that appears on the next page and paste or type it here: Opening: https://api.twitter.com/oauth/authorize? oauth_token=vNgKqIkvgL4Ci0WKWLf4trB73rA2KGgNJsdsVYmGpLY Please enter the PIN: Created new window in existing browser session.
  17. Calling the API ourselves In Python there is no shortage

    of http modules: - urllib, urllib2, httplib2, pycurl - This is due to a varied number of historical reasons but we will be using “Requests” as the most concise HTTP library to consume APIs http://docs.python-requests.org/en/latest/
  18. Installing requests (twitterenv)ubuntu@ubuntu :~/workspace/devcon$ pip install requests Downloading/unpacking requests Downloading

    requests -2.3.0.tar.gz (429Kb) : 429Kb downloaded Running setup.py egg_info for package requests Installing collected packages : requests Running setup.py install for requests Successfully installed requests Cleaning up...
  19. Sample output It was gloomy.Pose 4 #independencearmy @beachyogagirl @kinomacgregor @laurasykora17

    @glyderapparel… http: //t.co/jda7wQHmRV {"probability": {"neg": 0.62190366100043493 , "neutral": 0.39167237412434108 , "pos": 0.37809633899956507 }, "label": "neg"} Agile Product Ownership in a Nutshell https://t.co/ERh1TOeUcG {"probability": {"neg": 0.50765398516442639 , "neutral": 0.37258333409854316 , "pos": 0.49234601483557361 }, "label": "neg"} RT @zisheerin: Smoochy tries out the staple drink of hackers in Berlin #clubmate #spotsmoochy @jfdiasia @serrynaimo http: //t.co/msAs4NzNV2 {"probability": {"neg": 0.5526367605376894 , "neutral": 0.74636832316062796 , "pos": 0.4473632394623106 }, "label": "neutral"} A look at #July4th traditions across the US http://t.co/IUD62aZkRv #ProudAmerican http://t.co/qwRKddg4DL {"probability": {"neg": 0.27844778547153093 , "neutral": 0.55088901544258118 , "pos": 0.72155221452846907 }, "label": "neutral"}
  20. WorkShop Ideas 1. Get tweets from Twitter, and persist them

    to disk using sqlalchemy. Read a list of Tweets from sqlalchemy 2. Try to integrate with other APIs: Foursquare, Google Maps, Instagram, Facebook
  21. Questions? About the speaker: • Python Programmer • @ronaldlcheung •

    http://sg.linkedin.com/in/ronaldlcheung • Startup founder http://www.save22.com.ph/