Lab 22 — Working with APIs
In this lab you will practice using the requests library to work with public APIs. Start by downloading lab22.zip.
Fruityvice
For this part of the lab, you will work with the Fruityvice API. You can practice with the API on its home page. You should read the documentation so you can see how to use the API and how to interpret the data it returns, which is in JSON format. The documentation includes numerous examples.
Write your code in fruityvice.py
. The code includes a useful constant for you:
HOSTNAME = 'https://fruityvice.com/'
It also includes an error printing function:
def print_error(url, code):
print(f"Error number {code} occurred fetching {url}")
When using the API, you should check if the return code is equal to 200. If it is not, use this function to print an error and then return, unless the instructions tell you to do something different.
All Fruits
Write a function called get_all_fruits()
. This function prints the names of
all of the fruits in the system. The API endpoint to get this data is:
/api/fruit/all
One Fruit
Write a function called get_fruit_info(fruit)
. This function takes one
parameter:
fruit
: a fruit name, either a string, such as ‘banana’, or an ID, such as ‘2’
The function prints information about the fruit, such as:
Banana has 96 calories and 17.2 grams of sugar
The API endpoint to get this data is:
/api/fruit/:fruit
where :fruit
is the name of a fruit or an ID of a fruit that is in the system.
If the fruit does not exist in the system, then print:
Sorry, that fruit is not in the system
Running the program
You can run the program from a terminal using:
python fruityvice.py
By default, the program will print the names of all the fruits. You can use the following flags:
--fruit FRUIT
: list information about a specific fruit--help
: get help on how to run the program
Star Wars
For this part of the lab, you will work with the Star Wars API. You can practice with the API on its home page. You should read the documentation so you can see how to use the API and how to interpret the data it returns, which is in JSON format. The documentation includes numerous examples.
Write your code in starwars.py
. The code includes a useful constant for you:
BASE = 'https://swapi.dev/api'
It also includes an error printing function:
def print_error(url, code):
print(f"Error number {code} occurred fetching {url}")
When using the API, you should check if the return code is equal to 200. If it is not, use this function to print an error and then return, unless the instructions tell you to do something different.
Planet
Write a function called print_planet(planet_id)
. It takes one parameter:
planet_id
: the ID for a planet (a string)
The function prints information about a planet, including its name, population, climate, and terrain:
Planet: Alderaan, population: 2,000,000,000, climate: temperate, terrain: grasslands, mountains
The API endpoint to get this data is:
/planets/:planet
where :planet
is the ID of a planet that is in the system.
Starship
Write a function called print_starship(starship_id)
. It takes one parameter:
spaceship_id
: the ID for a spaceship (a string)
The function prints information about a starship, including its name, model, hyperdrive rating, number of passengers, number of crew, and length:
Starship: CR90 corvette, model: CR90 corvette, hyperdrive rating: 2.0, passengers: 600, crew: 30-165, length: 150
The API endpoint to get this data is:
/starship/:starship
where :starship
is the ID of a planet that is in the system.
Planet Residents
Write a function called print_planet_residents(planet_id)
. It takes one
parameter:
planet_id
: the ID for a planet (a string)
The function prints the names of the residents of a planet:
Residents of Alderaan:
Leia Organa
Bail Prestor Organa
Raymus Antilles
The API endpoint to get this data is:
/planets/:planet
where :planet
is the ID of a planet that is in the system. Inside the return
response is a list of URLs for the residents.
People in a Species
Write a function called print_people_in_species(species_id)
. It takes one
parameter:
species_id
: the ID for a species (a string)
The function prints the names of the people in a species:
People in Droid species:
C-3PO
R2-D2
R5-D4
IG-88
The API endpoint to get this data is:
/species/:species
where :species
is the ID of a species that is in the system. Inside the return
response is a list of URLs for the people.
Print Planets (Optional)
Write a function called print_planets()
. This function prints all the planets
in the system, along with their IDs.
The API endpoint to get this data is:
/planets/
The planets are divided into “pages” of results. Inside the return response is a
key called next
that returns either a URL (for the next page) or None. You
will need a while loop to fetch these.
Running the program
You can run the program from a terminal using:
python starwars.py
By default, the program doesn’t print anything. You can use the following flags:
--planet PLANET
: list information about a specific planet--starship STARSHIP
: list information about a specific starship--people_on_planet PLANET
: list the names of people who live on a planet--people_in_species SPECIES
: list the names of people who are in a species--planets
: list all of the planets--help
: get help on how to run the program
Lessons
What we want you to get from this lab:
-
You understand how to use the requests library to make basic requets for information on the Internet
-
You can use the requests library to call an API and process the returned JSON data
-
You can figure out what went wrong when something unexpected happens
-
Hopefully you had fun!
Points
Turn in a zip file that has your code.
Task | Description | Points |
---|---|---|
Fruityvice | ||
All Fruits | Your solution works | 1 |
One Fruit | Your solution works | 1 |
Star Wars | ||
Planet | Your solution works | 2 |
Starship | Your solution works | 2 |
Planet Residents | Your solution works | 2 |
People in a Species | Your solution works | 2 |