-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sync token is only updated on the first EventIterator#next call #200
Comments
Hi @trunneml, Thank you for the suggestion. I do think there are some issues with us trying to implement auto pagination with our events endpoint though. We might have to reconsider our implementation or remove it for the events endpoint. There are still issues even with your suggested fix. For example, let's say we have 101 events. This will call Get events on a resource (GET /events) endpoint twice with the same sync token and then new sync token after. We ideally don't want to be making the same call twice. Here's a scenario to explain this situation: Let's say this is the suggested change:
Since "Asana limits a single sync token to 100 events" if we have 101 tasks the
The second time it runs it'll skip the stop iteration block since
Then at some point it'll hit your change and store the new sync token for the next API call:
After that i'll return the same events as the first call since it gets to the following line:
Since The issue in this scenario is |
Another reason why it might be better for us not to auto paginate events is because users might have a preference on how they implement events. OPTION 1: Event stream -> keep calling GET /events and don't stop
OPTION 2: Get events one time:
As a work around to your issue, I recommend you use our Here's some sample code on how to do this: import json
import time
import asana
from asana.rest import ApiException
from pprint import pprint
configuration = asana.Configuration()
configuration.access_token = '<YOUR_ACCESS_TOKEN>'
api_client = asana.ApiClient(configuration)
# create # create an instance of the API class
events_api_instance = asana.EventsApi(api_client)
resource = "<TASK_GID>" # EX: task_gid
sync = None
opts = {}
# First API call to get_events this will fail and return us a sync token
try:
events = events_api_instance.get_events(resource, opts, full_payload=True)
pprint(events)
except Exception as e:
if (e.status == 412):
print("Saving sync token")
errors = json.loads(e.body.decode("utf-8"))
sync = errors["sync"]
else:
print("Exception when calling TasksApi->get_tasks: %s\n" % e)
# Set a 10 second delay for you to trigger an event in Asana (EX: add comment to task)
time.sleep(10)
# Make a follow up API call to get_events with the sync token
try:
opts = {
'sync': sync
}
events = events_api_instance.get_events(resource, opts, full_payload=True)
pprint(events)
except Exception as e:
print("Exception when calling TasksApi->get_tasks: %s\n" % e) Sample terminal output: Saving sync token
{'data': [{'action': 'added',
'created_at': '2024-07-29T20:46:09.314Z',
'parent': {'gid': '123',
'name': 'Task 1',
'resource_subtype': 'default_task',
'resource_type': 'task'},
'resource': {'created_at': '2024-07-29T20:46:09.097Z',
'created_by': {'gid': '456',
'name': '[email protected]',
'resource_type': 'user'},
'gid': '789',
'resource_type': 'story',
'text': 'hello',
'type': 'comment'},
'type': 'story',
'user': {'gid': '456',
'name': '[email protected]',
'resource_type': 'user'}}],
'has_more': False,
'sync': '9ldeb44b7c90d6sb6da91m8b7e6fad0c:0'} |
When calling the EventIterator the sync token is only updated the first time.
It looks like that the else block in line 28 should be part of the if block in line 31.
python-asana/asana/pagination/event_iterator.py
Line 26 in bab9fe8
The text was updated successfully, but these errors were encountered: