wikibase-rest-api-client


Namewikibase-rest-api-client JSON
Version 0.1.3 PyPI version JSON
download
home_page
SummaryA client library for accessing the Wikibase REST API
upload_time2024-01-26 06:51:02
maintainer
docs_urlNone
authorDaniel Erenrich
requires_python>=3.8,<4.0
licenseMIT
keywords wikibase wikidata rest-api client
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # wikibase-rest-api-client
A client library for accessing Wikibase REST API. See [the swagger UI](https://doc.wikimedia.org/Wikibase/master/js/rest-api/) for details of the API. Docs for the REST API can be found at [doc.wikimedia.org](https://doc.wikimedia.org/Wikibase/master/php/repo_rest-api_README.html#autotoc_md670).



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

First, create a client:

```python
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

```python
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)


```python
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](https://www.wikidata.org/wiki/Wikidata:REST_API/Authentication#Setting_up_OAuth_2.0) for how to get a token.

```python
from wikibase_rest_api_client import AuthenticatedClient

client = AuthenticatedClient(token="SuperSecretToken")
```

Now call your endpoint and use your models:

```python
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:

```python
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.

```python
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**.

```python
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.

```python
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):

```python
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):

```python
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"))
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "wikibase-rest-api-client",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "wikibase,wikidata,rest-api,client",
    "author": "Daniel Erenrich",
    "author_email": "daniel@erenrich.net",
    "download_url": "https://files.pythonhosted.org/packages/46/60/3b0a2b491c764352110adc77f0da150914e1d5d2f78c2f99d73091ebcb7d/wikibase_rest_api_client-0.1.3.tar.gz",
    "platform": null,
    "description": "# wikibase-rest-api-client\nA client library for accessing Wikibase REST API. See [the swagger UI](https://doc.wikimedia.org/Wikibase/master/js/rest-api/) for details of the API. Docs for the REST API can be found at [doc.wikimedia.org](https://doc.wikimedia.org/Wikibase/master/php/repo_rest-api_README.html#autotoc_md670).\n\n\n\n## Usage\nSee the tests for detailed examples of usage. For basic usage read on.\n\nFirst, create a client:\n\n```python\nfrom wikibase_rest_api_client import Client\n\n# by default will connect to Wikidata at https://www.wikidata.org/w/rest.php/wikibase/v0/\nclient = Client()\n# specify some other url to connect to\nother_client = Client(base_url=\"https://api.example.com\")\n```\n\nYou probably should specify a User-Agent when constructing the client\n\n```python\nfrom wikibase_rest_api_client import Client\nclient = Client(headers={\"User-Agent\": \"my-agent/1.0.0\"})\n\n```\n\nAnd you probably want to also have the client follow redirects (which will follow redirects between items when reading data)\n\n\n```python\nfrom wikibase_rest_api_client import Client\nclient = Client(headers={\"User-Agent\": \"my-agent/1.0.0\"}, follow_redirects=True)\n\n```\n\nIf the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead. See [the OAuth docs](https://www.wikidata.org/wiki/Wikidata:REST_API/Authentication#Setting_up_OAuth_2.0) for how to get a token.\n\n```python\nfrom wikibase_rest_api_client import AuthenticatedClient\n\nclient = AuthenticatedClient(token=\"SuperSecretToken\")\n```\n\nNow call your endpoint and use your models:\n\n```python\nfrom wikibase_rest_api_client.models import Item\nfrom wikibase_rest_api_client.api.items import get_item\nfrom wikibase_rest_api_client.types import Response\n\nwith client as client:\n    # it's detailed because it responds with need more info than just the Item (e.g. status_code / headers)\n    response: Response[Item] = get_item.sync_detailed('Q5', client=client)\n```\n\nOr do the same thing with an async version:\n\n```python\nfrom wikibase_rest_api_client.models import Item\nfrom wikibase_rest_api_client.api.items import get_item\nfrom wikibase_rest_api_client.types import Response\n\nasync with client as client:\n    response: Response[Item] = await get_item.asyncio_detailed('Q5', client=client)\n```\n\nBy 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.\n\n```python\nclient = AuthenticatedClient(\n    base_url=\"https://internal_api.example.com\", \n    token=\"SuperSecretToken\",\n    verify_ssl=\"/path/to/certificate_bundle.pem\",\n)\n```\n\nYou can also disable certificate validation altogether, but beware that **this is a security risk**.\n\n```python\nclient = AuthenticatedClient(\n    base_url=\"https://internal_api.example.com\", \n    token=\"SuperSecretToken\", \n    verify_ssl=False\n)\n```\n\n\n## Fluent usage\n\nThe 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.\n\n```python\nfrom wikibase_rest_api_client.utilities.fluent import FluentWikibaseClient\nfrom wikibase_rest_api_client import Client\n\ninner_client = Client()\n# defaults to English values\nc = FluentWikibaseClient(inner_client)\nitem = c.get_item(\"Q5\")\nassert item.label == \"human\"\ndescription = item.description\naliases = item.aliases\n\n# see source for the format of item.statements\n```\n\n## Advanced customizations\n\nThere 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):\n\n```python\nfrom wikibase_rest_api_client import Client\n\ndef log_request(request):\n    print(f\"Request event hook: {request.method} {request.url} - Waiting for response\")\n\ndef log_response(response):\n    request = response.request\n    print(f\"Response event hook: {request.method} {request.url} - Status {response.status_code}\")\n\nclient = Client(\n    base_url=\"https://api.example.com\",\n    httpx_args={\"event_hooks\": {\"request\": [log_request], \"response\": [log_response]}},\n)\n\n# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()\n```\n\nYou can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):\n\n```python\nimport httpx\nfrom wikibase_rest_api_client import Client\n\nclient = Client(\n    base_url=\"https://api.example.com\",\n)\n# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.\nclient.set_httpx_client(httpx.Client(base_url=\"https://api.example.com\", proxies=\"http://localhost:8030\"))\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A client library for accessing the Wikibase REST API",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/derenrich/wikibase-rest-api-client",
        "Issues": "https://github.com/derenrich/wikibase-rest-api-client/issues",
        "Repository": "https://github.com/derenrich/wikibase-rest-api-client.git"
    },
    "split_keywords": [
        "wikibase",
        "wikidata",
        "rest-api",
        "client"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e06a4a50dcd7e1a1158afa9d9391dd6c8eb2e6fff383d6d8ada42864d27259ba",
                "md5": "16938d4496717230724c3d9f68b4851c",
                "sha256": "a636be4ff4fb6a43591a5e190a0a773a7261b2d293b66c68871f1f1577dc8939"
            },
            "downloads": -1,
            "filename": "wikibase_rest_api_client-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16938d4496717230724c3d9f68b4851c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 96089,
            "upload_time": "2024-01-26T06:51:00",
            "upload_time_iso_8601": "2024-01-26T06:51:00.637300Z",
            "url": "https://files.pythonhosted.org/packages/e0/6a/4a50dcd7e1a1158afa9d9391dd6c8eb2e6fff383d6d8ada42864d27259ba/wikibase_rest_api_client-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "46603b0a2b491c764352110adc77f0da150914e1d5d2f78c2f99d73091ebcb7d",
                "md5": "6de5e92a91085d80c0b252d37824474d",
                "sha256": "43c37ce63e26ca45cde851c65265af3a65bd10fb46d454aec5f1049c90ee4a9d"
            },
            "downloads": -1,
            "filename": "wikibase_rest_api_client-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "6de5e92a91085d80c0b252d37824474d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 27111,
            "upload_time": "2024-01-26T06:51:02",
            "upload_time_iso_8601": "2024-01-26T06:51:02.868525Z",
            "url": "https://files.pythonhosted.org/packages/46/60/3b0a2b491c764352110adc77f0da150914e1d5d2f78c2f99d73091ebcb7d/wikibase_rest_api_client-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-26 06:51:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "derenrich",
    "github_project": "wikibase-rest-api-client",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "wikibase-rest-api-client"
}
        
Elapsed time: 0.18229s