Skip to main content

Python client for the Openverse API

Project description

A thoroughly typed Python client for the Openverse API.

Repository PyPI - Version PyPI - Python Version

Installation

pip install openverse-api-client

Usage

The Openverse API client has a single external dependency on httpx. HTTPx was chosen because the precense of both a synchronous and asynchronous HTTP client from a single library, with nearly identical APIs, made it easy to generate the Python client using the generator.

from openverse_api_client import OpenverseClient, AsyncOpenverseClient

with OpenverseClient() as openverse:
    images = openverse.GET_v1_images(
        q="dogs",
        license="by-nc-sa",
        source=["flickr", "wikimedia"],
    )

with AsyncOpenverseClient() as async_openverse:
    audio = await async_openverse.GET_v1_audio(
        q="birds",
        license="by",
        source=["freesound"],
    )

Using the Openverse client as context managers will automatically close the underlying HTTPx client. Call close on the Openverse client to manually close the underlying HTTPx client when not using the context manager.

Shared HTTPx client session

If you already use HTTPx and utilised a shared client session, you may pass this to the Openverse client constructors.

from openverse_api_client import OpenverseClient
import httpx

httpx_client = httpx.Client()

openverse = OpenverseClient(
    httpx_client=httpx_client,
)

The same API applies for the asynchronous Openverse client, but requires httpx.AsyncClient instead.

When using a shared HTTPx client session do not call close on the Openverse client as this will close the shared HTTPx instance. Likewise, do not use the Openverse client context manager if passing in a shared client, the context manager will close your shared HTTPx client on context exit.

Authentication

By default, the clients will make unauthenticated requests. Pass client_id and client_secret to the client constructor to authenticate requests. The client automatically handles requesting tokens and token expiration.

from openverse_api_client import OpenverseClient

# The same API applies to the async client
authenticated_openverse = OpenverseClient(
    client_id="...",
    client_secret="...",
)

Alternative Openverse API instances

The clients reference the official production Openverse API instance by default, https://api.openverse.engineering. If you would like to send requests to a different API instance, pass base_url to the constructor:

from openverse_api_client import OpenverseClient

# The same API applies to the async client
localhost_openverse = OpenverseClient(
    base_url="localhost:50280",
)

Development

Please refer to the repository README

License

openverse-api-client is distributed under the terms of the GNU Lesser General Public License v3.0 or later license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openverse_api_client-0.0.1a3.tar.gz (15.5 kB view hashes)

Uploaded Source

Built Distribution

openverse_api_client-0.0.1a3-py3-none-any.whl (24.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page