CS 257: Software Design

Using APIs

You may work with a partner or alone. If you would like help finding a partner, either post publicly on #general in Slack, or contact me privately on Slack.

APIs via HTTP

The acronym API stands for Application Programming Interface, and refers to a mechanism by which one software entity can obtain a service from another software entity. For example, when your Java program wants to compute a square root, it might use the Math.sqrt method, which has an interface that we sometimes call a signature (e.g. public static double sqrt(double))...and can also be referred to as an API.

API is one of those terms that can be used for lots of different but related things. For example, you might refer to the signature of Math.sqrt as an API. Or you might refer to the collection of methods in the Math class as an API. Or you might refer to the entirety of the Java standard libraries as an API.

More recently, as APIs made available over the web have started to become popular among programmers, some people have started using API to refer only to such entities. Such is the fate of technical vocabulary. Sometimes it's precise, sometimes it's muddy.

This week, we're going to take a look at these latter types of APIs—the kind that you can access via HTTP. An easy example is my linguistic API that I demonstrated in class the other day. By pointing your browser at a particularly formatted URL, you can conjugate verbs, look up word translations, pluralize nouns, find root words, etc. in about a dozen languages. The resulting output is in a machine-readable form (JSON) unless you specifically request human-readable results (HTML). That machine-readable output is particularly interesting, because it allows other software developers to use this API to help build new services.

Our goal will be to learn more about the structure of HTTP-based APIs, and to learn how to make use of them from Python programs.

Step 1: Find two or three APIs that interest you

Your first job is to search the internet for a few APIs that you'd like to play with. Generic search terms (e.g. "cool APIs") can get you started, but you should also spend a little time thinking about what kind of data interests you, and who might have that data. I love poking around in census data—and indeed, the US Census Bureau has APIs. I also like baseball statistics—does mlb.com have an API? How about baseball-reference.com? Think about your interests, and refine your searches accordingly. Find us something interesting!

Some APIs require you to pay for a subscription. Feel free to include those in your report, but make sure you identify at least one free-to-use API. (Some require you to create an account but don't require you to pay for basic service—that's OK.)

Step 2: learn about REST

Representational State Transfer (REST) is a...philosophy? architecture? buzzword that's widely misused?.... Go out on the web and see if you can get a rough idea, and come to class Wednesday ready to discuss it.

Step 3: write a short program accessing your favorite API

Write a Python program called api-test.py that gives the user the ability to perform two simple operations from the command line using one of your APIs: (1) get a list of things, and (2) get details about a single thing. For example, suppose you were using an API about the US Senate (which could be a valuable API regardless of how you might be feeling about the Senate right now). You might allow the user to (1) get a list of all senators and (2) given a senator's name (or ID number, or whatever the API uses to uniquely identify a senator), print detailed information about that senator.

Want to get fancy? Try giving your user a little more control, like "show me all the Democratic senators" or "show me details on both senators from Michigan." This fanciness is not necessary and will not improve your grade. Add features like that only after you've done the "list plus details" above, and only if you want to get more experience with accessing the API.

In addition to doing its work, your program should print a usage statement describing the options it offers to your user. You may write your own command-line parsing code using sys.argv as we did in phase 1 of the books assignment, or you might want to explore command-line parsing with the argparse module from the standard Python libraries.

You can use my api-test.py sample as a model.

What to hand in