Session¶
Overview¶
The MQRESTSession class is the main entry point for interacting with
IBM MQ via the REST API. A session encapsulates connection details,
authentication, attribute mapping configuration, and diagnostic state. It
inherits MQSC command methods from MQRESTCommandMixin (see
commands) and idempotent ensure methods from
MQRESTEnsureMixin (see ensure).
Creating a session¶
from pymqrest import MQRESTSession
from pymqrest.auth import LTPAAuth
session = MQRESTSession(
rest_base_url="https://localhost:9443/ibmmq/rest/v2",
qmgr_name="QM1",
credentials=LTPAAuth("mqadmin", "mqadmin"),
)
The constructor validates all required fields and constructs the transport, mapping data, and authentication state at creation time. Errors in configuration (e.g. invalid mapping overrides) are caught immediately.
Constructor parameters¶
| Parameter | Type | Description |
|---|---|---|
rest_base_url |
Required | Base URL of the MQ REST API (e.g. https://host:9443/ibmmq/rest/v2) |
qmgr_name |
Required | Target queue manager name |
credentials |
Required | Authentication credentials (LTPAAuth, BasicAuth, or CertificateAuth) |
gateway_qmgr |
Optional | Gateway queue manager for remote routing |
map_attributes |
Optional | Enable/disable attribute mapping (default: True) |
mapping_strict |
Optional | Strict or lenient mapping mode (default: True) |
mapping_overrides |
Optional | Custom mapping overrides (sparse merge) |
verify_tls |
Optional | Verify server TLS certificates (default: True) |
timeout |
Optional | Default request timeout in seconds |
csrf_token |
Optional | Custom CSRF token value |
transport |
Optional | Custom transport implementation |
Minimal example¶
session = MQRESTSession(
rest_base_url="https://localhost:9443/ibmmq/rest/v2",
qmgr_name="QM1",
credentials=LTPAAuth("mqadmin", "mqadmin"),
)
Full example¶
session = MQRESTSession(
rest_base_url="https://mq-server.example.com:9443/ibmmq/rest/v2",
qmgr_name="QM2",
credentials=LTPAAuth("mqadmin", "mqadmin"),
gateway_qmgr="QM1",
map_attributes=True,
mapping_strict=False,
mapping_overrides=overrides,
verify_tls=True,
timeout=30,
)
Command methods¶
The session provides ~144 command methods, one for each MQSC verb + qualifier combination. See Commands for the full list.
# DISPLAY commands return a list of dicts
queues = session.display_queue("APP.*")
# Queue manager singletons return a single dict or None
qmgr = session.display_qmgr()
# Non-DISPLAY commands return None (raise on error)
session.define_qlocal("MY.QUEUE", request_parameters={"max_queue_depth": 50000})
session.delete_queue("MY.QUEUE")
Ensure methods¶
The session provides 16 ensure methods for declarative object management. Each method implements an idempotent upsert: DEFINE if the object does not exist, ALTER only the attributes that differ, or no-op if already correct.
result = session.ensure_qlocal("MY.QUEUE",
request_parameters={"max_queue_depth": 50000})
# result.action is EnsureAction.CREATED, UPDATED, or UNCHANGED
See Ensure for detailed usage and the full list of available ensure methods.
Diagnostic state¶
The session retains the most recent request and response for inspection. This is useful for debugging command failures or understanding what the library sent to the MQ REST API:
session.display_queue("MY.QUEUE")
session.last_command_payload # the JSON sent to MQ (dict)
session.last_response_payload # the parsed JSON response (dict)
session.last_http_status # HTTP status code (int)
session.last_response_text # raw response body (str)
Diagnostic attributes¶
| Attribute | Type | Description |
|---|---|---|
qmgr_name |
str |
Queue manager name |
gateway_qmgr |
str \| None |
Gateway queue manager (or None) |
last_http_status |
int |
HTTP status code from last command |
last_response_text |
str |
Raw response body from last command |
last_response_payload |
dict |
Parsed response from last command |
last_command_payload |
dict |
Command sent in last request |
Transport¶
See Transport for the transport protocol, response type, and mock transport examples.
API reference¶
Bases: MQRESTSyncMixin, MQRESTEnsureMixin, MQRESTCommandMixin
Session wrapper for MQ REST admin calls.
Provides MQSC command execution via the IBM MQ runCommandJSON
REST endpoint. Attribute mapping between snake_case and native
MQSC parameter names is enabled by default.
All MQSC command methods are inherited from
:class:~pymqrest.commands.MQRESTCommandMixin — see
:doc:/api/commands for the full list.
Attributes:
| Name | Type | Description |
|---|---|---|
last_response_payload |
dict[str, object] | None
|
The parsed JSON payload from the most
recent command, or |
last_response_text |
str | None
|
The raw HTTP response body from the most
recent command, or |
last_http_status |
int | None
|
The HTTP status code from the most recent
command, or |
last_command_payload |
dict[str, object] | None
|
The |
Source code in src/pymqrest/session.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
_rest_base_url = rest_base_url.rstrip('/')
instance-attribute
¶
_qmgr_name = qmgr_name
instance-attribute
¶
_gateway_qmgr = gateway_qmgr
instance-attribute
¶
_verify_tls = verify_tls
instance-attribute
¶
_timeout_seconds = timeout_seconds
instance-attribute
¶
_map_attributes = map_attributes
instance-attribute
¶
_mapping_strict = mapping_strict
instance-attribute
¶
_csrf_token = csrf_token
instance-attribute
¶
_credentials = credentials
instance-attribute
¶
_mapping_data = replace_mapping_data(mapping_overrides)
instance-attribute
¶
_transport = RequestsTransport(client_cert=cert)
instance-attribute
¶
_ltpa_cookie_name = None
instance-attribute
¶
_ltpa_token = None
instance-attribute
¶
last_response_payload = None
instance-attribute
¶
last_response_text = None
instance-attribute
¶
last_http_status = None
instance-attribute
¶
last_command_payload = None
instance-attribute
¶
qmgr_name
property
¶
The queue manager name this session targets.
gateway_qmgr
property
¶
The gateway queue manager name, or None for direct access.
__init__(rest_base_url, qmgr_name, *, credentials, gateway_qmgr=None, verify_tls=True, timeout_seconds=30.0, map_attributes=True, mapping_strict=True, mapping_overrides=None, mapping_overrides_mode=MappingOverrideMode.MERGE, csrf_token=DEFAULT_CSRF_TOKEN, transport=None)
¶
Initialize an MQ REST session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rest_base_url
|
str
|
Base URL of the MQ REST API
(e.g. |
required |
qmgr_name
|
str
|
Name of the target queue manager. |
required |
credentials
|
Credentials
|
A credential object (:class: |
required |
gateway_qmgr
|
str | None
|
Name of the gateway queue manager that routes
commands to qmgr_name. When set, the
|
None
|
verify_tls
|
bool
|
Whether to verify the server's TLS certificate.
Set to |
True
|
timeout_seconds
|
float | None
|
HTTP request timeout in seconds, or
|
30.0
|
map_attributes
|
bool
|
When |
True
|
mapping_strict
|
bool
|
When |
True
|
mapping_overrides
|
Mapping[str, object] | None
|
Optional overrides for the built-in
mapping data. Keys must be a subset of
|
None
|
mapping_overrides_mode
|
MappingOverrideMode
|
How to apply mapping_overrides.
:attr: |
MERGE
|
csrf_token
|
str | None
|
CSRF token value for the
|
DEFAULT_CSRF_TOKEN
|
transport
|
MQRESTTransport | None
|
Custom :class: |
None
|
Raises:
| Type | Description |
|---|---|
MQRESTAuthError
|
If LTPA login fails at construction time. |
ValueError
|
If mapping_overrides has an invalid structure. |
Source code in src/pymqrest/session.py
192 193 194 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 | |
_mqsc_command(*, command, mqsc_qualifier, name, request_parameters, response_parameters, where=None)
¶
Source code in src/pymqrest/session.py
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 | |