Lab: using HTTP-based APIs
Nothing to hand in
I recommend doing the coding part of this with a partner for fun and to have a sounding board as you do the work.
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 will demonstrate for you in a video or in class. 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—JavaScript Object Notation) 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.
Your goal for this lab will be to learn more about the structure of HTTP-based APIs, and to learn how to make use of them from Python programs.
Your tasks
- Play around with the Ultralingua API (ULAPI) for a couple minutes to get the idea of what the requests and responses look like. (We're using my API instead of one of the thousands of others out there in the world, by the way, so that if you accidentally crash my server, I won't get in trouble with whoever owns the server.) (Also, I really want to do version 3.0 of this API to simplify the output, but that's way down my priority list, so I hope you'll forgive its slightly messy output.)
- Read the using-apis/api-test.py sample program from my GitHub repo for this class. Pay attention to how it goes about constructing and sending requests to ULAPI, and how it receives and works with the response.
- Adapt my sample program to make requests of this COVID-19 data API (even though they stopped collecting new data for the API months ago). See, for example, if you can write a version of this program that will take a state abbreviation (e.g. MN, WA, etc.) as a command-line argument and print out the most recent date's new COVID-19 cases and deaths for that state. Play with this until you're comfortable with the Python mechanisms for making requests and using responses. (Note that this API has stopped collecting new data as of last March, but it should still work for earlier dates.)
- As always, ask questions!