bimmer_connected.api

The bimmer_connected.api module contains helper functions to communicate with the BMW APIs.

bimmer_connected.api.authentication

Authentication management for BMW APIs.

class bimmer_connected.api.authentication.MyBMWAuthentication(username: str, password: str, region: bimmer_connected.const.Regions, access_token: Optional[str] = None, expires_at: Optional[datetime.datetime] = None, refresh_token: Optional[str] = None)[source]

Authentication for MyBMW API.

async_auth_flow(request: httpx.Request) → AsyncGenerator[httpx.Request, httpx.Response][source]

Execute the authentication flow asynchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

login() → None[source]

Get a valid OAuth token.

login_lock

Make sure that there is a lock in the current event loop.

sync_auth_flow(request: httpx.Request) → Generator[httpx.Request, httpx.Response, None][source]

Execute the authentication flow synchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

class bimmer_connected.api.authentication.MyBMWLoginClient(*args, **kwargs)[source]

Async HTTP client based on httpx.AsyncClient with automated OAuth token refresh.

class bimmer_connected.api.authentication.MyBMWLoginRetry[source]

httpx.Auth used as workaround to retry & sleep on 429 Too Many Requests.

async_auth_flow(request: httpx.Request) → AsyncGenerator[httpx.Request, httpx.Response][source]

Execute the authentication flow asynchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

sync_auth_flow(request: httpx.Request) → Generator[httpx.Request, httpx.Response, None][source]

Execute the authentication flow synchronously.

By default, this defers to .auth_flow(). You should override this method when the authentication scheme does I/O and/or uses concurrency primitives.

bimmer_connected.api.client

Generic API management.

class bimmer_connected.api.client.MyBMWClient(config: bimmer_connected.api.client.MyBMWClientConfiguration, *args, brand: Optional[bimmer_connected.const.CarBrands] = None, **kwargs)[source]

Async HTTP client based on httpx.AsyncClient with automated OAuth token refresh.

generate_default_header(brand: Optional[bimmer_connected.const.CarBrands] = None) → Dict[str, str][source]

Generate a header for HTTP requests to the server.

class bimmer_connected.api.client.MyBMWClientConfiguration(authentication: bimmer_connected.api.authentication.MyBMWAuthentication, log_responses: Optional[bool] = False, observer_position: Optional[bimmer_connected.models.GPSPosition] = None, use_metric_units: Optional[bool] = True)[source]

Stores global settings for MyBMWClient.

log_responses = False
observer_position = None
set_log_responses(log_responses: bool) → None[source]

Set if responses are logged and clear response store.

use_metric_units = True

bimmer_connected.api.regions

Get the right url for the different countries.

bimmer_connected.api.regions.get_aes_keys(region: bimmer_connected.const.Regions) → Dict[str, bytes][source]

Get the keys for login nonce.

bimmer_connected.api.regions.get_app_version(region: bimmer_connected.const.Regions) → str[source]

Get the app version & build number for the region.

bimmer_connected.api.regions.get_ocp_apim_key(region: bimmer_connected.const.Regions) → str[source]

Get the authorization for OAuth settings.

bimmer_connected.api.regions.get_region_from_name(name: str) → bimmer_connected.const.Regions[source]

Get a region for a string.

This function is not case-sensitive.

bimmer_connected.api.regions.get_server_url(region: bimmer_connected.const.Regions) → str[source]

Get the url of the server for the region.

bimmer_connected.api.regions.valid_regions() → List[str][source]

Get list of valid regions as strings.

bimmer_connected.api.utils

Utils for bimmer_connected.api.

bimmer_connected.api.utils.anonymize_data(json_data: Union[List[T], Dict[KT, VT]]) → Union[List[T], Dict[KT, VT]][source]

Replace parts of the logfiles containing personal information.

bimmer_connected.api.utils.anonymize_response(response: httpx.Response) → bimmer_connected.models.AnonymizedResponse[source]

Anonymize a responses URL and content.

bimmer_connected.api.utils.anonymize_vin(match: re.Match)[source]

Anonymize VINs but keep assignment.

bimmer_connected.api.utils.create_s256_code_challenge(code_verifier: str) → str[source]

Create S256 code_challenge with the given code_verifier.

bimmer_connected.api.utils.generate_token(length: int = 30, chars: str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~') → str[source]

Generate a random token with given length and characters.

bimmer_connected.api.utils.get_correlation_id() → Dict[str, str][source]

Generate corrlation headers.

bimmer_connected.api.utils.handle_http_status_error(ex: httpx.HTTPStatusError, module: str = 'MyBMW API', log_handler: Optional[logging.Logger] = None, debug: bool = False) → None[source]

Try to extract information from response and re-raise Exception.