class Client:
Constructor: Client(project_name, api_key, client_id, client_secret, ...)
Low-level HTTP client for the DataGraphs REST API.
Handles authentication, pagination, and batched writes. Every read either
returns complete results or raises: a failed request raises
HttpStatusError (or DatagraphsError for a transport failure such as a
timeout), and a paged read whose pages do not add up raises
IncompleteReadError. An empty list always means there was nothing to read.
| Method | __init__ |
Initialise the DataGraphs client. |
| Method | apply |
Create or update datasets so they match the supplied list. |
| Method | apply |
Apply a schema to the project, replacing the currently active domain model. |
| Method | clear |
Delete all data of a specific class from a dataset, keeping the dataset itself intact. |
| Method | clear |
Delete all data from a dataset, keeping the dataset itself intact. |
| Method | create |
Create a new dataset. |
| Method | delete |
Delete a single entity by class and ID. |
| Method | drop |
Remove the specified class from a dataset, deleting all data of that class. |
| Method | drop |
Drop a dataset and all its data entirely. |
| Method | get |
Retrieve all entities of a given type. |
| Method | get |
Retrieve a specific dataset by slug. |
| Method | get |
Retrieve all datasets in the project. |
| Method | get |
Retrieve the active schema for the project. |
| Method | get |
Get the schema update dependencies for a new schema. |
| Method | put |
Load entities into a dataset. |
| Method | query |
Query the API with filters, facets, sorting, and pagination. |
| Method | query |
Read every result of a search, paging to the end. |
| Method | query |
Fetch one page of search results, with the total and the tokens for paging on. |
| Method | set |
Set the wait time between paginated requests. |
| Method | status |
Check the API service status. |
| Method | tear |
Remove all datasets and their data from the project. |
| Method | update |
Update an existing dataset. |
| Constant | DEFAULT |
Undocumented |
| Constant | DEFAULT |
Undocumented |
| Constant | DEFAULT |
Undocumented |
| Constant | DEFAULT |
Undocumented |
| Instance Variable | project |
Undocumented |
| Property | wait |
The current wait time in milliseconds between paginated requests. |
| Class Method | _build |
Build a query string from key-value pairs, URL-encoding each value. |
| Static Method | _bisection |
The requests bisection makes to isolate rejected entities spread evenly through a rejected batch of size. |
| Static Method | _extract |
Return the error details from a failure response body, falling back to its text. |
| Static Method | _log |
Undocumented |
| Static Method | _parse |
Parse a search response body, rejecting one that is not a search page. |
| Static Method | _retry |
The delay in seconds from a response's Retry-After header, if it gives one in seconds. |
| Method | _assert |
Poll the API until the applied datasets match datasets or timeout_s elapses. |
| Method | _cache |
Return a cache-busting timestamp value. |
| Method | _datasets |
Check if two lists of datasets match by slug and content. |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _get |
Undocumented |
| Method | _has |
Undocumented |
| Method | _identify |
Identify classes that need to be cleared or dropped based on the new schema. |
| Method | _is |
Whether error means a request was sent but no response arrived in time. |
| Method | _is |
Whether error is a failure that retrying the same read could cure. |
| Method | _isolate |
Find the rejected entities of a rejected group, accounting for every entity in ledger. |
| Method | _isolation |
The requests beyond one per entity that isolating a rejected batch of size may spend. |
| Method | _poll |
Select the poll interval for a given elapsed time, per _POLL_SCHEDULE. |
| Method | _put |
PUT one batch and classify the outcome, returning it with the error (if any). |
| Method | _put |
Send entities in batches, accounting for each in ledger. |
| Method | _query |
Undocumented |
| Method | _read |
Read every page of a search, returning all results or raising. |
| Method | _read |
Execute a request and return its JSON body, raising on any failure. |
| Method | _read |
GET one page of search results. |
| Method | _remove |
Undocumented |
| Method | _request |
Execute an HTTP request with automatic auth retry, raising on any failure. |
| Method | _resolve |
Resolve schema update dependencies by clearing or dropping classes as needed. :param dependencies: The list of schema update dependencies. |
| Method | _retry |
The delay before retry number attempt, honouring any Retry-After the API sent. |
| Method | _sanitise |
Undocumented |
| Method | _send |
Execute a request whose response body is not needed, raising on any failure. |
| Constant | _ALL |
Undocumented |
| Constant | _AUTH |
Undocumented |
| Constant | _AUTH |
Undocumented |
| Constant | _DATA |
Undocumented |
| Constant | _DATASETS |
Undocumented |
| Constant | _DEFAULT |
Undocumented |
| Constant | _DEFAULT |
Undocumented |
| Constant | _HTTP |
Undocumented |
| Constant | _HTTP |
Undocumented |
| Constant | _HTTP |
Undocumented |
| Constant | _HTTP |
Undocumented |
| Constant | _HTTP |
Undocumented |
| Constant | _HTTP |
Undocumented |
| Constant | _IN |
Undocumented |
| Constant | _MAX |
Undocumented |
| Constant | _NON |
Undocumented |
| Constant | _POLL |
Undocumented |
| Constant | _POLL |
Undocumented |
| Constant | _PROD |
Undocumented |
| Constant | _QUERY |
Undocumented |
| Constant | _RETRY |
Undocumented |
| Constant | _RETRY |
Undocumented |
| Constant | _RETRYABLE |
Undocumented |
| Constant | _TRANSIENT |
Undocumented |
| Instance Variable | _api |
Undocumented |
| Instance Variable | _auth |
Undocumented |
| Instance Variable | _batch |
Undocumented |
| Instance Variable | _client |
Undocumented |
| Instance Variable | _client |
Undocumented |
| Instance Variable | _http |
Undocumented |
| Instance Variable | _max |
Undocumented |
| Instance Variable | _service |
Undocumented |
| Instance Variable | _timeout |
Undocumented |
| Instance Variable | _wait |
Undocumented |
| Property | _base |
Undocumented |
str, api_key: str, client_id: str = '', client_secret: str = '', batch_size: int = DEFAULT_BATCH_SIZE, service_url: str = _PROD_URL, timeout: float | tuple[ float, float] = DEFAULT_TIMEOUT_S, max_read_retries: int = DEFAULT_MAX_READ_RETRIES):
¶
Initialise the DataGraphs client.
| Parameters | |
projectstr | Name of the project. |
apistr | API key for authentication. |
clientstr | OAuth client ID (required for write operations). |
clientstr | OAuth client secret (required for write operations). |
batchint | Number of items to process in each batch. |
servicestr | Base URL for the API service. |
timeout:float | tuple[ | Seconds to wait for every request, including the auth token request: either one value for both connecting and reading, or a (connect, read) pair. A request that exceeds it raises. |
maxint | How many times a GET request is retried after a transient failure (HTTP 429, 502, 503 or 504, a connection error or a timeout) before the failure is raised. 0 disables retries. Writes and GQL queries are never retried. |
Create or update datasets so they match the supplied list.
New datasets are created; existing datasets with changes are updated. Waits for confirmation that all datasets have been applied.
| Parameters | |
datasets:list[ | Datasets to apply. |
timeoutfloat | Maximum time in seconds to wait for the API to confirm all datasets are applied. |
| Raises | |
DatagraphsError | If datasets are not applied within the timeout. |
DatagraphsSchema, mode: SCHEMA_APPLY_MODE = SCHEMA_APPLY_MODE.APPLY) -> None | list[ dict[ str, Any]]:
¶
Apply a schema to the project, replacing the currently active domain model.
| Parameters | |
schema:DatagraphsSchema | The schema to apply. |
mode:SCHEMA_APPLY_MODE | APPLY to apply it, VALIDATE_ONLY to report what would block it without applying it, or FORCE to clear and drop whatever blocks it and then apply it. |
| Returns | |
None | list[ | For VALIDATE_ONLY, the blocking dependencies the API reported (empty if none); otherwise None. |
| Raises | |
ValueError | If mode is not a SCHEMA_APPLY_MODE value. |
DatagraphsError | If the API rejects the schema (and, for FORCE, its dependencies cannot be resolved). |
HttpStatusError | If the request fails for any reason other than the schema being rejected, e.g. a 504. |
Delete all data of a specific class from a dataset, keeping the dataset itself intact.
| Parameters | |
datasetstr | The slug of the dataset to clear. |
classstr | The name of the class to clear from the dataset. |
Delete all data from a dataset, keeping the dataset itself intact.
| Parameters | |
datasetstr | The slug of the dataset to clear. |
Delete a single entity by class and ID.
| Parameters | |
classstr | The entity class. |
entitystr | The entity identifier. |
| Raises | |
HttpStatusError | If the API does not confirm the delete. |
Remove the specified class from a dataset, deleting all data of that class.
| Parameters | |
datasetstr | The slug of the dataset to update. |
classstr | The name of the class to drop from the dataset. |
str, lang: str = 'all', include_date_fields: bool = False) -> list[ dict[ str, Any]]:
¶
Retrieve all entities of a given type.
Pages through every result, and returns them only once all have been
read (see query_all).
| Parameters | |
classstr | The entity class to fetch. |
lang:str | Language code for results (default 'all'). |
includebool | Whether to include system date metadata. |
| Returns | |
list[ | A list of every entity of the class. |
| Raises | |
HttpStatusError | If any page request fails. |
IncompleteReadError | If the pages do not add up to the reported total. |
DatagraphsError | If the read fails for any other reason. |
Retrieve all datasets in the project.
| Returns | |
list[ | A Dataset for every dataset in the project. |
| Raises | |
HttpStatusError | If any page request fails. |
IncompleteReadError | If the pages do not add up to the reported total. |
DatagraphsSchema) -> tuple[ set[ tuple[ str, str]], set[ tuple[ str, str]]]:
¶
Get the schema update dependencies for a new schema.
| Parameters | |
schema:DatagraphsSchema | The new schema to check. |
| Returns | |
tuple[ | A tuple containing two sets: - Classes to clear (set of (class_name, dataset_slug)) - Classes to drop (set of (class_name, dataset_slug)) |
str, data: dict[ str, Any] | list[ dict[ str, Any]], isolation: ISOLATION_MODE = ISOLATION_MODE.BISECT) -> PutResult:
¶
Load entities into a dataset.
Entities are sent in batches of the configured batch_size, and every batch is attempted regardless of the outcome of earlier ones. A batch whose payload is rejected (a 4xx response other than 404, 405, 408 or 429, or a value that cannot be JSON-encoded) is resubmitted as isolation directs, so that only the offending entities fail. A batch that fails for reasons unrelated to its payload (those 4xx statuses, a 5xx response or a transport error) is not resubmitted, and all of its entities are reported as failed. A batch that times out (504 or a read timeout) is reported as timed out, since its write may still have completed. All of this applies equally to each part of a batch that is resubmitted.
DataGraphs writes no part of a batch it rejects, so resubmitting parts of one cannot write an entity twice, even one without an id. An entity is reported as rejected only once it has been rejected on its own, so each rejection carries that entity's own status and errors.
| Parameters | |
dataset:str | Target dataset slug. |
data:dict[ | A single entity dict or a list of entity dicts. |
isolation:ISOLATION_MODE | How to find the rejected entities of a rejected batch. BISECT (the default) splits it in halves until they are isolated, switching to one entity at a time when most are rejected; INDIVIDUAL resubmits each entity on its own; NONE reports the whole batch as failed without resubmitting it. |
| Returns | |
PutResult | The outcome for every submitted entity. |
| Raises | |
ValueError | If isolation is not an ISOLATION_MODE value. |
AuthenticationError | If authentication fails; no further batches are attempted. Its result accounts for every entity, with those not written in unsent. |
TokenRequestError | If an auth token cannot be obtained; no further batches are attempted. Its result accounts for every entity, with those not written in unsent. |
str = '', dataset: str = '_all', q: str = '', filters: str = '', facets: str = '', facet_size: int = -1, date_facets: str = '', fields: str = '', embed: str = '', sort: str = '', ids: str = '', lang: str = 'all', page_no: int = -1, page_size: int = -1, previous_page_token: str = '', next_page_token: str = '', include_date_fields: bool = False) -> list[ dict[ str, Any]] | tuple[ list[ dict[ str, Any]], list[ dict[ str, Any]]]:
¶
Query the API with filters, facets, sorting, and pagination.
Returns a single page of results. Use query_page to also get the
total and the page tokens, or query_all to read every page.
| Parameters | |
gql:str | a GQL query string - if this is specified then all other arguments except lang are ignored, and the rows the query returns are returned as a list. |
dataset:str | Dataset slug to query (default '_all'). |
q:str | Free-text search query string. |
filters:str | Filter expression (e.g. 'type:Person'). |
facets:str | Comma-separated facet field names. |
facetint | Number of facet values to return (default 10). |
datestr | Date facet specification. |
fields:str | Comma-separated field names to include in results. |
embed:str | Embedding depth for related entities. |
sort:str | Sort expression (e.g. 'label:asc'). |
ids:str | Comma-separated entity IDs to fetch directly. |
lang:str | Language code for results (default 'all'). |
pageint | Page number for offset-based pagination. |
pageint | Number of results per page. |
previousstr | Token for cursor-based backward pagination. |
nextstr | Token for cursor-based forward pagination. |
includebool | Whether to include system date metadata. |
| Returns | |
list[ | A list of result dicts, or a (results, facets) tuple when the response carries facets. |
| Raises | |
HttpStatusError | If the request fails. |
DatagraphsError | If the request fails for any other reason, or the response is malformed. |
str = '_all', q: str = '', filters: str = '', fields: str = '', embed: str = '', sort: str = '', lang: str = 'all', page_size: int = -1, include_date_fields: bool = False) -> list[ dict[ str, Any]]:
¶
Read every result of a search, paging to the end.
Results are returned only once all have been read, so a partial result is never returned: every page must succeed, and the results must add up to the totalResults the API reported, with no result repeated. Pages are fetched by nextPageToken whenever the API provides one, since page numbers cannot reach past the first 10,000 results.
| Parameters | |
dataset:str | Dataset slug to query (default '_all'). |
q:str | Free-text search query string. |
filters:str | Filter expression (e.g. 'type:Person'). |
fields:str | Comma-separated field names to include in results. |
embed:str | Embedding depth for related entities. |
sort:str | Sort expression (e.g. 'label:asc'). |
lang:str | Language code for results (default 'all'). |
pageint | Number of results per page request (default: the client's batch_size). |
includebool | Whether to include system date metadata. |
| Returns | |
list[ | Every result, in the order the API returned them. |
| Raises | |
HttpStatusError | If any page request fails. |
IncompleteReadError | If the pages do not add up to the reported total. |
DatagraphsError | If the read fails for any other reason, or a response is malformed. |
str = '_all', q: str = '', filters: str = '', facets: str = '', facet_size: int = -1, date_facets: str = '', fields: str = '', embed: str = '', sort: str = '', lang: str = 'all', page_no: int = -1, page_size: int = -1, previous_page_token: str = '', next_page_token: str = '', include_date_fields: bool = False) -> QueryPage:
¶
Fetch one page of search results, with the total and the tokens for paging on.
Takes the same arguments as query, except gql and ids.
page_size defaults to the client's batch_size.
| Returns | |
QueryPage | The page. |
| Raises | |
HttpStatusError | If the request fails. |
DatagraphsError | If the request fails for any other reason, or the response is malformed. |
Check the API service status.
| Returns | |
str | The API status string, or 'unknown' if unavailable. |
| Raises | |
AuthenticationError | If authentication fails. |
Remove all datasets and their data from the project.
| Parameters | |
dropbool | If True, drops each dataset entirely. If False, only clears the data from each dataset. |
Build a query string from key-value pairs, URL-encoding each value.
Every character that could change how the query string is read (&, =, #, +, %, spaces, non-ASCII...) is encoded, so each value reaches the API exactly as given.
The requests bisection makes to isolate rejected entities spread evenly through a rejected batch of size.
Poll the API until the applied datasets match datasets or timeout_s elapses.
Polling backs off on the schedule described by _POLL_SCHEDULE: every 1s for the first 10s, every 2s for the next 20s, then every 5s.
| Parameters | |
datasets:list[ | The datasets expected to be applied. |
timeoutfloat | Maximum time in seconds to wait for confirmation. |
| Raises | |
DatagraphsError | If the datasets are not applied within the timeout. |
str = '_all', q: str = '', filters: str = '', facets: str = '', facet_size: int = -1, date_facets: str = '', fields: str = '', embed: str = '', sort: str = '', ids: str = '', lang: str = 'all', page_no: int = -1, page_size: int = -1, previous_page_token: str = '', next_page_token: str = '', include_date_fields: bool = False) -> str:
¶
Undocumented
list[ dict]) -> tuple[ set[ tuple[ str, str]], set[ tuple[ str, str]]]:
¶
Identify classes that need to be cleared or dropped based on the new schema.
| Parameters | |
dependencies:list[ | The list of schema update dependencies. |
| Returns | |
tuple[ | A tuple containing two sets: - Classes to clear (set of (class_name, dataset_slug)) - Classes to drop (set of (class_name, dataset_slug)) |
Whether error means a request was sent but no response arrived in time.
The API's 504 and a client read timeout both mean the request may still have completed server-side. A connect timeout does not: the request was never sent.
str, group: list[ dict[ str, Any]], start: int, ledger: _PutLedger, budget: _IsolationBudget):
¶
Find the rejected entities of a rejected group, accounting for every entity in ledger.
The group is split in halves while budget allows, and otherwise
resubmitted one entity at a time. start is the position of its first
entity in the data passed to put.
The requests beyond one per entity that isolating a rejected batch of size may spend.
Resubmitting one entity at a time costs size requests. Bisecting costs about 2·log₂(size) per rejected entity: far less when few are rejected, but up to twice as much when all are. Bisection is therefore allowed the cost of isolating one entity beyond size, and none at all when the rejections so far suggest it would cost more than size.
Select the poll interval for a given elapsed time, per _POLL_SCHEDULE.
| Parameters | |
elapsedfloat | Seconds elapsed since polling began. |
| Returns | |
float | The interval in seconds to wait before the next check. |
str, batch: list[ dict[ str, Any]]) -> tuple[ _BatchOutcome, Exception | None]:
¶
PUT one batch and classify the outcome, returning it with the error (if any).
str, dataset: str, entities: list[ dict[ str, Any]], isolation: ISOLATION_MODE, ledger: _PutLedger):
¶
Send entities in batches, accounting for each in ledger.
Callable[ [ str, int], str], lang: str = 'all') -> list[ dict[ str, Any]]:
¶
Read every page of a search, returning all results or raising.
| Parameters | |
pageCallable[ | Builds the URL of a page from a nextPageToken ('' when there is none) and a page number. |
lang:str | Undocumented |
| Returns | |
list[ | Undocumented |
Execute a request and return its JSON body, raising on any failure.
A GET is idempotent, so one that fails transiently is retried, with backoff, before the failure is raised.
| Parameters | |
method:HTTP | Undocumented |
url:str | Undocumented |
retries:int | None | How many times to retry a transient failure: by default max_read_retries for a GET, and none otherwise. |
| **kwargs | Undocumented |
| Returns | |
Any | Undocumented |
| Raises | |
DatagraphsError | If the request fails, or a successful response has an empty or unparseable body. |
set[ tuple[ str, str]], classes_to_drop: set[ tuple[ str, str]]):
¶
Undocumented
HTTP, url: str, lang: str = 'all', _retry_count: int = 0, **kwargs) -> requests.Response:
¶
Execute an HTTP request with automatic auth retry, raising on any failure.
The headers, including any auth token, are built afresh for every attempt, so a token obtained or refreshed for one attempt is used by it.
| Parameters | |
method:HTTP | The HTTP method to use. |
url:str | The request URL. |
lang:str | The language to request results in. |
_retryint | Internal retry counter (do not set externally). |
| **kwargs | Additional arguments forwarded to the HTTP client. |
| Returns | |
requests.Response | The response, which always has a 2xx status. |
| Raises | |
AuthenticationError | If authentication fails after max retries. |
TokenRequestError | If an auth token cannot be obtained; the request itself was then never sent. |
HttpStatusError | If the API responds with any other non-2xx status. |
DatagraphsError | If the request fails for any other reason, with the underlying requests exception (e.g. a ReadTimeout) as its __cause__. |
Resolve schema update dependencies by clearing or dropping classes as needed. :param dependencies: The list of schema update dependencies.
Undocumented
| Value |
|