0

I have the following code

def generate_matter(leaver_user,vaultAccessToken):
    for user in leaver_user:
        url = "https://vault.googleapis.com/v1/matters/"

        headers = {
        "Accept" : "application/json",
        "Content-Type" : "application/json",
        "Authorization": "Bearer " + vaultAccessToken
        }

        body = json.dumps ({           
        "state": "OPEN",
        "description": "Generated by Python",
        "name": user + "'s archive"
        })

        response = requests.request(
        "POST",
        url,
        headers=headers,
        data=body
        )

        jsonContent = json.loads(response.text)
        matterID=jsonContent["matterId"]
        #print("Matter ID for " + user + " is " + matterID)
        #print(jsonContent)

        matter={
            "matterInstance": {
                "user": user,
                "userInfo": {
                    "matterID": matterID
            }
            
            }
        }

    return matter

def generate_search_query(matter,leaver_user,vaultAccessToken):
    print(matter)
    for key, value in matter.items():
        user=(matter['matterInstance']['user'])
        matterID=(matter['matterInstance']['userInfo']['matterID'])
        url = "https://vault.googleapis.com/v1/matters/"+matterID+"/savedQueries"
        
        headers = {
        "Accept" : "application/json",
        "Content-Type" : "application/json",
        "Authorization": "Bearer " + vaultAccessToken
        }

        body=json.dumps({
            "displayName": user + "'s email search query",
            "query": {
                "corpus": "MAIL",
                "dataScope": "ALL_DATA",
                "searchMethod": "ACCOUNT",
                "accountInfo": { "emails": [user]},
                "mailOptions": {"excludeDrafts" : "false"},
                "timeZone": "Atlantic/Canary",
                "method": "ACCOUNT"
        }}
        )

        response = requests.request(
        "POST",
        url,
        headers=headers,
        data=body
        )
        jsonContent = json.loads(response.text)
        print(matterID)
        print(body)
        print(jsonContent)
        savedQueryID=jsonContent["savedQueryId"]
        print("savedQueryId for " + user + " is " + savedQueryID + " matterID is " + matterID)
        

        matter={
            "matterInstance": {
                "user": user,
                "userInfo": {
                    "matterID": matterID,
                    "savedQueryID": savedQueryID
            }
            
            }
        }

    return matter
matter=generate_matter(leaver_user,vaultAccessToken)
savedQueryID=generate_search_query(matter,leaver_user,vaultAccessToken)

What works is the first function - generate_matter().

This returns multiple instances of matter such as

{'matterInstance': {'user': '[email protected]', 'userInfo': {'matterID': '12-34-56-78-91'}}}
{'matterInstance': {'user': '[email protected]', 'userInfo': {'matterID': '12-34-56-78-99'}}}

However the function generate_search_query() only seems to execute on the first instance of matter.

I've confirmed this by printing the matter in generate_search_query() before the for loop executes and only the first instance of matter is returned.

 {'matterInstance': {'user': '[email protected]', 'userInfo': {'matterID': '12-34-56-78-91'}}}

Adding from the below comments as its useful information.

Printing matter within the for loop from generate_matter does return multiple instances of matter.

Printing matter immediately before calling savedQueryID=generate_search_query(matter,leaver_user,vaultAccessToken) only returns a single instance of matter, so this is when i print / call it outside of the function

How would I solve this so that multiple instances of matter are executed in the for loop within generate_search_query() ?

Thanks

2
  • So printing matter immediately before calling savedQueryID=generate_search_query(matter,leaver_user,vaultAccessToken) prints multiple lines, but then printing on the first line of the function prints only one line? Commented Dec 29, 2021 at 14:09
  • @yuuuu thanks for the suggestion. Printing matter before savedQueryID=generate_search_query(matter,leaver_user,vaultAccessToken) only prints a single instance of matter. Printing matter within the for loop of generate_matter() does return multiple instances of matter. Commented Dec 29, 2021 at 14:12

1 Answer 1

1

At the end of generate_matter() you're overwriting matter with the last iteration, and then returning it, so its only returning a single element.

To fix this, create a list at the start of generate_matter() (matterList = []) and then where you have matter={...} in generate_matter() replace it with matterList.append({...}). Then at the end of the function return matterList instead of matter.

In generate_search_query() you'll need to wrap everything in another for loop to loop through the list.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, can you clarify what i need to specify in the first for loop for generate_search_query()? thanks
If the code you have now works correctly for a single element, then all you'll need to do is change the definition to def generate_search_query(matterList..... and add for matter in matterList: around everything that is in that function now.
I've just noticed from the new for loop that it keeps iterating through for matter in matterList: even though its already completed it for the item in the list. I've made sure the return matterList is outside of both for loops. Any ideas?
No worries fixed it with for matter in matterList: matterList = []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.