Base Module¶
The base module provides the core connection functionality for PyCentral.
NewCentralBase¶
NewCentralBase(token_info, logger=None, log_level='INFO', enable_scope=False)
¶
Constructor initializes the NewCentralBase class with token information and logging configuration.
Validates and processes the provided token information, sets up logging, and optionally initializes scope-related functionality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_info
|
dict or str
|
Dictionary containing token information for supported applications (new_central, glp). Can also be a string path to a YAML or JSON file with token information. |
required |
logger
|
Logger
|
Logger instance. Defaults to None. |
None
|
log_level
|
str
|
Logging level. Defaults to "INFO". |
'INFO'
|
enable_scope
|
bool
|
Flag to enable scope management. If True, the SDK will automatically fetch data about existing scopes and associated profiles, simplifying scope and configuration management. If False, scope-related API calls are disabled, resulting in faster initialization. Defaults to False. |
False
|
Source code in pycentral/base.py
set_logger(log_level, logger=None)
¶
Set up the logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_level
|
str
|
Logging level. |
required |
logger
|
Logger
|
Logger instance. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
Logger instance. |
Source code in pycentral/base.py
create_token(app_name)
¶
Create a new access token for the specified application.
Generates a new access token using the client credentials for the specified application, updates the self.token_info dictionary with the new token, and optionally saves it to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str
|
Name of the application. Supported applications: "new_central", "glp". |
required |
Returns:
| Type | Description |
|---|---|
str
|
Access token. |
Raises:
| Type | Description |
|---|---|
LoginError
|
If there is an error during token creation. |
Source code in pycentral/base.py
command(api_method, api_path, app_name='new_central', api_data=None, api_params=None, headers=None, files=None)
¶
Execute an API command to Central or GreenLake Platform.
This is the primary method for making API calls from the SDK. It handles authentication, token refresh on expiry, request formatting, and response parsing. All other SDK modules internally use this method to make API calls.
The method automatically
- Validates the application name and HTTP method
- Constructs the full URL from self.base_url and api_path
- Adds appropriate headers (Content-Type, Accept) if not provided
- Serializes api_data to JSON when Content-Type is application/json
- Handles 401 errors by refreshing the access token and retrying if client credentials are available
- Parses JSON responses when possible
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_method
|
str
|
HTTP method for the API call. Supported methods: POST, PATCH, DELETE, GET, PUT. |
required |
api_path
|
str
|
API endpoint path (e.g., "monitoring/v1/aps"). This is appended to the base_url configured in token_info. |
required |
app_name
|
str
|
Target application for the API call. Use "new_central" for Central APIs (default). Use "glp" for GreenLake Platform APIs. |
'new_central'
|
api_data
|
dict
|
Request body/payload to be sent. Automatically serialized to JSON if Content-Type is application/json. Defaults to None. |
None
|
api_params
|
dict
|
URL query parameters for the API request. Defaults to None. |
None
|
headers
|
dict
|
Custom HTTP headers. If not provided and no files are being uploaded, defaults to {"Content-Type": "application/json", "Accept": "application/json"}. |
None
|
files
|
dict
|
Files to upload in multipart/form-data requests. When provided, Content-Type header is not automatically set. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
API response containing: - code (int): HTTP status code - msg (dict or str): Parsed JSON response body, or raw text if not JSON - headers (dict): Response headers |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If there is an error during the API call. |
Source code in pycentral/base.py
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 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 | |
request_url(url, access_token, app_name='new_central', data=None, method='GET', headers=None, params=None, files=None)
¶
Make an API call to application (New Central or GLP) via the requests library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
HTTP Request URL string. |
required |
access_token
|
str
|
Access token for authentication. |
required |
app_name
|
str
|
App name used to select HTTP client. Defaults to "new_central". |
'new_central'
|
data
|
dict
|
HTTP Request payload. Defaults to None. |
None
|
method
|
str
|
HTTP Request Method supported by GLP/New Central. Defaults to "GET". |
'GET'
|
headers
|
dict
|
HTTP Request headers. Defaults to None. |
None
|
params
|
dict
|
HTTP URL query parameters. Defaults to None. |
None
|
files
|
dict
|
Files dictionary with file pointer depending on API endpoint as accepted by GLP/New Central. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Response
|
HTTP response of API call using requests library. |
Raises:
| Type | Description |
|---|---|
ResponseError
|
If there is an error during the API call. |
Note
Transient transport failures (DNS resolution errors, connection failures,
timeouts, and proxy errors) are automatically retried with exponential
backoff up to RETRY_MAX_RETRIES attempts. All other exceptions are
raised immediately as ResponseError without retrying.
Source code in pycentral/base.py
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 | |
get_scopes()
¶
Set up the scopes for the current instance by creating a Scopes object.
Initializes the scopes attribute using the Scopes class, passing the current instance as the central_conn parameter. If the scopes attribute is already initialized, it simply returns the existing object.
Returns:
| Type | Description |
|---|---|
Scopes
|
The initialized or existing Scopes object. |
Source code in pycentral/base.py
close()
¶
Close all underlying HTTP clients and release connection pool resources.