Legacy Dev Forum Posts

 View Only

Sign Up

Recent versions of python SDK have made classes not subscriptable

  • 1.  Recent versions of python SDK have made classes not subscriptable

    Posted 06-05-2025 18:09

    AllanK | 2023-10-24 23:29:14 UTC | #1

    Hi All,

    It seems over the last couple of weeks changes may have been made that make object classes for standard objects like QUEUE or CAMPAIGN not iterable - so I've had to go through loops (pun intended) to convert the contained information to another format to make it iterable - in order to extract a piece of information>>...

    FIrstly - why has this change happened and can it be fixed.

    B) Here is my method of doing it --- BUT I'm very open to better ways. (without hacking the SDK package(

    Note - This also includes my solution for getting a unique name where the business may have named objects in a similar way (e.g. DefaultOutboundScript, DefaultOutboundScript1)

    Here's the bit that gets the data out of the object and turns it into a standard dictionary.

    def clean_object(thing):
        thing=vars(thing)
        keysList= list(thing.keys())
        #get the list of keys that start without underscores then remove them 
        plain_keys = [key for key in keysList if key[0:1] != "_"]
        for i in range (0,len(plain_keys)):
                del thing[plain_keys[i]]
        #get the list of keys that start with underscores 
        und_keys = [key for key in keysList if key[0:1] == "_"]
        #replace entries in dictionary with underscore with entry without underscore and same value 
        for i in range (0,len(und_keys)):
            new_key=und_keys[i][1:]
            thing[new_key]=thing.pop(und_keys[i])
            #print ("thing= ", thing , "\n thing type = " , type(thing))
        return thing

    Here is a function that gets the id for a script (and works around that the backend of the API's use a contains clause instead of == ..

    def get_unique_script_id(role,name):
        api_key = get_token_sdk(role)
        api_instance = PureCloudPlatformClientV2.ScriptsApi(api_key)
        try:
            # Retrieve a list of all divisions defined for the organization
            api_response = api_instance.get_scripts(name=name)
            script_obj_list=(list(api_response.entities))
            script_list=[]
            name_list=[]
            #this loop iterates through the response and converts the class objects back into dictionaries and produces a list of the names of the scripts that were returned (e.g. ['DefaultOutbound_RED2', 'DefaultOutbound_RED'])
            for i in range(0,len(script_obj_list)):
                sub_list=clean_object(script_obj_list[i])
                script_list.append(sub_list)
                #print(type(script_list),script_list)
                name_list.append(sub_list.get('name'))
            #print (name_list)
            #find the index of the exact match and return that id
            try:
                name_idx=name_list.index(name)
                print (name_idx)
                script_id=script_list[name_idx]['id']
                print (script_id)
                #print ("division id = ",script_id)
                return script_id
            except:
                script_id = -1 
                return script_id 
                #sys.exit()
        except ApiException as e:
            print("Exception when calling ScriptsApi->get_scripts %s\n" % e)
            sys.exit()

    Declan_ginty | 2023-10-26 11:17:25 UTC | #2

    Hi,

    I have created a ticket for us to investigate this, thanks for pointing it out.

    Regards, Declan


    AllanK | 2023-10-30 01:39:33 UTC | #3

    Thanks

    Is there a version I should role back my env to?


    system | 2023-11-30 01:40:09 UTC | #4

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 22732