Package: wikibase-rest-api-client
wikibase-rest-api-clientA client library for accessing Wikibase REST API. Seethe swagger UIfor details of the API. Docs for the REST API can be found atdoc.wikimedia.org.UsageSee the tests for detailed examples of usage. For basic usage read on.First, create a client:fromwikibase_rest_api_clientimportClient# by default will connect to Wikidata at https://www.wikidata.org/w/rest.php/wikibase/v0/client=Client()# specify some other url to connect toother_client=Client(base_url="https://api.example.com")You probably should specify a User-Agent when constructing the clientfromwikibase_rest_api_clientimportClientclient=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)fromwikibase_rest_api_clientimportClientclient=Client(headers={"User-Agent":"my-agent/1.0.0"},follow_redirects=True)If the endpoints you're going to hit require authentication, useAuthenticatedClientinstead. Seethe OAuth docsfor how to get a token.fromwikibase_rest_api_clientimportAuthenticatedClientclient=AuthenticatedClient(token="SuperSecretToken")Now call your endpoint and use your models:fromwikibase_rest_api_client.modelsimportItemfromwikibase_rest_api_client.api.itemsimportget_itemfromwikibase_rest_api_client.typesimportResponsewithclientasclient:# 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:fromwikibase_rest_api_client.modelsimportItemfromwikibase_rest_api_client.api.itemsimportget_itemfromwikibase_rest_api_client.typesimportResponseasyncwithclientasclient:response:Response[Item]=awaitget_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 thatthis is a security risk.client=AuthenticatedClient(base_url="https://internal_api.example.com",token="SuperSecretToken",verify_ssl=False)Fluent usageThe 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.fromwikibase_rest_api_client.utilities.fluentimportFluentWikibaseClientfromwikibase_rest_api_clientimportClientinner_client=Client()# defaults to English valuesc=FluentWikibaseClient(inner_client)item=c.get_item("Q5")assertitem.label=="human"description=item.descriptionaliases=item.aliases# see source for the format of item.statementsAdvanced customizationsThere are more settings on the generatedClientclass which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlyinghttpx.Clientorhttpx.AsyncClient(depending on your use-case):fromwikibase_rest_api_clientimportClientdeflog_request(request):print(f"Request event hook:{request.method}{request.url}- Waiting for response")deflog_response(response):request=response.requestprint(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):importhttpxfromwikibase_rest_api_clientimportClientclient=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"))