duo_client_python icon indicating copy to clipboard operation
duo_client_python copied to clipboard

Using json_api_call for bulk operations?

Open jasongabler opened this issue 5 months ago • 1 comments
trafficstars

I went down the rabbit hole of trying to use duo_client to implement bulk user operations.

def bulk_update_users(updates):
    params = []
    for update in updates:
        params.append({
            "method": "POST",
            "path": f"/admin/v1/users/{update['user_id']}",
            "body": update['changes']
        })

    r = client.json_api_call('POST', '/admin/v1/bulk', params)

Where update['changes'] is a dict of modification string key-value pairs as described in the API docs.

But I was foiled by client.py's canon_json() that expects the params to be a dict. As you can see, my params is an array of modify dicts, which violates that isinstance():

    def canon_json(cls, params):
        if not isinstance(params, dict):
            raise ValueError('JSON request must be an object.')
        return json.dumps(params, sort_keys=True, separators=(',', ':'))

Am I missing something here? Or is this not possible with duo_client?

Thanks.

jasongabler avatar Jun 17 '25 00:06 jasongabler

And it was my mistake. It's all good if I provide a dict instead of an array, with "operations" as the key, as the API describes.

Closing.

def bulk_update_users(updates):
    params = []
    for update in updates:
        params.append({
            "method": "POST",
            "path": f"/admin/v1/users/{update['user_id']}",
            "body": update['changes']
        })

    r = duo_admin.json_api_call('POST', '/admin/v1/bulk', {"operations": json.dumps(params)})

jasongabler avatar Jun 17 '25 03:06 jasongabler