0

I've managed to install PyCharm CE and the Python Asana library (https://github.com/Asana/python-asana).

I can connect, retrieve projects, tasks and subtasks. But for tasks and subtasks, it always seem to only return the id and name.

How can I retrieve other metadata?

import asana
import json
from six import print_


# create a client with your Asana API key
client = asana.Client.basic_auth('<MyAPIkey')

me = client.users.me()
#print_("me=" + json.dumps(me, indent=2))

# find your "Personal Projects" project
# personal_projects = next(workspace for workspace in me['workspaces'] if  workspace['name'] == 'Personal Projects')
# projects = client.projects.find_by_workspace(personal_projects['id'], iterator_type=None)
# print_("personal projects=" + json.dumps(projects, indent=2))

# find "Lithium" project
lithium_projects = next(workspace for workspace in me['workspaces'] if workspace['name'] == 'lithium.com')
projects = client.projects.find_by_workspace(lithium_projects['id'], iterator_type=None)
#print_("Lithium projects=" + json.dumps(projects, indent=2))

for project in projects:
    #print_ ("id", project['id'] )
    print_ ("")
    print_ ("Project", project['name'] )
    project_id = project['id']
    project_tasks = client.tasks.find_by_project(project_id, iterator_type=None)

    for task in project_tasks:
        #print_("Tasks=" + json.dumps(task, indent=2))
        print_ ("  Task ", task['id'], ":", task['name'] )
        task_id = task['id']
        task_subtasks = client.tasks.subtasks(task_id, full_payload=True)

        for subtask in task_subtasks:
            print_("    Sub-tasks=" + json.dumps(subtask, indent=2))
            #print_ (subtask['id'], ":", subtask['name'] )

Short example of results:

Project X
  Task  32131361438409 : [Case] Title1
  Task  32131361438400 : [Case] Title2
    Sub-tasks={
    "id": 32131361438402, 
    "name": "1:1 Subtask1"
    }
Hexaholic
  • 3,299
  • 7
  • 30
  • 39

1 Answers1

2

Requests that return a collection of objects use the compact format to represent the objects.

You can use field selectors to specify exactly what metadata you would like included for each task or subtask.

For instance, if you wanted to always have the notes field included in responses your request might look like:

project_tasks = client.tasks.find_by_project(project_id, {"opt_fields":"this.notes"},  iterator_type=None)
Andrew Noonan
  • 848
  • 6
  • 13