Skip to main content

wikibase-rest-api-client

A client library for accessing Wikibase REST API. See the swagger UI for details of the API. Docs for the REST API can be found at doc.wikimedia.org.

Installation

PyPI version

sudo pip3 install wikibase-rest-api-client

Usage

See the tests for detailed examples of usage. For basic usage read on.

First, create a client:

from wikibase_rest_api_client import Client

# by default will connect to Wikidata at https://www.wikidata.org/w/rest.php/wikibase/v0/
client = Client()
# specify some other url to connect to
other_client = Client(base_url="https://api.example.com")

You probably should specify a User-Agent when constructing the client

from wikibase_rest_api_client import Client
client = Client(headers={"User-Agent": "my-agent/1.0.0"})

And you probably want to also have the client follow redirects (which will follow redirects between items when reading data)

from wikibase_rest_api_client import Client
client = Client(headers={"User-Agent": "my-agent/1.0.0"}, follow_redirects=True)

If the endpoints you're going to hit require authentication, use AuthenticatedClient instead. See the OAuth docs for how to get a token.

from wikibase_rest_api_client import AuthenticatedClient

client = AuthenticatedClient(token="SuperSecretToken")

Now call your endpoint and use your models:

from wikibase_rest_api_client.models import Item
from wikibase_rest_api_client.api.items import get_item
from wikibase_rest_api_client.types import Response

with client as client:
    # it's detailed because it responds with need more info than just the Item (e.g. status_code / headers)
    response: Response[Item] = get_item.sync_detailed('Q5', client=client)

Or do the same thing with an async version:

from wikibase_rest_api_client.models import Item
from wikibase_rest_api_client.api.items import get_item
from wikibase_rest_api_client.types import Response

async with client as client:
    response: Response[Item] = await get_item.asyncio_detailed('Q5', client=client)

By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.

client = AuthenticatedClient(
    base_url="https://internal_api.example.com", 
    token="SuperSecretToken",
    verify_ssl="/path/to/certificate_bundle.pem",
)

You can also disable certificate validation altogether, but beware that this is a security risk.

client = AuthenticatedClient(
    base_url="https://internal_api.example.com", 
    token="SuperSecretToken", 
    verify_ssl=False
)

Fluent usage

The library also offers a "fluent" API that is useful for many common operations. To use it you will first need to create a client as above.

from wikibase_rest_api_client.utilities.fluent import FluentWikibaseClient
from wikibase_rest_api_client import Client

inner_client = Client()
# defaults to English values
c = FluentWikibaseClient(inner_client)
item = c.get_item("Q5")
assert item.label == "human"
description = item.description
aliases = item.aliases

# see source for the format of item.statements

Advanced customizations

There are more settings on the generated Client class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying httpx.Client or httpx.AsyncClient (depending on your use-case):

from wikibase_rest_api_client import Client

def log_request(request):
    print(f"Request event hook: {request.method} {request.url} - Waiting for response")

def log_response(response):
    request = response.request
    print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")

client = Client(
    base_url="https://api.example.com",
    httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
)

# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()

You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):

import httpx
from wikibase_rest_api_client import Client

client = Client(
    base_url="https://api.example.com",
)
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
client.set_httpx_client(httpx.Client(base_url="https://api.example.com", proxies="http://localhost:8030"))

Metadata

Release files for wikibase-rest-api-client 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wikibase-rest-api-client 0.2.5
File Size Uploaded Provenance
wikibase_rest_api_client-0.2.5.tar.gz 27.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for wikibase-rest-api-client 0.2.5
File Interpreter ABI Platform
wikibase_rest_api_client-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 137.4 kB

Release files / wikibase_rest_api_client-0.2.5.tar.gz

Download URL wikibase_rest_api_client-0.2.5.tar.gz
Size 27.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c3a24caaf721af5cec72a5add0ef243ab098079e5212be8f3cef75a28c5cdaec
BLAKE2b-256 checksum
How to use checksums
2809979341d141e66a1952bf3c368367876f31039f82ae39e8f43f268eb28b5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.3 Darwin/24.5.0

Release files / wikibase_rest_api_client-0.2.5-py3-none-any.whl

Download URL wikibase_rest_api_client-0.2.5-py3-none-any.whl
Size 110.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96c6075a38458e78ae4b689b64848ee3c72f464c60814f6d67675f9a844de7c5
BLAKE2b-256 checksum
How to use checksums
239b2edaa801b9acb873f3346fa2ed736a3dac62808e2ad6e9d2d1f39513ecd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.3 Darwin/24.5.0

Security for wikibase-rest-api-client 0.2.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page