Searching tasks efficiently in large projects

I’m running into long delays when querying the API because one of my projects has a lot of completed tasks. I’m new to the API and have been searching the forum but still have questions.

Right now, to search a task with the word “job” in the name, I use a function that loops through all tasks and narrows a list down by a Levenstein distance (in python)

for workspace in me['workspaces']:
        #projects = client.projects.find_by_workspace(workspace['gid'], iterator_type=None)
        #for project in projects:
        for task in client.tasks.find_by_project(1111637993120419, {"opt_expand":"name"}):
            print (task)
            if distance(name, task['name']) <= 30:
                tasks_list.append(task)

This worked well for projects with few tasks, but is causing problems with larger projects. I looked at the function, search_in_tasks, but see from another forum discussion that it is undergoing changes. (i’m also having trouble figuring out how to name the parameters).

As an alternative, I thought about just downloading all of the tasks regularly and then searching that a list locally without having to make a request to the API.

I was hoping someone could help me figure out a better way to search tasks by name efficiently, and ideally, with the python package. Thanks and let me know if I can make this question easier to answer

Hi. I never used Python before, but for any language you use, I suggest to tryt the “task search” endpoint instead, to get your response directly. (documentation)

That query will search for the word “job” in any field, after you can select only the one containing job in your title, but the first query will be faster than downloading all your project tasks.

For Python, as described here, I suppose that it can look like this:

import requests
url = 'https://app.asana.com/api/1.0/workspaces/xxxxx/tasks/search'
headers = {"Authorization": "Bearer YOUR-ASANA-BEARER"}
params = {"text": "job"}
results = requests.get(url, params = params, headers = headers)
2 Likes

Thanks! Ok, I’m hoping someone can help me with the python request format. I tried getting only completed tasks, but I haven’t been able to figure it out

client.tasks.search_in_workspace([workspaceid], params={‘text’: {‘job’, ‘completed_since=now’}})

But, when I add the completed_since parameter, no matter when I specify, it doesn’t pick up on the tasks I’m looking for. The documentation doesn’t say how to only search for completed tasks. Thanks for any help

completed_since are parameters used by other endpoints.
When you use the “search_in_workspace”, the parameters are different, you can see all of them in the link of my first response.
You can use the completed_on.before parameter and use a future date as value.
Also, I think the parameter object is not correctly written.

Try something like this:
client.tasks.search_in_workspace(xxx, params={‘text’: ‘job’, completed_on.before: ‘2099-01-01’})

Not sure if the “dot” in the parameter can cause problems, I think I saw another person on that forum asking questions about it using Python.
If that doesn’t work, you can try with [completed_on.before]?
client.tasks.search_in_workspace(xxx, params={‘text’: ‘job’, [completed_on.before]: ‘2099-01-01’})

Again, I never did any Python, just trying to help! :slight_smile:

1 Like