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

Makes 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: bimmer_connected.const.CarBrands = None, **kwargs)[source]

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

generate_default_header(brand: 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_response_path: Optional[pathlib.Path] = None, observer_position: Optional[bimmer_connected.models.GPSPosition] = None, use_metric_units: Optional[bool] = True)[source]

Stores global settings for MyBMWClient.

log_response_path = None
observer_position = None
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.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: logging.Logger = None, debug: bool = False) → None[source]

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

bimmer_connected.api.utils.log_to_to_file(content: Union[str, bytes, List[T], Dict[KT, VT]], logfile_path: pathlib.Path, logfile_name: str) → None[source]

If a log path is set, log all responses to a file.