Running a script to update ~2,000 users in our tenant (mostly just toggling some custom attributes and updating phone numbers). I’m hitting /api/v2/users/{userId} with a PUT request using the Python SDK.
Everything works fine for the first 50 or so calls. Then the server starts throwing 429 Too Many Requests errors like crazy. I see the Retry-After header in the response, but my current sleep logic isn’t catching it fast enough and I’m getting rate-limited harder.
Here’s the rough loop I’ve got:
import time
from purecloudplatform.client import PureCloudPlatformClientV2
api_instance = PureCloudPlatformClientV2(api_client).users_api
for user in users_to_update:
try:
body = User(
phone_numbers=[PhoneNumber(value=user['phone'], type='work')],
custom_attributes=user['custom_attrs']
)
api_instance.put_user(user['id'], body=body)
# Naive sleep
time.sleep(0.1)
except PureCloudPlatformClientV2.rest.ApiException as e:
if e.status == 429:
# This feels wrong
time.sleep(1)
continue
else:
raise e
The Retry-After header sometimes says 5 seconds, sometimes 10. If I just sleep for 1 second and retry, I keep hitting the wall. Should I be parsing the Retry-After header dynamically and sleeping for that exact duration? Or is there a built-in method in the SDK I’m missing that handles rate limiting automatically?
Also, is there a way to batch these updates? I know there’s a bulk update endpoint for some things, but I don’t see one for general user profile changes.