A Library of Your Own for an API
Choose a service with an API, build a small library for it with an AI tool, test it against the real service and keep it in your portfolio.
Last updated:
On this page we choose a service with an API, build a small library for it with an AI tool, check that it works against the real service, and prepare it for a portfolio.
What a library for an API does
In the lesson on the API we saw wikipedia-api: a few lines in place of a request that we wrote by hand, with an endpoint, parameters, a status code and a loop over continue.
The library adds nothing new to the service. It wraps the repeated requests in functions with clear names, so a program that uses it writes search('fpga') and not the address, the parameters and the path in the answer.
The task: build such a library for a service that we did not study, check that it works, and keep it in a repository of your own.
With an AI tool the code can be written fast. What stays in your hands is the decisions (which service, which functions) and the verification:
that code that looks right does what the service does.
Choosing a service
The task is to build a library for a service that has no existing library in Python, so the table holds only such services.
The services in the table answer requests without a personal key, according to their documentation.
A search in PyPI and in the repositories of GitHub found no known library for any of them, but a search does not prove that none exists:
before you start, search for the name of the service with the word python in both places yourselves.
Read the documentation of the service you choose before anything else, and check its terms:
whether a key is required, the rate limit, and the licence of the data.
| Service | What it gives |
|---|---|
| The Metropolitan Museum of Art Collection API | search for works of art and details of each work: artist, period, image |
| Art Institute of Chicago API | search for works, artists and exhibitions |
| Cleveland Museum of Art Open Access API | works of art with details and images |
| Library of Congress (loc.gov) | search in the library’s items: books, maps, photographs and manuscripts |
The terms of each service, according to its documentation when this was written:
- The Met: no key, and the documentation asks not to exceed 80 requests per second. The data is under CC0, and images are available for works in the public domain (
isPublicDomain). - The Art Institute of Chicago: no key, and up to 60 requests per minute for an anonymous user. The documentation asks to add an
AIC-User-Agentheader with the name of the project and a contact address, and to work in one thread with a pause between requests. The data is under CC0, except the fielddescription, which is under CC-BY. - The Cleveland Museum of Art: no key. The data is under CC0, and CC0 images are available only for works whose
share_license_statusisCC0. - The Library of Congress: no key, and the service limits the rate of requests and blocks requests that exceed it. Paging past the 100,000th result is not supported. The rights of each item are written in its record, so check them before using the content.
Before writing a library, send one request by hand with requests, as in the lesson:
an endpoint, parameters, a check of the status code and a read of the JSON.
If that request does not work, a library will not work either.
What the library offers
A first library has three to five functions: a search, getting one item by its identifier, and sometimes a list by topic.
Before asking the tool for code, write in words:
- what each function takes and what it returns (a text, a list or a dictionary);
- what happens when the item is not found: a clear error or an empty value;
- what happens when the service answers with a status code that is not 200, and in particular 429.
This is the skeleton of such a library. The code is shown and not run, because the boxes of this page cannot reach external services, and it is written as an example and was not tested against the service:
import requests
class OpenLibrary:
base = 'https://openlibrary.org/'
def __init__(self, user_agent):
self.headers = {'User-Agent': user_agent}
def _get(self, path, params):
answer = requests.get(
self.base + path,
params=params,
headers=self.headers,
timeout=30,
)
answer.raise_for_status()
return answer.json()
def search(self, text, limit=5):
data = self._get(
'search.json', {'q': text, 'limit': limit},
)
return [book['title'] for book in data['docs']]
The function _get is the only place that sends requests: the address, the user agent, the longest wait and the check of the status code are there.
Every public function calls it, so a fix in one place fixes them all.
Working with an AI tool
The tool writes fast, so the value of your work is in the steps around it:
- Give the tool the documentation of the service and the description of the functions from the previous section, and ask first for a plan: which files and which functions.
- Ask for the code, then read it line by line. Ask the tool about every line that is not clear.
- Ask for a file of tests, and run it yourself.
- Run the library against the real service and compare the result with what you see in the browser or in the documentation.
An example of a request to the tool
“Here is the documentation of the service (pasted). Write a library in one file, named books.py, with one class and two functions:
a search by text and getting one book by its identifier. Every request goes through one function that checks the status code and sends a user agent.
First show a plan, and write the code only after I approve it.”
Tests that really test
A test that always passes tests nothing. Two ways to make sure that your tests really work:
- Run against the real service, with few requests and pauses, and check that the result is correct.
- Break it on purpose. Spoil a line in the code, for example delete the check of the status code, and check that at least one test fails. If no test failed, the tests do not check what you thought.
Tests that run on a saved answer, like the copies in the lesson, save requests to the service.
The saved answer must be a real answer, saved once without change.
Before putting it in a portfolio
- A README file: what the library does, how to install it, and an example of a few lines.
- A licence: a clear licence in the repository, for example MIT, and the licence of the service’s data.
- Cleaning: check that the files hold no keys, passwords, paths of your computer or personal details. AI tools sometimes leave real values in examples.
- Politeness: a user agent that identifies you, few requests with pauses, and the source of the data when the service asks for it.
Contributing to an existing project
If a library for the service already exists, you can contribute to it instead of building a new one:
a fix, an example or documentation. First open an issue and ask, and read the contribution guidelines of the project, including its policy on code written with an AI tool:
some projects ask to state the use of a tool, and some do not accept such contributions.
Keep in mind
- A library for an API wraps repeated requests in functions with clear names, and sends all of them through one function.
- The tool writes the code, and you decide which functions and verify them against the real service.
- A test counts only if it fails when the code is spoiled.
- Before putting it in a portfolio: a README, a licence, cleaning of personal details and politeness toward the service.