使用Python检索Asana元数据

时间:2015-05-20 09:24:18

标签: python api asana asana-api

我设法安装了PyCharm CE和Python Asana库(https://github.com/Asana/python-asana)。

我可以连接,检索项目,任务和子任务。但对于任务和子任务,它似乎总是只返回idname

如何检索其他元数据?

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'] )

结果的简短例子:

Project X
  Task  32131361438409 : [Case] Title1
  Task  32131361438400 : [Case] Title2
    Sub-tasks={
    "id": 32131361438402, 
    "name": "1:1 Subtask1"
    }

1 个答案:

答案 0 :(得分:2)

返回对象集合的请求使用紧凑格式来表示对象。

您可以使用field selectors准确指定您希望为每个任务或子任务包含哪些元数据。

例如,如果您希望响应中包含notes字段,请求可能如下:

project_tasks = client.tasks.find_by_project(project_id, {"opt_fields":"this.notes"},  iterator_type=None)