# madbot_cli.client

*module*

A client of the madbot REST API, authenticated by a personal access token.

The client knows the conventions of the API: the token, the workspace header,
the pages of a list and the background tasks. It knows nothing of nodes or
submissions: `madbot_cli.operations` builds the steps of a deposit on it.

Every request goes through a *transport*, a function that sends one HTTP
request and returns the status and the body of the answer. The default one
uses the standard library; another one can be given, for a proxy or tests.

A request that can be sent again without effect (a read, a PUT, the values of
a metadata object) is sent again when the network drops or a gateway gives
up, after a growing pause: see `RETRY_DELAYS`. A request that creates is
never sent twice.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L1-1)

## madbot_cli.client.GATEWAY_GAVE_UP

*attribute* · *module attribute*

```python
GATEWAY_GAVE_UP = {502, 503, 504}
```

Statuses of a proxy or gateway that did not reach madbot: worth a new attempt.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L61-61)

## madbot_cli.client.LIST_READS

*attribute* · *module attribute*

```python
LIST_READS = 3
```

Times a list whose length changes while it is read is read again from its first page.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L49-49)

## madbot_cli.client.MAX_PAGE_SIZE

*attribute* · *module attribute*

```python
MAX_PAGE_SIZE = 500
```

The largest page the API serves.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L47-47)

## madbot_cli.client.MIN_API_VERSION

*attribute* · *module attribute*

```python
MIN_API_VERSION = (2, 2, 0)
```

The oldest version of the madbot API this release works with.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L44-44)

## madbot_cli.client.OnRetry

*attribute* · *module attribute*

```python
OnRetry = Callable[[str, float, int], None]
```

Told of a new attempt: (reason, seconds before it, attempt number).

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L64-64)

## madbot_cli.client.REDIRECTION_MAX

*attribute* · *module attribute*

```python
REDIRECTION_MAX = 400
```

The first HTTP status after those of a redirection.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L69-69)

## madbot_cli.client.REDIRECTION_MIN

*attribute* · *module attribute*

```python
REDIRECTION_MIN = 300
```

The first HTTP status of a redirection, which the client never follows.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L67-67)

## madbot_cli.client.REPLAYABLE_METHODS

*attribute* · *module attribute*

```python
REPLAYABLE_METHODS = {'GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE'}
```

Methods whose repetition has no further effect: they are sent again on a network drop.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L59-59)

## madbot_cli.client.RETRY_DELAYS

*attribute* · *module attribute*

```python
RETRY_DELAYS = (1, 2, 4, 8, 16)
```

Seconds to wait before each new attempt of a request that can be repeated: 31 s of pauses in all, each attempt waiting up to the timeout of the transport.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L57-57)

## madbot_cli.client.TASK_DONE

*attribute* · *module attribute*

```python
TASK_DONE = {'SUCCESS'}
```

States of a task over in success.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L52-52)

## madbot_cli.client.TASK_FAILED

*attribute* · *module attribute*

```python
TASK_FAILED = {'FAILURE', 'REVOKED'}
```

States of a task over in failure.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L54-54)

## madbot_cli.client.Transport

*attribute* · *module attribute*

```python
Transport = Callable[[str, str, Mapping[str, str], 'bytes | None'], 'tuple[int, bytes]']
```

A transport sends one request: (method, url, headers, body) -> (status, body).

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L41-41)

## madbot_cli.client.Client

*class*

```python
class Client
```

A client of the madbot REST API.

**Parameters**

- `api_url` (`str`) — The address of the API, such as ``https://api.madbot.example.org``. It is often not the address of the web application.
- `token` (`str`) — A personal access token of that instance, with the read and write scopes.
- `workspace` (`str`) (default: `None`) — The identifier of the workspace the requests act in, sent in the ``X-Workspace`` header. `use_workspace` sets it later.
- `transport` (`Transport`) (default: `None`) — Sends the requests; `urllib_transport()` by default.
- `sleep` (`callable`) (default: `time.sleep`) — Waits between two reads of a background task; `time.sleep` by default.
- `clock` (`callable`) (default: `time.monotonic`) — Measures how long a wait lasts; `time.monotonic` by default.
- `retry_delays` (`sequence of float`) (default: `RETRY_DELAYS`) — Seconds to wait before each new attempt of a request that can be repeated, when the network drops; `RETRY_DELAYS` by default, empty for none.
- `on_retry` (`callable`) (default: `None`) — Told of each new attempt, to show it: (reason, seconds before it, attempt number).

**Examples**

```pycon
>>> client = Client.from_env()
>>> client.use_workspace("my-workspace")
>>> nodes = client.list("/api/nodes")
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L135-706)

### madbot_cli.client.Client.api_url

*attribute* · *instance attribute*

```python
api_url = api_url.rstrip('/')
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L184-184)

### madbot_cli.client.Client.clock

*attribute* · *instance attribute*

