execute_sparql_query#

wikibaseintegrator.wbi_helpers.execute_sparql_query(query, prefix=None, endpoint=None, user_agent=None, max_retries=None, retry_after=60, auth=None, headers=None)[source]#

Static method which can be used to execute any SPARQL query

Server errors (HTTP 5xx), throttling (HTTP 429, after the delay asked by the Query Service), request timeouts (HTTP 408), connection errors and responses cut before their end are retried by wbi_backoff, up to config[‘BACKOFF_MAX_TRIES’] times. If the Query Service still fails after that, the last exception (e.g. requests.HTTPError) is raised. The other client errors (e.g. HTTP 400 for a malformed query) are raised at once.

Parameters:
  • prefix (str | None, default: None) – The URI prefixes required for an endpoint, default is the Wikidata specific prefixes

  • query (str) – The actual SPARQL query string

  • endpoint (str | None, default: None) – The URL string for the SPARQL endpoint. Default is the URL for the Wikidata SPARQL endpoint

  • user_agent (str | None, default: None) – Set a user agent string for the HTTP header to let the Query Service know who you are.

  • max_retries (int | None, default: None) – Deprecated and ignored, use config[‘BACKOFF_MAX_TRIES’] instead.

  • retry_after (int, default: 60) – The number of seconds to wait after a HTTP 429 (Too Many Requests) response without a Retry-After header.

  • auth (tuple[str, str] | AuthBase | None, default: None) – Authentication for a protected SPARQL endpoint: a (username, password) tuple for HTTP Basic auth or any requests.auth.AuthBase instance. Default is config[‘SPARQL_AUTH’].

  • headers (dict[str, str] | None, default: None) – Additional HTTP headers sent with the query (e.g. {‘Authorization’: ‘Bearer <token>’}). They override the default ones.

Return type:

dict[str, dict]

Returns:

The results of the query are returned in JSON format

Raises:

requests.HTTPError – If the Query Service returns an HTTP error, after the retries for the transient ones are exhausted