passing parameter in Rest API request from other file or list variable- using

869 Views Asked by At

new to python and API.
i have list of values like below typeid=['1','12','32','1000','9']

I have to pass this value as parameter in API request, so that it would take one typeid at a time and append the json. code i have following but not sure how it will move from one value to other?

# activity type id store as following in other .py file typeid=['1','12','32','1000','9']

#importing the file in main program file.

From typeid list import activitytypeids 


act1 = requests.get(host + '/rest/v1/activities.json',
                    params={
                            'activityTypeIds': activitytypeids[0]
                            }).text

json_obj = json.loads(act1)
results.append(json_obj)
more_result = json_obj['moreResult']
while True:
    act1 = requests.get(host + '/rest/v1/activities.json',
                        params={
                                'activityTypeIds': activitytypeids[0]
                                }).text
    json_obj = json.loads(act1)
    results.append(json_obj)
    more_result =json(results['moreResult'])

    if not more_result:
        break

How do I pass the activity's in request param one by one, so that get the result of all type ids.

1

There are 1 best solutions below

0
Joran Beasley On

take your code to get one id and put it in a function that accepts an activity_id, and change all activitytypeids[0] to just be activity_id

From typeid list import activitytypeids

def get_activity_id(activity_id):
    act1 = requests.get(host + '/rest/v1/activities.json',
                    params={
                            'activityTypeIds': activity_id
                            }).text
    return act1.json()

then you can just iterate over your list

results = [get_activity_id(id) for id in activitytypeids]

that said it seems very surprising that a variable named activityTypeIds only accepts one id ... i would very much expect this to be able to accept a list based on nothing more than the variable name