Lesson 09: Web APIs, pip, the requests module, and the Star Wars API
This week you will learn how to connect over HTTP to a web application programming interface (API)
in order to retrieve resources (i.e., data) serialized as Javascript Object Notation (JSON). You
will use pip
, a Python package manager, to install the requests
module in order to simplify
communicating with the Star Wars API
endpoint.
Readings
Below are a set of readings that you should consider reviewing. Note that the Real Python articles referenced below may require signing up for a free account.
1.0 Required
Arnaud Lauret, The Design of Web APIs (Manning, 2019).
Chapter 1 provides a high-level overview of the purpose of APIs with useful analogies. Read only the following sections and subsections:
- 1.1 What is an API?
- 1.1.1 An API is a Web Interface for Software
- 1.1.2 APIs turn Software into LEGO® Bricks
- 1.2 Why API Design matters
- 1.2.1 A Public or Private API is an Interface for other Developers
- 1.2.2 An API is made to hide the Implementation
Philipp Acsany, Using Python's pip to Manage Your Projects' Dependencies (RealPython, Sept 2024).
You will be installing your first Python package this week. Isaac’s article describes how to use
pip
, a package manager that comes bundled with Python 3.
Adam Snyder, Crash Course in HTTP Requests Using Python (Medium, May 2019).
Quick read that covers the absolute basics of communicating with an API over HTTP in order to retrieve data.
Alex Ronquillo, Python's Request Library (Guide) (RealPython, Jan 2019).
Alex’s article provides a solid overview of the requests module. Concentrate on the following sections and subsections:
- Getting started with requests
- The GET Request
- The Response
- Status Code
- Query String Parameters
2.0 Recommended
Shif Ben Avraham, What is REST — A Simple Explanation for Beginners, Part 1: Introduction (Medium, Sept 2017).
Shif Ben Avraham, What is REST — A Simple Explanation for Beginners, Part 1: Introduction (Medium, Sept 2017).
The terms Web API and REST API are often used interchangeably. However, not all Web APIs are REST APIs. A RESTful API is an API whose design is influenced by a set of principles articulated by Roy Fielding. Shif outlines the style in two parts.
Philipp Acsany, Working With JSON Data in Python (Real Python, Jul 2024).
Comprehensive guide to working with JSON data in Python.
3.0 Reference
Kenneth Reitz, Requests: HTTP for Humans (A Kenneth Reitz Project).
Official documentation for the Python requests module. Review the Quickstart guide. Familiarize yourself (minimally) with making requests, passing parameters in URLs, and JSON response content.
The Star Wars API, Documentation.
Familiarize yourself with the resources that you can request from the Star Wars API (SWAPI). You will be working with SWAPI JSON representations of Star Wars planets, people, species, starships, and vehicles from now until the end of the semester.