Lab: Build API Client with Token Refresh & Async Polling
Objectives
- Create Python API client library that handles OAuth2 authentication, token refresh, pagination, rate limiting, and async operation polling.
Prerequisites
VCF lab environment deployed and operational
Lab Environment
Standard VCF lab environment for Advanced VCF 9.0 Automation
Tasks
Task 1 Lab: Build API Client with Token Refresh & Async Polling
Create Python API client library that handles OAuth2 authentication, token refresh, pagination, rate limiting, and async operation polling.
Create VCFAutomationClient class with authenticate() method: for a VCF Automation 9.0 service account, obtain the token through the device authorization grant (/tm/oauth/tenant/<org>/device_authorization, approve the user code, then /tm/oauth/tenant/<org>/token with grant_type device_code) (VCF 9.0.1 docs p.6096-6097)
Implement auto token refresh in get_headers(): exchange the refresh token at /tm/oauth/tenant/<org>/token before expires_in elapses, and store the NEW refresh_token from every response — service-account API tokens rotate on every use (RFC 6749 §6; VCF 9.0.1 docs p.5861)
Add api_request() wrapper that handles HTTP 429 with exponential backoff
Implement paginate() method for bulk listing
Add submit_deployment() method that returns request ID
Implement poll_request_status() with 10-minute timeout and 5s poll interval
Test with real VCF Automation instance: deploy blueprint, poll until completion
Verify token refresh happens before expiry (e.g. at the 55-minute mark for expires_in 3600) and that the client persists and uses the rotated refresh_token on the next refresh
Validation Gate
Check: Verify lab completion
Expected: Lab exercise completed successfully
Common Errors
Final Validation
Lab completed successfully
✓ All steps completed → No errors observed
Cleanup / Restore
• Revert to snapshot if needed
Design Reflection (VCDX)
API integration skills demonstrate automation maturity. VCDX designs with programmatic management show scalability beyond UI-driven operations.