Skip to main content

Brego SDK for Python

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.

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.

Metadata

Release files for brego 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for brego 0.1.0
File Size Uploaded Provenance Actions
brego-0.1.0.tar.gz 9.7 kB Without provenance Details for brego-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for brego 0.1.0
File Interpreter ABI Platform Actions
brego-0.1.0-py3-none-any.whl Python 3 none any Details for brego-0.1.0-py3-none-any.whl

Total release size: 17.6 kB

Release files / brego-0.1.0.tar.gz

Download URL brego-0.1.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2fdcf80d2290b25d04e8e770a3a442dde2f59bca58ebf25b156210af659a9fd3
BLAKE2b-256 checksum
How to use checksums
ade6597473d8f1e71045d19002d9b12d855ee499be34d5d2c4bbf03583f32f3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.16

Release files / brego-0.1.0-py3-none-any.whl

Download URL brego-0.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1c74523910542c88adc56442c4be88267d6af74995a62e0af3d8770b4d8223c
BLAKE2b-256 checksum
How to use checksums
ac0f9593d626745184859447cffc13fef9ddfe244af686581644aa8d1547370b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.16

Security for brego 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page