Client Module¶
This section documents the client components of the Nextmv Cloud API.
client
¶
Module with the client class.
This module provides the Client class for interacting with the Nextmv Cloud
API, and a helper function get_size to determine the size of objects.
| CLASS | DESCRIPTION |
|---|---|
Client |
Client that interacts directly with the Nextmv Cloud API. |
| FUNCTION | DESCRIPTION |
|---|---|
get_size |
Finds the size of an object in bytes. |
Client
dataclass
¶
Client(
api_key: str | None = None,
allowed_methods: list[str] = (
lambda: ["GET", "POST", "PUT", "DELETE"]
)(),
backoff_factor: float = 1,
backoff_jitter: float = 0.1,
backoff_max: float = 60,
configuration_file: str | None = None,
headers: dict[str, str] | None = None,
max_retries: int = 10,
status_forcelist: list[int] = (lambda: [429])(),
timeout: float = 20,
url: str | None = None,
console_url: str = "https://cloud.nextmv.io",
profile: str | None = None,
)
Client that interacts directly with the Nextmv Cloud API.
You can import the Client class directly from cloud:
The Client class is configured mainly with an API key and an endpoint
URL. There are multiple ways to provide these configurations, and the
client will check for them in the following order of precedence:
- The
api_keyandurlattributes set directly on the client instance. - The
NEXTMV_API_KEYandNEXTMV_ENDPOINTenvironment variables. - If the
NEXTMV_PROFILEenvironment variable is set, it is used to look up the API key and endpoint for that profile in the configuration file (~/.nextmv/config.yaml). - If the
profileattribute is set on the client, it is used to look up the API key and endpoint for that profile in the configuration file. - If the
profileattribute is not set, the default profile is used to look up the API key and endpoint in the configuration file. - If all of the above lookups fail, an exception is raised indicating that
the API key is missing, and the endpoint falls back to the hardcoded
default URL of
https://api.cloud.nextmv.io.
| ATTRIBUTE | DESCRIPTION |
|---|---|
api_key |
API key to use for authenticating with the Nextmv Cloud API. Resolved
from the constructor argument, the
TYPE:
|
allowed_methods |
HTTP methods for which failed requests are eligible for retry.
Defaults to
TYPE:
|
backoff_factor |
Multiplier applied between retry attempts using exponential backoff.
A value of
TYPE:
|
backoff_jitter |
Random jitter (in seconds) added to each backoff delay to avoid
thundering-herd problems. Defaults to
TYPE:
|
backoff_max |
Upper bound on the backoff delay, in seconds. No single wait between
retries will exceed this value. Defaults to
TYPE:
|
configuration_file |
Deprecated โ no longer used. Kept for backwards compatibility. Previously held the path to the Nextmv CLI configuration file.
TYPE:
|
headers |
HTTP headers sent with every request. Automatically populated during
TYPE:
|
max_retries |
Total number of retry attempts allowed per request. Defaults to
TYPE:
|
profile |
Named profile to use when looking up credentials in the configuration
file (
TYPE:
|
status_forcelist |
HTTP status codes that trigger an automatic retry. Defaults to
TYPE:
|
timeout |
Maximum number of seconds to wait for the server to send a response.
Defaults to
TYPE:
|
url |
Base URL of the Nextmv Cloud API. Resolved from the constructor
argument, the
TYPE:
|
console_url |
URL of the Nextmv Cloud web console. Defaults to
TYPE:
|
Examples:
Authenticate with an explicit API key:
>>> client = Client(api_key="YOUR_API_KEY")
>>> response = client.request(method="GET", endpoint="/v1/applications")
>>> print(response.status_code)
200
Authenticate via the NEXTMV_API_KEY environment variable (the
api_key argument can be omitted):
Use a named profile from the configuration file:
Override the profile with an environment variable:
>>> os.environ["NEXTMV_PROFILE"] = "production"
>>> client = Client() # uses the "production" profile
Customise retry and timeout behaviour:
>>> client = Client(
... api_key="YOUR_API_KEY",
... max_retries=3,
... backoff_factor=0.5,
... backoff_jitter=0.05,
... backoff_max=30,
... status_forcelist=[429, 500, 502, 503],
... timeout=60,
... )
Point the client at a self-hosted or staging API endpoint:
allowed_methods
class-attribute
instance-attribute
¶
Allowed HTTP methods to use for retries in requests to the Nextmv Cloud API.
api_key
class-attribute
instance-attribute
¶
API key to use for authenticating with the Nextmv Cloud API.
The API key is determined in the following order of precedence:
1. This api_key attribute set on the client.
2. The NEXTMV_API_KEY environment variable.
3. If the NEXTMV_PROFILE environment variable is set, it is used to look
up the API key for that profile in the configuration file.
4. If the profile attribute is set on the client, it is used to look up
the API key for that profile in the configuration file.
5. If the profile attribute is not set, the default profile is used to
look up the API key in the configuration file.
6. If all of the above lookups fail, an exception is raised indicating that
the API key is missing.
backoff_factor
class-attribute
instance-attribute
¶
Exponential backoff factor to use for requests to the Nextmv Cloud API.
backoff_jitter
class-attribute
instance-attribute
¶
Jitter to use for requests to the Nextmv Cloud API when backing off.
backoff_max
class-attribute
instance-attribute
¶
Maximum backoff time to use for requests to the Nextmv Cloud API, in seconds.
configuration_file
class-attribute
instance-attribute
¶
Deprecated. This attribute is no longer being used. Use the profile
attribute to specify different configurations instead.
console_url
class-attribute
instance-attribute
¶
URL of the Nextmv Cloud console.
headers
class-attribute
instance-attribute
¶
Headers to use for requests to the Nextmv Cloud API.
max_retries
class-attribute
instance-attribute
¶
Maximum number of retries to use for requests to the Nextmv Cloud API.
profile
class-attribute
instance-attribute
¶
Profile to use from the configuration file. Profiles allow you to configure multiple sets of API keys and endpoints in the configuration file and select between them.
The profile is determined in the following order of precedence:
1. The NEXTMV_PROFILE environment variable.
2. This profile attribute set on the client.
request
¶
request(
method: str,
endpoint: str,
data: Any | None = None,
headers: dict[str, str] | None = None,
payload: dict[str, Any] | None = None,
query_params: dict[str, Any] | None = None,
json_configurations: dict[str, Any] | None = None,
) -> Response
Makes a request to the Nextmv Cloud API.
| PARAMETER | DESCRIPTION |
|---|---|
|
HTTP method to use (e.g., "GET", "POST").
TYPE:
|
|
API endpoint to send the request to (e.g., "/v1/applications").
TYPE:
|
|
Data to send in the request body. Typically used for form data.
Cannot be used if
TYPE:
|
|
Additional headers to send with the request. These will override the default client headers if keys conflict.
TYPE:
|
|
JSON payload to send with the request. Prefer using this over
TYPE:
|
|
Query parameters to append to the request URL.
TYPE:
|
|
Additional configurations for JSON serialization. This allows
customization of the Python
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
The response object from the Nextmv Cloud API. |
| RAISES | DESCRIPTION |
|---|---|
HTTPError
|
If the response status code is not in the 2xx range. |
ValueError
|
If both |
Examples:
List all applications:
>>> client = Client(api_key="YOUR_API_KEY")
>>> response = client.request(method="GET", endpoint="/v1/applications")
>>> print(response.status_code)
200
>>> apps = response.json()
>>> print([a["id"] for a in apps["items"]])
['my-app', 'another-app']
Create a new run with a JSON payload:
>>> run_payload = {
... "applicationId": "my-app",
... "instanceId": "candidate",
... "input": {"value": 10},
... }
>>> response = client.request(
... method="POST",
... endpoint="/v1/runs",
... payload=run_payload,
... )
>>> print(response.json()["id"])
run_xxxxxxxxxxxx
Retrieve a specific run using query parameters:
>>> response = client.request(
... method="GET",
... endpoint="/v1/runs",
... query_params={"applicationId": "my-app", "limit": 5},
... )
>>> print(len(response.json()["items"]))
5
Send a request with custom headers (e.g., to pass a request ID):
>>> response = client.request(
... method="GET",
... endpoint="/v1/applications",
... headers={**client.headers, "X-Request-Id": "abc-123"},
... )
Send a JSON payload with custom serialization (pretty-printed):
>>> response = client.request(
... method="POST",
... endpoint="/v1/runs",
... payload={"applicationId": "my-app", "input": {}},
... json_configurations={"indent": 2},
... )
Source code in nextmv/nextmv/cloud/client.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
status_forcelist
class-attribute
instance-attribute
¶
Status codes to retry for requests to the Nextmv Cloud API.
timeout
class-attribute
instance-attribute
¶
Timeout to use for requests to the Nextmv Cloud API.
upload_to_presigned_url
¶
upload_to_presigned_url(
data: dict[str, Any] | str | None,
url: str,
json_configurations: dict[str, Any] | None = None,
tar_file: str | None = None,
) -> None
Uploads data to a presigned URL.
This method is typically used for uploading large input or output files directly to cloud storage, bypassing the main API for efficiency.
| PARAMETER | DESCRIPTION |
|---|---|
|
The data to upload. If a dictionary is provided, it will be JSON-serialized. If a string is provided, it will be uploaded as is.
TYPE:
|
|
The presigned URL to which the data will be uploaded.
TYPE:
|
|
Additional configurations for JSON serialization. This allows
customization of the Python
TYPE:
|
|
If provided, this will be used to upload a tar file instead of
a JSON string or dictionary. This is useful for uploading large
files that are already packaged as a tarball. If this is provided,
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
HTTPError
|
If the upload request fails. |
Examples:
Upload a dictionary as JSON (presigned URL obtained from a prior API call):
>>> client = Client(api_key="YOUR_API_KEY")
>>> input_data = {"stops": [{"id": "A"}, {"id": "B"}], "config": {"max_duration": 30}}
>>> client.upload_to_presigned_url(data=input_data, url="PRE_SIGNED_URL")
Upload a raw JSON string:
>>> client.upload_to_presigned_url(
... data='{"stops": [{"id": "A"}]}',
... url="PRE_SIGNED_URL",
... )
Upload a pre-built tarball (e.g., a packaged application):
>>> client.upload_to_presigned_url(
... data=None,
... url="PRE_SIGNED_URL",
... tar_file="/path/to/app.tar.gz",
... )
Source code in nextmv/nextmv/cloud/client.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
url
class-attribute
instance-attribute
¶
URL (endpoint) of the Nextmv Cloud API.
The endpoint is determined in the following order of precedence:
1. This url attribute set on the client.
2. The NEXTMV_ENDPOINT environment variable.
3. If the NEXTMV_PROFILE environment variable is set, it is used to look
up the endpoint for that profile in the configuration file.
4. If the profile attribute is set on the client, it is used to look up
the endpoint for that profile in the configuration file.
5. If the profile attribute is not set, the default profile is used to
look up the endpoint in the configuration file.
6. If all of the above lookups fail, the hardcoded default URL of
https://api.cloud.nextmv.io is used.
get_size
¶
get_size(
obj: dict[str, Any] | IO[bytes] | str,
json_configurations: dict[str, Any] | None = None,
) -> int
Finds the size of an object in bytes.
This function supports dictionaries (JSON-serialized), file-like objects (by reading their content), and strings.
| PARAMETER | DESCRIPTION |
|---|---|
|
The object whose size is to be determined. - If a dict, it's converted to a JSON string. - If a file-like object (e.g., opened file), its size is read. - If a string, its UTF-8 encoded byte length is calculated.
TYPE:
|
|
Additional configurations for JSON serialization. This allows
customization of the Python
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The size of the object in bytes. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the object type is not supported (i.e., not a dict, file-like object, or string). |
Examples:
>>> my_dict = {"key": "value", "number": 123}
>>> get_size(my_dict)
30
>>> import io
>>> my_string = "Hello, Nextmv!"
>>> string_io = io.StringIO(my_string)
>>> # To get size of underlying buffer for StringIO, we need to encode
>>> string_bytes_io = io.BytesIO(my_string.encode('utf-8'))
>>> get_size(string_bytes_io)
14
>>> get_size("Hello, Nextmv!")
14