Hi,
The python Asana library is behaving weirdly. I am stepping through tasks a few days at a time so that I can do some historical reporting. This code will work for a few years then starts failing.
Issues
How do i fix the error “asana.error.InvalidRequestError: Invalid Request: completed_on.after: Should be before completed_on.before” (details below)
How do i add “new_goal_memberships” to your “Asana-Enable” or “Asana-Disable” header? I see nothing in the python library documentation to help
what is the syntax to get 100 (or more?) results at one time? i could not get limit to change getting max 50 results.
System Details: Python 3.11, Windows 11, Premium Asana
Code
# start_date and end_date are both date variables.
time.sleep(1.5) # Search can only be called 50 per minute
task_results = client.tasks.search_tasks_for_workspace(WORKSPACE_GID,
{
'completed_on.after':start_date,
'text': 'visit',
'sort_by': 'completed_at',
'sort_ascending': 'true',
'completed_on.before':end_date,
},
opt_pretty=True)
Output
daterange:2021-01-02 to 2021-01-07
storing: items num/overall:( 6/49)
daterange:2021-01-08 to 2021-01-13
storing: items num/overall:( 3/52)
daterange:2021-01-14 to 2021-01-19
storing: items num/overall:( 3/55)
daterange:2021-01-20 to 2021-01-25
storing: items num/overall:( 5/60)
daterange:2021-01-26 to 2021-01-31
Traceback (most recent call last):
File “c:\Users\donal\OneDrive\Documents\python dev\asana stats\get_asana.py”, line 94, in
find_tasks(start_date, end_date)
File “c:\Users\donal\OneDrive\Documents\python dev\asana stats\get_asana.py”, line 30, in find_tasks
store_results(task_results)
File “c:\Users\donal\OneDrive\Documents\python dev\asana stats\get_asana.py”, line 40, in store_results
for index, element in enumerate(task_results):
File “C:\Program Files\Python311\Lib\site-packages\asana\page_iterator.py”, line 58, in items
for page in self:
File “C:\Program Files\Python311\Lib\site-packages\asana\page_iterator.py”, line 40, in next
result = self.get_initial()
^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\site-packages\asana\page_iterator.py”, line 69, in get_initial
return self.client.get(self.path, self.query, **self.options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\site-packages\asana\client.py”, line 174, in get
return self.request(‘get’, path, params=query, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\site-packages\asana\client.py”, line 92, in request
raise STATUS_MAPresponse.status_code
asana.error.InvalidRequestError: Invalid Request: completed_on.after: Should be before completed_on.before
That’s expected, when running a search you have to play with the creation date of the tasks to be able to request the next batch. See Search tasks in a workspace
start:2021-01-20 end:2021-01-25
storing items num/overall:( 3/55)
daterange:2021-01-20 to 2021-01-25
start:2021-01-26 end:2021-01-31
storing items num/overall:( 5/60)
daterange:2021-01-26 to 2021-01-31
start:2021-02-01 end:2021-01-31
Traceback (most recent call last):
File “c:\Users\donal\OneDrive\Documents\python dev\asana stats\get_asana.py”, line 95, in
find_tasks(start_date, end_date)
File “c:\Users\donal\OneDrive\Documents\python dev\asana stats\get_asana.py”, line 31, in find_tasks
store_results(task_results)
File “c:\Users\donal\OneDrive\Documents\python dev\asana stats\get_asana.py”, line 41, in store_results
for index, element in enumerate(task_results):
File “C:\Program Files\Python311\Lib\site-packages\asana\page_iterator.py”, line 58, in items
for page in self:
File “C:\Program Files\Python311\Lib\site-packages\asana\page_iterator.py”, line 40, in next
result = self.get_initial()
^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\site-packages\asana\page_iterator.py”, line 69, in get_initial
return self.client.get(self.path, self.query, **self.options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\site-packages\asana\client.py”, line 174, in get
return self.request(‘get’, path, params=query, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\site-packages\asana\client.py”, line 92, in request
raise STATUS_MAPresponse.status_code
asana.error.InvalidRequestError: Invalid Request: completed_on.after: Should be before completed_on.before
i thought i could find the answer without reading source code from the library.
After looking at the source code, i still don’t know how to change headers. Care to point in the right direction.
My python asana code is:
limit of 50 means the program makes twice the calls and takes twice as long to run. Getting data for 4 days at a time for 5 years means close to 10 minutes runtime. (as the search task can only run 50 times/minute, even on paid platform).
Doubling that limit to 100 would halve the number of calls and halve the time.
So you got the latest lib but it doesn’t have the function we see on GitHub?
Anyway, the limit is definitely part of the search parameters… I always assumed it worked but maybe all this time I was not getting 100 results
Hmm is right. I used python pip to install. i don’t know how to tell where pip sourced the files.
Strange to have 2 versions.
For limit, i tried the other way and limited to 5 results for testing. It always returned more. The git docs refer to using item_limit:# so i will try that later. Note the Asana doc refers to the parameter as limit as per my above code