Python API Basics

I have set of imports that I like to have at the top of every API client I write, some are built in, but some are not. I’ll just quickly go over them before you get started on the fun stuff. This post is just to list off the modules you might need. If you’re on 64 bit OSes, you might need to take a look here for compatible packages, I know I did. For then entirety of the tutorials, I’ll be using Windows 7×64, with Python 2.7, so maybe you’ll have a different experience with Python 3, and on a different OS.*

First off, requests module is essential to everything we’ll be doing within the following tutorials. It handles all the hard-to-understand stuff, like POST, PUT, and GET requests, as well as cookies, so we don’t have to worry about it much at all; and ideal Python module.

Secondly, pprint is a nice thing to have, since we’ll be dealing a lot with dictionaries, and the regular print statement doesn’t print it off in the most readable of formats. Instead of printing everything in a single line, it linewraps, as well as uses separate lines where doing so would help with legibility. For example: a dictionary would be printed like this normally:

dictionary = {'a': 1, 'b': 2, 'c': 3}

but with pprint it’d look something more like this:

dictionary = {'a' : 1,
              'b' : 2,
              'c' : 3' }

which is amazing when you have huge dictionaries that you need to visually parse.

Thirdly, the json module is another essential one, as the APIs will often feed us JSON data. JSONs are essentially dictionaries writtien in Javascript. We’ll take the request data from the requests module, and feed it into the json.loads function and it’ll return a native python dict that we can manipulate like it’s not even a big deal. It’s pretty great.

Here’s an example of all three modules working together:

#imports
from pprint import pprint
import requests
import json

r = requests.get(r'http://www.reddit.com/user/tankorsmash/about/.json')
#print r.text  #raw text response as a string

j = json.loads(r.text)  #turn the json response into a python dict
#print j  #now it's a python dict

pprint(j)  #here's the final respone, printed out nice an readable format

There you have it, ┬áthe basic python modules I’ll be using throughout the next few tutorials.

*thanks to EuphoriaForAll for the Python Version inclusion here

Tagged , , , . Bookmark the permalink.

10 Responses to Python API Basics

  1. Justin says:

    Did not know about the pprint module, looks very helpful. Thanks!

    • Tankor Smash says:

      Happy to help man! It definitely makes debugging a ton easier with JSON files and other dicts. If you didn’t already know, you can import functions and objects under different names, so to make things a little easier on you, at the cost of some readability, you can can have:

       from pprint import pprint as pp

      So whenever you want to use the `pprint.pprint()` function, you can just call `pp()`.

  2. Mike says:

    Thank you for this intro – I’ve never heard of pprint before and it looks really nice!

    • Tankor Smash says:

      Hey, no problem at all, I enjoyed writing it out! Did you have any issues with the API tutorials, if you’ve checked em out?

  3. Hugh says:

    Thanks for this smashing tutorial!

  4. Joe Reynolds says:

    Keep the good stuff coming!
    For a newbie lacking things to do, this was refreshing :D

  5. Doug says:

    I can’t thank you enough for introducing me to the requests module. That’s some real quality-of-life improvement!

    By the way, you can do the JSON conversion with requests alone with: j = r.json()

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>