Skip to content

What every client gives you

Every client stands on one core, so they work the same way. You learn one set of methods, one settings mechanism, one error hierarchy, and one paging model. Each client page then only has to tell you what its source does differently. This page lists what you can rely on in every client, and each linked page shows one item with examples from several clients.

The same methods on every resource

A client groups its API into resources. client.works on the Crossref client is one resource, and client.organizations on the ROR client is another. Every resource offers the methods in the table, and the quick start of each client lists the few resources where the API has no route for one of them.

Method Returns Stops at
get(id) one model a missing record raises NotFoundError
search(filters=None, *, query=None, ...) one Page with results and total one page per call
iterate(...) models, one at a time limit=, or the last page
collect(...) a list of models limit=, or the last page
first(...) one model or None the first match
count(...) an integer one small request
batch_get(ids) a dict keyed by id the last chunk

When an API has no route for a method, the call raises ConfigurationError before any request goes out. Europe PMC serves each article by id and has no route that returns many records at once, so a batch call fails at once with a message that names the fix:

from bibliofabric import ConfigurationError
from bibliofabric.europepmc import EuropePmcSyncClient

with EuropePmcSyncClient() as client:
    try:
        client.articles.batch_get(["MED:23903748"])
    except ConfigurationError as error:
        print(error)
ArticlesSyncResource.batch_get is disabled: the upstream API has no route that returns many records by id. Call get() per id.

Page through results and Search and filter cover these methods with their options.

Settings from the environment, a settings object, or arguments

CrossrefClient() with no arguments reads environment variables with the prefix CROSSREF_ and a .env file in the working directory. For settings that you want to change in code, pass a settings object, and for the contact address and the credentials, pass the keyword arguments of the constructor. A keyword argument wins over the environment:

from bibliofabric.crossref import CrossrefSettings, CrossrefSyncClient

settings = CrossrefSettings(max_retries=5)
with CrossrefSyncClient(settings, mailto="you@example.org") as client:
    print(client.settings.mailto, client.settings.max_retries)
you@example.org 5

Every settings class shares the same fields for timeouts, retries, and rate limits. Settings and credentials lists the fields and the credentials of every client.

Identifiers in the form you have them

get() accepts a bare identifier and its common URL form, so that you can pass a DOI copied from a browser. The normalizers in bibliofabric clean identifiers that arrive in other forms, such as an ORCID iD from a spreadsheet or an ISSN without its hyphen:

from bibliofabric import normalize_doi, normalize_issn, normalize_orcid

print(normalize_doi("https://doi.org/10.1038/NATURE12373"))
print(normalize_orcid("https://orcid.org/0000-0002-1825-0097"))
print(normalize_issn("20493630"))
10.1038/nature12373
0000-0002-1825-0097
2049-3630

Identifiers lists every normalizer and the identifier forms that each client accepts.

Typed models instead of dictionaries

Calls return Pydantic models, so work.primary_title is a string or None, work.is_referenced_by_count is an integer, and dates arrive as datetime objects. Your editor completes the attribute names, and a ValidationError tells you when the API answered with a field that the model doesn't know. Results and models shows what the models parse and how to serialize them.

Retries and typed errors

A client retries 429 and 5xx answers and network failures on its own, up to max_retries times, and waits as long as the API asks in Retry-After. When a call still fails, it raises one of the exceptions in bibliofabric, and each exception carries the request and the response. For a harvest over thousands of records, on_error="skip" sends a bad record to a failures list instead of stopping the loop. Errors, retries, and rate limits has the exception table and both patterns.

Async or sync, your choice

Every client has a sync twin with the same methods. Its name adds Sync before Client, so CrossrefClient becomes CrossrefSyncClient. Use the sync client in scripts and notebooks, and the async client when you run many requests at once. Async and sync clients shows both on one workflow.

Beyond one source

The clients compose, because each returns typed models with identifiers that the next client accepts. Use several clients together walks one DOI through Crossref, OpenAlex, and OpenCitations in a single script. Example scripts indexes the runnable scripts of every client.