Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Larry cai <larry.caiyu@gmail.com>
Agenda








What is REST API ?
Exercise 1: use curl to play with REST API
Exercise 2: use requests module to GET...
REST API



REST = REpresentation State Transfer
REST vs. SOAP
XML vs. JSON



REST/JSON is first choice to use ! (twitt...
Environment


Python 2.7.x In Windows with Git Bash




4

http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi
htt...
Exercise 1: using curl


Try to get the data in browser and command using cURL
http://httpbin.org/get

$ export http_prox...
Exercise: use urllib2 module



SKIP this, it wastes time

6

Learn REST API with Python in 90 minutes
Requests Module




Python’s standard urllib2 module provides most of the
HTTP capabilities you need, but the API is
tho...
Exercise 2: use requests module


Install requests module (could use pip)
http://docs.python-requests.org/en/latest/user/...
JSON format and usage in python


JSON (JavaScript Object Notation) is a lightweight datainterchange format. It is easy f...
Exercise 3: write in python script


Download exercise code restapi.py from github
NOTE: this is prepared script




ht...
Exercise 4: HTTPS & Authentication


HTTPS (SSL certification)
$ curl https://api.github.com/users/larrycai
=> requests.g...
Exercise 5: POST data


Upload with POST method
curl -X POST -d
'{"public":true,"files":
{"test.txt":{"content":"String
f...
Summary



Requests module is preferred to use in python
Try browser/cURL before script to access API
REST/JSON is good m...
Exercise 6 : Homework


Implement for all gist API



Use oauth to access github in python script



Get all jenkins jo...
Reference




Understand REST API: http://www.restapitutorial.com/
Requests python module: http://www.python-requests.o...
Upcoming SlideShare
Loading in …5
×

Learn REST API with Python

42,080 views

Published on

Published in: Technology
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download Full doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download PDF EBOOK here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m6jJ5M } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy &amp; Proven Way to Build Good Habits &amp; Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Can I watch this presentation somewhere?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Learn REST API with Python

  1. 1. Larry cai <larry.caiyu@gmail.com>
  2. 2. Agenda        What is REST API ? Exercise 1: use curl to play with REST API Exercise 2: use requests module to GET headers Exercise 3: write in python script Exercise 4: HTTPS & Basic Authentication towards Github Exercise 5: POST your script into gist Summary & Homework & Reference 2 Learn REST API with Python in 90 minutes
  3. 3. REST API  REST = REpresentation State Transfer REST vs. SOAP XML vs. JSON  REST/JSON is first choice to use ! (twitter, jenkins…)   See slides http://www.slideshare.net/rmaclean/json-and-rest for detail Or "Beautiful REST + JSON APIs" by Les Hazlewood http://www.youtube.com/watch?v=hdSrT4yjS1g 3 Learn REST API with Python in 90 minutes
  4. 4. Environment  Python 2.7.x In Windows with Git Bash    4 http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi http://git-scm.com/downloads Add into Path Learn REST API with Python in 90 minutes
  5. 5. Exercise 1: using curl  Try to get the data in browser and command using cURL http://httpbin.org/get $ export http_proxy= $ export https_proxy= $ curl –v http://httpbin.org/get 5 Learn REST API with Python in 90 minutes
  6. 6. Exercise: use urllib2 module  SKIP this, it wastes time 6 Learn REST API with Python in 90 minutes
  7. 7. Requests Module   Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. Python HTTP: When in doubt, or when not in doubt, use Requests. Beautiful, simple, Pythonic. http://www.python-requests.org 7 Learn REST API with Python in 90 minutes
  8. 8. Exercise 2: use requests module  Install requests module (could use pip) http://docs.python-requests.org/en/latest/user/install/#install  Use it interactive mode $ python >>> import requests >>> r = requests.get("http://httpbin.org/get") 8 Learn REST API with Python in 90 minutes
  9. 9. JSON format and usage in python  JSON (JavaScript Object Notation) is a lightweight datainterchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.  >>> print r.json()  Python JSON module >>>import json >>>data[“firstName”] “John” http://www.slideshare.net/rmaclean/json-and-rest 9 Learn REST API with Python in 90 minutes
  10. 10. Exercise 3: write in python script  Download exercise code restapi.py from github NOTE: this is prepared script   https://gist.github.com/larrycai/7823499 Implement tasks in method exer3()  print result text, status_code for http://httpbin.org/get print r.status_code    Print “origin” and “User-Agent” (JSON) (use r.json()) Enable debug by calling enable_debug() Execute command to verify $ ./restapi.py –e 3 10 Learn REST API with Python in 90 minutes
  11. 11. Exercise 4: HTTPS & Authentication  HTTPS (SSL certification) $ curl https://api.github.com/users/larrycai => requests.get(…,verify=False)  Authentication (Basic, Digest, OAuth), use curl command $ curl –v –u larrycai https://api.github.com/user > Authorization: Basic bGFycnljYWk6TTFkZUBMMWZl => HTTP Basic Auth => requests.get(…,auth = HTTPBasicAuth(user, passwd))  Tasks:   Use curl to try your own account Use script get your account’s “created_at” http://developer.github.com/v3/ 11 Learn REST API with Python in 90 minutes
  12. 12. Exercise 5: POST data  Upload with POST method curl -X POST -d '{"public":true,"files": {"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists  >>> import json >>> json.dumps(['foo', {'bar': ('baz', 1.0, 2)}]) '["foo", {"bar": ["baz",1.0, 2]}]' >>> print json.dumps(""foobar") ""foobar“  Task:  Upload your current restapi.py to YOUR gist http://developer.github.com/v3/gists/#create-a-gist 12 Learn REST API with Python in 90 minutes
  13. 13. Summary  Requests module is preferred to use in python Try browser/cURL before script to access API REST/JSON is good marriage  Coding and learn from others   13 Learn REST API with Python in 90 minutes
  14. 14. Exercise 6 : Homework  Implement for all gist API  Use oauth to access github in python script  Get all jenkins jobs in python script  Submit your final result into gist, then give me email, you may get gift ;-) 14 Learn REST API with Python in 90 minutes
  15. 15. Reference    Understand REST API: http://www.restapitutorial.com/ Requests python module: http://www.python-requests.org Github developer API v3   http://developer.github.com/v3/ Jenkins API  15 https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API Jenkins Scriptler in 90 minutes

×