Metadata-Version: 2.4
Name: lvi-api-client
Version: 0.7.0
Summary: LogicVein API Client Library
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Dynamic: license-file

# LogicVein API Client

A Python client for interacting with the LogicVein API.

## Installation

From PyPI:

```bash
pip install lvi-api-client
```

From TestPyPI (pre-release / testing):

```bash
pip install \
  --index-url https://test.pypi.org/simple/ \
  --extra-index-url https://pypi.org/simple/ \
  lvi-api-client
```

See [PUBLISHING.md](PUBLISHING.md) for instructions on publishing new releases.

## Usage

```python
from logicvein import LogicVeinAPIClient

client = LogicVeinAPIClient(host="your-lvi-server", api_token="your-token")
devices = client.search_devices_in_network()
```

Settings not passed as arguments are read from `LVI_HOST`, `LVI_API_TOKEN` (or `LVI_USERNAME`
and `LVI_PASSWORD`), `LVI_VERIFY_SSL` and `LVI_TIMEOUT`; `ignore_env_vars=True` reads none of
them.

### Logging in

With an API token there is no login: the token goes in a header of every call. With a username
and password and no token, the client logs in as it is created, and `authenticate()` logs in
again. A login that does not work raises:

- `LogicVeinAuthenticationError`: the server rejected the username or password (HTTP 401). It
  has `host` and `username`.
- `LogicVeinConnectionError`: the server could not be reached or did not answer as it should:
  connection refused, host name not found, TLS failure, timeout, a redirect, a request for a
  one-time code, no `/auth` login, a success that leaves the session with no cookie, any
  other HTTP error status, or a CA bundle path that does not exist. It has `host`, `reason`
  and, if the server answered, `status_code`.

```python
from logicvein import LogicVeinAPIClient, LogicVeinAuthenticationError, LogicVeinConnectionError

try:
    client = LogicVeinAPIClient(host="your-lvi-server", username="your-user", password="your-password")
except LogicVeinAuthenticationError as error:
    print(f"Wrong username or password: {error}")
except LogicVeinConnectionError as error:
    print(f"Server problem: {error}")
```

Neither error holds the password, the request or its URL, so both are safe to print and log.

The password login is a POST to `https://<host>/auth` with the username and password in a form
body. The URL has no query string, so the password is not in the request line that servers and
reverse proxies commonly write to their access logs. The response `authenticate()` returns
holds the login request, and so the password, in `request.body`: do not log or print that.

Four answers to the login are a `LogicVeinConnectionError` whose `reason` says what they mean:

| Answer | Meaning | What to do |
|--------|---------|------------|
| HTTP 202 | The account needs a one-time code, which the client cannot supply. | Use an API token. |
| HTTP 404 or 405 | The server has no `/auth` login. The client has no other password login. | Use an API token. |
| Any HTTP 3xx | The login was redirected. The client never follows a login's redirect, because that can send the password to another address. | Set `host` to the name the server itself answers on. |
| HTTP 2xx (not 202) and no cookie in the session | A cookie login with no cookie logged nothing in: the answer came from a proxy's page or a host that is not a LogicVein server. | Check `host` and any proxy in front of the server. |

`authenticate()` passes extra keyword arguments on to the session's `post()`, except the ones
the login sets itself, which are a `TypeError`: `allow_redirects`, `data`, `files`, `headers`,
`json`, `params` and `timeout`.

A redirect whose `Location` header cannot be parsed is a `LogicVeinConnectionError` too, but
without the status: `requests` fails on it before the client sees the answer, so the `reason`
names that failure, as in `request failed (ValueError)`, and `status_code` is `None`.

The login is judged by its HTTP status and by the session's cookies. Any 2xx but 202 is taken as
a login if the session holds a cookie afterwards, and raises if it holds none. The session's
cookie jar is what is checked, not the answer's `Set-Cookie` header: the server answers a second
login on a session that is already logged in without a new cookie, and that login returns. The
check is a necessary condition only. Any cookie in the jar counts: its name, the host it is for
and whether it has expired are not checked. A proxy's page that sets a cookie of its own passes,
and so does a session that already held a cookie from somewhere else, so `test_connection()` is
still what tells a LogicVein server from a host that is not one.

An API token needs no login and works where a password login does not: prefer one where you
can.

### TLS verification

The client verifies the server's TLS certificate by default. For a server whose certificate
comes from a private CA, keep verification on and point `REQUESTS_CA_BUNDLE` at the CA's
certificate. `verify=False`, or `LVI_VERIFY_SSL=false`, turns verification off, and the API
token or password is then sent to whichever server answers. `LVI_VERIFY_SSL` accepts
true/false, 1/0, yes/no and on/off in any case; unset or empty means verify, and anything else
is an error.
