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

10 thoughts on “Python API Basics”

    1. 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()`.

    1. 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?

  1. 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.