1

I am trying to export all tasks from all of my asana work-spaces using python-asana API. But at some point it exists after giving the following error message.

Traceback (most recent call last):
  File "export.py", line 56, in <module>
    for index, task in enumerate(tasks):
  File "build\bdist.win32\egg\asana\page_iterator.py", line 58, in items
  File "build\bdist.win32\egg\asana\page_iterator.py", line 54, in next
  File "build\bdist.win32\egg\asana\page_iterator.py", line 43, in __next__
  File "build\bdist.win32\egg\asana\page_iterator.py", line 74, in get_next
  File "build\bdist.win32\egg\asana\client.py", line 104, in get
  File "build\bdist.win32\egg\asana\client.py", line 75, in request
asana.error.InvalidRequestError: Invalid Request: Your pagination token has expired.

I read that to solve this we need to make paginated requests. But I tried passing only offset to my request as following:

tasks = client.tasks.find_all({'project' : project['id']}, limit=50)

But, there was no difference as I was not getting any 'next_page' value even though there was more than 50 tasks in the project.

So my question is:

How can I do paginated request using python-asana API? An explanation with an example would be best!

EDIT:

I am fetching the tasks as below:

tasks = client.tasks.find_all({'project' : project['id']}, item_limit=1)
print "Tasks", tasks    # Prints generator object
for index, task in enumerate(tasks):
    complete_task = client.tasks.find_by_id(task["id"])
    print complete_task    #Prints complete task dictionary

Now My question is where will I get the next_page content for the remaining tasks and how do I access it.

user2109788
  • 1,266
  • 2
  • 12
  • 29
  • 2
    Hi could you provide more detail on how you are using the results of tasks. Also you want to use item_limit instead of limit. this works for me tasks = client.tasks.find_all({'project' : }, item_limit=1) for task in tasks: print task – Mark Sep 22 '16 at 22:30
  • @Mark Thanks for the reply. After using item_limit now I get only limited number of tasks. I used the same lines as you mentioned in your comment to get the tasks. But now how do I get the next_page data? I get only 50 tasks(generator object that has 50 tasks) without any URL/link to the next_page as mentioned in the doc. Can you please give an example? – user2109788 Sep 23 '16 at 11:12
  • @Mark After getting the tasks I iterate over the generator object as follows: for index, task in enumerate(tasks): complete_task = client.tasks.find_by_id(task["id"]); print task – user2109788 Sep 23 '16 at 11:20
  • The next_page stuff should be transparent to you when you use it like that. The underlying implementation fetches you the next page as you need it. – Mark Sep 24 '16 at 16:20
  • @Mark I tried the same. But when I use item_limit parameter it just returns me the generator object. And if I iterate over it I just get tasks list without any next_page content. I have edited my answer for the usage. Could you please give me an example how can I get the access next_page content. – user2109788 Oct 05 '16 at 09:00
  • That is expected. You don't have to interact with next_page at all. You specify how many you want, and the underlying generator object will make sure you get all the items you want without ever using next_page. If you want to go through all items, you don't need to specify a limit. The client library will do pagination under the hood for you. – Mark Oct 06 '16 at 19:06
  • @Mark When I try without specifying any limit in the request the script exits by giving the above error message. In one of my projects there are close to 500 tasks. I want to export all using python-asana api. But I am getting the above error message when I run my script. The script in "Edit" (in the above question) is what I am using to export (With or without item_limit). So any idea how can I solve the issue? – user2109788 Oct 12 '16 at 04:22
  • I don't quite understand the issue you are running into. I've just tried running thins on a large project and it seems to work: `>>> tasks = client.tasks.find_all({"project": })` `>>> for index, task in enumerate(tasks): print index, task["name"]` Can you tell me when it errors (e.g. how many did you see)? The error you have shown indicates an offset expiring, which can happen if you use an offset that is old. In this case, perhaps you somehow iterated through the items really slowly such that it expired. Also, do you have the latest version of the api client? – Mark Oct 15 '16 at 00:13
  • @Mark, For each task I am trying to fetch all the stories, attachments, subtasks etc. So it takes a lot of time to run the script and errors out. Is there anyway I can handle it? – user2109788 Oct 16 '16 at 17:57
  • 1
    Ah ok, I think I see what is probably happening. Since you fetch all the stories/attachments/subtasks in between, by the time you fetch the next page of tasks- the offset token has expired. What you can do is first save all the tasks somewhere (without the stories etc), and then iterate through your newly created list of tasks and get the stories etc for them. – Mark Oct 17 '16 at 18:33

0 Answers0