```python
clock = clock
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L189-189)

### madbot_cli.client.Client.on_retry

*attribute* · *instance attribute*

```python
on_retry = on_retry
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L191-191)

### madbot_cli.client.Client.retry_delays

*attribute* · *instance attribute*

```python
retry_delays = tuple(retry_delays)
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L190-190)

### madbot_cli.client.Client.sleep

*attribute* · *instance attribute*

```python
sleep = sleep
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L188-188)

### madbot_cli.client.Client.token

*attribute* · *instance attribute*

```python
token = token
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L185-185)

### madbot_cli.client.Client.transport

*attribute* · *instance attribute*

```python
transport = transport or urllib_transport()
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L187-187)

### madbot_cli.client.Client.workspace

*attribute* · *instance attribute*

```python
workspace = workspace
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L186-186)

### madbot_cli.client.Client.attempt

*method*

```python
def attempt(method: str, url: str, headers: Mapping[str, str], body: bytes | None, replayable: bool) -> tuple[int, bytes]
```

Send a request through the transport, again after a pause when it can be repeated.

**Parameters**

- `method` (`str`) — The HTTP method.
- `url` (`str`) — The URL.
- `headers` (`mapping`) — The headers.
- `body` (`bytes or None`) — The body.
- `replayable` (`bool`) — Whether the request can be sent again: on no answer, or when a gateway gave up (`GATEWAY_GAVE_UP`).

**Returns**

- (`tuple`) — The status and the body of the answer.

**Raises**

- `ApiUnreachable` — When no answer came, after the last attempt.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L353-404)

### madbot_cli.client.Client.check_version

*method*

```python
def check_version() -> dict
```

Read the version of the API and tell whether this release works with it.

**Returns**

- (`dict`) — ``version`` of madbot, and ``compatible``: False for a version older than `MIN_API_VERSION`, None when it cannot be read.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L629-644)

### madbot_cli.client.Client.from_env

*method* · *classmethod*

```python
def from_env(api_url: str | None = None, token: str | None = None, environ: Mapping[str, str] | None = None, **options: Any) -> Client
```

Make a client from the ``MADBOT_API`` and ``MADBOT_TOKEN`` environment variables.

**Parameters**

- `api_url` (`str`) (default: `None`) — The address of the API; ``MADBOT_API`` when not given.
- `token` (`str`) (default: `None`) — The personal access token; ``MADBOT_TOKEN`` when not given.
- `environ` (`mapping`) (default: `None`) — The environment to read; `os.environ` by default.
- `**options` (`Any`) (default: `{}`) — Passed to the client: `workspace`, `transport`, `sleep`, `clock`, `retry_delays`, `on_retry`.

**Returns**

- (`Client`) — The client.

**Raises**

- `MadbotError` — When the address or the token is missing, or the address is not
http(s). There is no default
address, so that nothing is written to an instance by mistake.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L193-250)

### madbot_cli.client.Client.get

*method*

```python
def get(path: str, **params: Any) -> Any
```

Read one object.

**Parameters**

- `path` (`str`) — The path of the object.
- `**params` (`Any`) (default: `{}`) — Query parameters.

**Returns**

- (`Any`) — The decoded answer.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L438-454)

### madbot_cli.client.Client.list

*method*

```python
def list(path: str, *, must_settle: bool = False, **params: Any) -> list[Any]
```

Read every page of a list.

The API makes each page anew: a list that changes between two pages,
such as a folder a source lists differently from one request to the
next, would come back cut or with an item twice. Each page gives the
length of the whole list; when it changes, the list is read again
from its first page.

**Parameters**

- `path` (`str`) — The path of the list.
- `must_settle` (`bool`) (default: `False`) — True for a list that must be read whole, such as a folder of a source where a name is sought: when its length still changes after `LIST_READS` reads, the command stops. Otherwise the last read is kept, as for the data madbot still links while it is read.
- `**params` (`Any`) (default: `{}`) — Query parameters, such as filters.

**Returns**

- (`list`) — All the items of the list.

**Raises**

- `MadbotError` — With `must_settle`, when the length of the list still changes
after `LIST_READS` reads.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L498-543)

### madbot_cli.client.Client.post

*method*

```python
def post(path: str, payload: Any = None, *, replayable: bool = False) -> Any
```

Create an object or run an action.

**Parameters**

- `path` (`str`) — The path of the collection or action.
- `payload` (`Any`) (default: `None`) — The body of the request.
- `replayable` (`bool`) (default: `False`) — True when sending it twice has no further effect, such as values that replace those there: it is sent again when the network drops.

**Returns**

- (`Any`) — The decoded answer.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L456-475)

### madbot_cli.client.Client.read_pages

*method*

```python
def read_pages(path: str, params: Mapping[str, Any]) -> tuple[list[Any], bool]
```

Read a list once, page after page.

**Parameters**

- `path` (`str`) — The path of the list.
- `params` (`mapping`) — Query parameters, such as filters.

**Returns**

- (`tuple`) — The items read, and whether every page gave the same length of the list.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L545-574)

### madbot_cli.client.Client.request

*method*

```python
def request(method: str, path: str, payload: Any = None, *, params: Mapping[str, Any] | None = None, replayable: bool | None = None) -> Any
```

Send a request and return the decoded answer.

**Parameters**

- `method` (`str`) — The HTTP method.
- `path` (`str`) — A path under the API address, or a full URL.
- `payload` (`Any`) (default: `None`) — The body of the request, sent as JSON.
- `params` (`mapping`) (default: `None`) — Query parameters.
- `replayable` (`bool`) (default: `None`) — Whether the request can be sent again without effect; see `send`.

**Returns**

- (`Any`) — The decoded JSON answer, None when empty.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L406-436)

### madbot_cli.client.Client.same_origin

*method*

```python
def same_origin(url: str) -> str
```

Put a link of the API back on the address of the client.

Behind a proxy, the API may build its links on another scheme or host
(``http://`` behind TLS): the token must only go to `api_url`.

**Parameters**

- `url` (`str`) — A full URL given by the API, such as the `next` page of a list.

**Returns**

- (`str`) — The same path and query, on the scheme and host of `api_url`.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L477-496)

### madbot_cli.client.Client.send

*method*

```python
def send(method: str, path: str, payload: Any = None, *, params: Mapping[str, Any] | None = None, replayable: bool | None = None) -> tuple[int, Any]
```

Send a request and return the status with the decoded answer.

**Parameters**

- `method` (`str`) — The HTTP method.
- `path` (`str`) — A path under the API address, or a full URL.
- `payload` (`Any`) (default: `None`) — The body of the request, sent as JSON.
- `params` (`mapping`) (default: `None`) — Query parameters.
- `replayable` (`bool`) (default: `None`) — Whether the request can be sent again without effect, when the network drops; by default, for the methods of `REPLAYABLE_METHODS`. Never for a request that creates.

**Returns**

- (`tuple`) — The HTTP status and the decoded JSON answer (None when empty).

**Raises**

- `ApiError` — A subclass matching the status of an error answer; `ApiUnreachable`
without an answer, once the new attempts are spent; `NotJson`
when the answer is not JSON.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L278-351)

### madbot_cli.client.Client.url

*method*

```python
def url(path: str, params: Mapping[str, Any] | None = None) -> str
```

Build the URL of a request.

**Parameters**

- `path` (`str`) — A path under the API address (``/api/...``), or a full URL such as the `next` page of a list.
- `params` (`mapping`) (default: `None`) — Query parameters; those set to None are left out.

**Returns**

- (`str`) — The URL.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L254-276)

### madbot_cli.client.Client.use_workspace

*method*

```python
def use_workspace(name_slug_or_id: str) -> dict
```

Send the following requests in a workspace of the account.

**Parameters**

- `name_slug_or_id` (`str`) — The name, the slug (after ``/w/`` in the address of the web application) or the identifier of the workspace.

**Returns**

- (`dict`) — The workspace.

**Raises**

- `NotFound` — When the account has no such workspace.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L578-614)

### madbot_cli.client.Client.version

*method*

```python
def version() -> dict
```

Read the version of the API.

**Returns**

- (`dict`) — ``version`` of madbot and ``plugins_api_version``.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L618-627)

### madbot_cli.client.Client.wait_task

*method*

```python
def wait_task(task_id: str, *, timeout: float = 1800, interval: float = 2, pending_grace: float = 60, on_wait: Callable[[float], None] | None = None) -> dict
```

Wait for a background task of madbot to end.

A task is read with ``GET /api/tasks/{task_id}``. Right after it is
queued the API may not know it yet, then reports it ``PENDING`` until a
worker starts it: both are waited for.

**Parameters**

- `task_id` (`str`) — The identifier of the task, as an answer of the API gives it.
- `timeout` (`float`) (default: `1800`) — Seconds to wait in all.
- `interval` (`float`) (default: `2`) — Seconds between two reads.
- `pending_grace` (`float`) (default: `60`) — Seconds during which an unknown task is taken for one not recorded yet.
- `on_wait` (`callable`) (default: `None`) — Called with the seconds waited so far, before each pause: shows the progress.

**Returns**

- (`dict`) — The task, ended in success, with its ``result``.

**Raises**

- `TaskFailed` — When the task failed or was revoked.
- `WaitTimeout` — When the task is not over after `timeout` seconds.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L646-706)

## madbot_cli.client.urllib_transport

*function*

```python
def urllib_transport(timeout: float = 120) -> Transport
```

Make the default transport, built on `urllib.request`.

**Parameters**

- `timeout` (`float`) (default: `120`) — Seconds to wait for an answer.

**Returns**

- (`Transport`) — A function that sends a request and returns its status and body. An HTTP error is returned like any answer; only the absence of an answer raises `ApiUnreachable`. A redirection is not followed: it raises `Redirected`, with the address the API sends to.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/cli/madbot_cli/client.py#L85-132)
