PloneClient#
PloneClient holds the connection to one Plone site.
Parameters#
Parameter |
Type |
Default |
Meaning |
|---|---|---|---|
|
|
required |
URL of the site root, without |
|
|
|
Login name; empty for anonymous access. |
|
|
|
Password of |
|
|
|
A JWT issued beforehand, sent instead of a password. |
|
|
|
How |
|
|
|
A custom authentication flow; overrides the three above. |
|
|
|
Traversal prefix of the REST API. |
|
|
|
Seconds to wait for the site. |
|
|
|
Verify TLS certificates; a path of a CA bundle, a file or a directory, replaces the default certificates. |
|
|
|
Retries of idempotent requests. |
|
|
|
Base of the exponential delay between retries, in seconds. |
|
mapping or |
|
Extra headers sent with every request. |
|
|
|
Refuse every request that could change the site. |
|
|
|
Record changing requests in |
|
|
|
A custom transport, for tests. |
Constructors#
Constructor |
Reads |
|---|---|
|
The arguments. |
|
|
A profile from the configuration file, and its secret. |
Keyword arguments passed to either class method win over the settings it reads.
Retries#
Failure |
Requests retried |
|---|---|
Connection refused or reset before sending |
All |
|
|
Any other status |
None |
The delay between retries is Retry-After when the site sends it, and backoff * 2 ** attempt otherwise.
Safety switches#
Switch |
Changing requests |
Reads |
Always allowed |
|---|---|---|---|
|
Raise |
Sent |
|
|
Recorded in |
Sent |
Same as above |
Methods#
- class plone_client.PloneClient(url, *, username='', password='', token='', auth_method='basic', auth=None, api_prefix='++api++', timeout=30.0, verify=True, retries=3, backoff=0.5, headers=None, read_only=False, dry_run=False, frontend_url='', transport=None)[source]#
Bases:
objectClient for one Plone site, through
plone.restapi.Paths given to the client and its managers can be written as site paths (
/news/item), as absolute URLs of the site (the@idvalues the API returns) or as API URLs (with++api++). They are all mapped to the same API URL.- Parameters:
url (str) – URL of the site root, such as
https://example.orgorhttp://localhost:8080/Plone.username (str) – Login name.
password (str) – Password.
token (str) – JWT issued beforehand; used instead of the username and password.
auth_method (Literal['basic', 'jwt']) –
basic(default) sends the username and password on every request;jwtlogs in with@loginand sends the token.auth (Auth | None) – Any
httpx.Auth; overrides the credentials above.api_prefix (str) – Traversal prefix of the REST API.
++api++works with and without a Volto frontend in front of the backend; use an empty string to rely on theAcceptheader only.timeout (float) – Seconds to wait for the site.
verify (bool | str | SSLContext) – Verify TLS certificates. A path (a file or a directory) verifies them against that CA bundle instead of the default one; an
ssl.SSLContextis used as given.retries (int) – Times a request is repeated after a network error, or after a
429/502/503/504answer to an idempotent method.backoff (float) – Base delay in seconds between retries; doubles each time.
headers (Mapping[str, str] | None) – Extra headers sent with every request.
read_only (bool) – Refuse every request that could change the site.
dry_run (bool) – Do not send requests that could change the site; log them and return a description of what would have been sent instead.
frontend_url (str) – URL of the Volto frontend, for services that Volto serves itself (
@blocks-schema); the site URL by default, which is right whenurlis the frontend.transport (BaseTransport | None) – Custom
httpxtransport, mostly for tests.
- classmethod from_env(prefix='PLONE_CLIENT_', **overrides)[source]#
Build a client from environment variables.
Reads
<prefix>URL,<prefix>USERNAME,<prefix>PASSWORD,<prefix>TOKEN,<prefix>AUTH_METHOD,<prefix>TIMEOUT,<prefix>VERIFY,<prefix>RETRIES,<prefix>BACKOFF,<prefix>READ_ONLYand<prefix>FRONTEND_URL; seesettings_from_env().- Parameters:
- Returns:
A configured client.
- Raises:
PloneConfigurationError – If no URL is set.
- Return type:
- classmethod from_profile(name='', *, config_file=None, **overrides)[source]#
Build a client from a named profile.
Profiles live in the configuration file (see
plone_client.profiles) and their secrets in the operating system’s keyring.- Parameters:
- Returns:
A configured client.
- Raises:
PloneConfigurationError – If the profile or its secret is missing.
- Return type:
- path(ref)[source]#
Site path for a path or a URL of the site.
- Parameters:
ref (str) –
/news,news,https://example.org/newsorhttps://example.org/++api++/news. A query string is dropped.- Returns:
Path starting with
/, without a trailing slash (/for the site root).- Raises:
ValueError – If
refis a URL of another site.- Return type:
- token()[source]#
A JWT for the client’s user, for services that only take tokens.
Volto’s own services, such as
@blocks-schema, read a Bearer token and not basic authentication. A token given to the client is returned as is; otherwise the client logs in with@loginonce and keeps the token.- Returns:
The token, or an empty string for an anonymous client.
- Raises:
PloneAuthenticationError – If the site refuses the credentials.
- Return type:
- recording()[source]#
Record the requests sent, or skipped, inside a
withblock.Used by
plone_client.operationsto report every request an operation made. Recordings can be nested.
- request(method, ref='/', *, params=None, json=None, content=None, headers=None, raise_for_status=True, timeout=None)[source]#
Send a request to the site.
This is the escape hatch for services the managers do not cover, such as the ones added by add-ons.
- Parameters:
method (str) – HTTP method.
ref (str) – Path or URL of the site,
@services included (/news/@workflow/publish).params (Mapping[str, Any] | list[tuple[str, Any]] | None) – Query parameters (see
encode_params()).json (Any) – Body, serialized as JSON.
content (bytes | None) – Raw body, instead of
json.headers (Mapping[str, str] | None) – Extra headers for this request.
raise_for_status (bool) – Raise
PloneAPIErrorfor error answers.timeout (float | None) – Seconds to wait, instead of the client’s timeout.
- Returns:
The response.
- Raises:
PloneReadOnlyError – If the client is read-only and the request could change the site.
PloneAPIError – If the site answers with an error status.
PloneConnectionError – If the site cannot be reached.
- Return type:
Response
- frontend_request(method, ref='/', *, headers=None, raise_for_status=True)[source]#
Send a read request to the Volto frontend, with a Bearer token.
For the services that Volto serves itself rather than
plone.restapi, such as@blocks-schema. The request goes tofrontend_url, without++api++.- Parameters:
- Returns:
The response.
- Raises:
ValueError – If the method could change the site.
- Return type:
Response
- iter_batches(ref, params=None, *, key='items')[source]#
Yield every page of a batched answer, following
batching.next.- Parameters:
- Returns:
Each page, as returned by the site.
- Return type:
- iter_items(ref, params=None, *, key='items', limit=None)[source]#
Yield the items of every page of a batched answer.
- Parameters:
- Returns:
Each item.
- Return type:
- exists(ref)[source]#
Say whether there is an item at a path.
- Parameters:
ref (str) – Path or URL of the site.
- Returns:
Trueif the item exists,Falseon404.- Raises:
PloneAPIError – For any other error, such as
401.- Return type:
- plone_client.client.encode_params(params)[source]#
Encode query parameters the way
plone.restapireads them.Nonevalues are dropped;booleans become
true/false;lists and tuples become repeated keys (
portal_type=Document&portal_type=News Item);nested mappings are flattened with dots, which is how catalog query options are written (
{"path": {"query": "/news", "depth": 1}}becomespath.query=/news&path.depth=1).