air_sdk.utils#
Attributes#
Classes#
File-like object that reads only a specific portion of a file. |
Functions#
|
Filter out MISSING values from kwargs. |
|
|
|
|
|
Convert datetime to ISO string in UTC. |
|
|
|
|
|
|
|
|
|
Fallback type hint resolution when get_type_hints() fails. |
A wrapper for validating the type of payload during create. |
|
|
Get the SHA256 hash of the local file. |
|
Calculate part information for multipart upload. |
|
|
|
Ensures the API URL ends with the correct path. |
|
Validates that a given API response has the expected status code and JSON payload |
|
Wait for a model to reach one of the target states. |
Module Contents#
- air_sdk.utils.F#
- air_sdk.utils.filter_missing(**kwargs: Any) dict[str, Any][source]#
Filter out MISSING values from kwargs.
This is a helper function to remove dataclasses.MISSING sentinel values before passing kwargs to API methods.
- Parameters:
**kwargs – Keyword arguments that may contain MISSING values
- Returns:
Dictionary with MISSING values filtered out
- air_sdk.utils.BUILTIN_TYPES#
- air_sdk.utils.API_URI_PATTERN = '^/api/v\\d+.*$'#
- air_sdk.utils.COMPILED_API_URI_PATTERN#
- air_sdk.utils.iso_string_to_datetime(iso: str) datetime.datetime | None[source]#
- air_sdk.utils.datetime_to_iso_string(date: datetime.datetime) str[source]#
Convert datetime to ISO string in UTC.
Accepts any timezone-aware datetime. For naive datetimes (no timezone), assumes local timezone and emits a warning.
- Parameters:
date – The datetime to convert
- Returns:
ISO 8601 formatted string in UTC (with ‘Z’ suffix)
- Warns:
UserWarning – If datetime is naive (no timezone specified)
- air_sdk.utils.to_url(url: str) urllib.parse.ParseResult | None[source]#
- air_sdk.utils._resolve_type_hints_fallback(
- func: Callable[Ellipsis, Any],
Fallback type hint resolution when get_type_hints() fails.
This handles cases where TYPE_CHECKING imports aren’t available at runtime. Only validates types that can be resolved at runtime.
- Parameters:
func – Function to extract type hints from
- Returns:
Dictionary of resolvable type hints (name -> type)
- air_sdk.utils.validate_payload_types(func: F) F[source]#
A wrapper for validating the type of payload during create.
- air_sdk.utils.sha256_file(path: str | pathlib.Path) str[source]#
Get the SHA256 hash of the local file.
- air_sdk.utils.calculate_multipart_info( ) list[dict[str, int]][source]#
Calculate part information for multipart upload.
- Parameters:
file_size – Total size of the file in bytes
chunk_size – Size of each chunk in bytes
- Returns:
part_number: 1-based part number
start: Start byte offset
size: Size of this part in bytes
- Return type:
List of dictionaries containing part info with keys
- class air_sdk.utils.FilePartReader(file_path: str | pathlib.Path, start: int, size: int)[source]#
File-like object that reads only a specific portion of a file.
Used for streaming multipart uploads without loading entire file into memory. This class implements the context manager protocol and provides a read() method compatible with requests streaming uploads.
- Parameters:
file_path – Path to the file to read from
start – Starting byte offset in the file
size – Number of bytes to read from the start offset
Example
>>> with FilePartReader('large_file.bin', start=0, size=5242880) as reader: ... requests.put(presigned_url, data=reader)
- file_path#
- start#
- size#
- remaining#
- __enter__() FilePartReader[source]#
- air_sdk.utils.normalize_api_url(url: str) str[source]#
Ensures the API URL ends with the correct path.
- air_sdk.utils.raise_if_invalid_response(
- res: requests.Response,
- status_code: http.HTTPStatus = HTTPStatus.OK,
- data_type: type | None = dict,
Validates that a given API response has the expected status code and JSON payload
Arguments: res (requests.HTTPResponse) - API response object status_code [int] - Expected status code (default: 200)
Raises: AirUnexpectedResponse - Raised if an unexpected response is received from the API
- air_sdk.utils.wait_for_state(
- model: air_sdk.air_model.AirModel,
- target_states: str | list[str],
- *,
- state_field: str = 'state',
- timeout: datetime.timedelta | None = None,
- poll_interval: datetime.timedelta | None = None,
- error_states: str | list[str] | None = None,
Wait for a model to reach one of the target states.
This is a generic utility that works with any AirModel that has a state field (e.g., Simulation.state, Node.state, Image.upload_status, etc.).
- Parameters:
model – The AirModel instance to monitor (Simulation, Node, Image, etc.)
target_states – Single state or list of states to wait for
state_field – Name of the field containing the state (default: ‘state’). Use ‘upload_status’ for Images, ‘state’ for most other models.
timeout – Maximum time to wait (default: 120 seconds)
poll_interval – Time between status checks (default: 2 seconds)
error_states – Single state or list of states that should raise an error. If None, no error states are checked.
- Raises:
ValueError – If the model enters one of the error states
TimeoutError – If timeout is reached before target state
AttributeError – If the model doesn’t have the specified state_field
Example
>>> # Wait for simulation to become active >>> wait_for_state(simulation, 'ACTIVE', error_states=['INVALID', 'DELETING']) >>> >>> # Wait for image upload to complete >>> wait_for_state(image, 'COMPLETE', state_field='upload_status') >>> >>> # Wait for node to boot or become active >>> wait_for_state(node, ['BOOTING', 'ACTIVE'], error_states='ERROR')