Metadata-Version: 2.4
Name: brego
Version: 0.1.1
Summary: Brego API client for partner apps: OAuth connect flow, token refresh and webhook signature checks.
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# Brego SDK for Python

```bash
pip install brego
```

The partner connect flow for Python 3.8 or later, standard library only:
PKCE and the authorise URL, code exchange, thread-safe refresh, revoke, API
calls and webhook signature checks.

```python
from brego import BregoPartnerClient, create_pkce, create_state

brego = BregoPartnerClient(client_id, client_secret, token_store)
verifier, challenge = create_pkce()
state = create_state()
url = brego.authorize_url(redirect_uri, state, challenge, scopes=["api:vehicle:read"])

# In your callback, after checking state:
connection = brego.exchange_code(code, redirect_uri, verifier)
info = brego.get_connection(connection.connection_id)
```

`token_store` needs `get(connection_id)`, `set(tokens)` and
`delete(connection_id)`, which `revoke` calls once the connection is ended. Store the newest
refresh token after every refresh. Refresh tokens are single use: if several
processes share the store, also give it `with_lock(connection_id, fn)`, which
runs `fn` while holding a lock they all respect (a database row lock, for
example) and returns its result, so only one of them refreshes at a time. `request` refreshes the access token before it expires and
raises `BregoApiError` on a `403`: if `get_connection` also fails, refresh
once, and ask the organisation to connect again if that fails too.

Pass API paths that start with a single `/` (for example `/v1/vehicles`); anything else is refused, so a connection's token is only ever sent to the configured API.

If two approvals of the same connection are exchanged at once, the later one replaces the earlier tokens. The SDK saves exchanged tokens one connection at a time and never over stored tokens that still work while the new ones are refused; the exchange then returns the stored tokens. If Brego can't be reached to check which tokens work, nothing is overwritten and the exchange raises `BregoTokenCheckError`, carrying the new tokens in `tokens` and the stored ones in `stored`. The code that produced the new tokens can't be used again, so keep whichever set works once you can check. If Brego refuses both sets, nothing is saved and the exchange raises `BregoTokensRefusedError`: the connection was approved again or revoked, so read its tokens from your store again.

If your token store cannot save tokens Brego has just issued, the SDK retries a few times and then raises `BregoTokenStoreError`, carrying the new tokens in `tokens`. The code or refresh token that produced them can't be used again, so save those tokens yourself.
