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: Optional[str] = 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 = "~/.nextmv/config.yaml",
headers: Optional[dict[str, str]] = None,
max_retries: int = 10,
status_forcelist: list[int] = lambda: [429](),
timeout: float = 20,
url: str = "https://api.cloud.nextmv.io",
console_url: str = "https://cloud.nextmv.io",
)
Client that interacts directly with the Nextmv Cloud API.
You can import the Client
class directly from cloud
:
The API key will be searched, in order of precedence, in:
- The
api_key
argument in the constructor. - The
NEXTMV_API_KEY
environment variable. - The
~/.nextmv/config.yaml
file used by the Nextmv CLI.
PARAMETER | DESCRIPTION |
---|---|
|
API key to use for authenticating with the Nextmv Cloud API. If not
provided, the client will look for the
TYPE:
|
|
Allowed HTTP methods to use for retries in requests to the Nextmv
Cloud API. Defaults to
TYPE:
|
|
Exponential backoff factor to use for requests to the Nextmv Cloud
API. Defaults to
TYPE:
|
|
Jitter to use for requests to the Nextmv Cloud API when backing off.
Defaults to
TYPE:
|
|
Maximum backoff time to use for requests to the Nextmv Cloud API, in
seconds. Defaults to
TYPE:
|
|
Path to the configuration file used by the Nextmv CLI. Defaults to
TYPE:
|
|
Headers to use for requests to the Nextmv Cloud API. Automatically set up with the API key.
TYPE:
|
|
Maximum number of retries to use for requests to the Nextmv Cloud
API. Defaults to
TYPE:
|
|
Status codes to retry for requests to the Nextmv Cloud API. Defaults
to
TYPE:
|
|
Timeout to use for requests to the Nextmv Cloud API, in seconds.
Defaults to
TYPE:
|
|
URL of the Nextmv Cloud API. Defaults to
TYPE:
|
|
URL of the Nextmv Cloud console. Defaults to
TYPE:
|
Examples:
>>> client = Client(api_key="YOUR_API_KEY")
>>> response = client.request(method="GET", endpoint="/v1/applications")
>>> print(response.json())
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. If not provided, the client will look for the NEXTMV_API_KEY environment variable.
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
¶
Path to the configuration file used by the Nextmv CLI.
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.
request
¶
request(
method: str,
endpoint: str,
data: Optional[Any] = None,
headers: Optional[dict[str, str]] = None,
payload: Optional[dict[str, Any]] = None,
query_params: Optional[dict[str, Any]] = None,
json_configurations: Optional[dict[str, Any]] = 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:
>>> client = Client(api_key="YOUR_API_KEY")
>>> # Get a list of applications
>>> response = client.request(method="GET", endpoint="/v1/applications")
>>> print(response.status_code)
200
>>> # Create a new run
>>> run_payload = {
... "applicationId": "app_id",
... "instanceId": "instance_id",
... "input": {"value": 10}
... }
>>> response = client.request(
... method="POST",
... endpoint="/v1/runs",
... payload=run_payload
... )
>>> print(response.json()["id"])
run_xxxxxxxxxxxx
Source code in nextmv/nextmv/cloud/client.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 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 |
|
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: Union[dict[str, Any], str],
url: str,
json_configurations: Optional[dict[str, Any]] = 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:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
HTTPError
|
If the upload request fails. |
Examples:
Assume presigned_upload_url
is obtained from a previous API call.
>>> client = Client(api_key="YOUR_API_KEY")
>>> input_data = {"value": 42, "items": [1, 2, 3]}
>>> client.upload_to_presigned_url(data=input_data, url="PRE_SIGNED_URL")
Source code in nextmv/nextmv/cloud/client.py
url
class-attribute
instance-attribute
¶
URL of the Nextmv Cloud API.
get_size
¶
get_size(
obj: Union[dict[str, Any], IO[bytes], str],
json_configurations: Optional[dict[str, Any]] = 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