Asana / python-asana

Official Python client library for the Asana API v1
MIT License
299 stars 103 forks source link

How to receive next sync token using EventsApi#get_events #199

Open trunneml opened 2 months ago

trunneml commented 2 months ago

Your example only shows how to receive events when we already have the sync token. But atleast your example does not provide the new sync token for the next call.

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 an instance of the API class
events_api_instance = asana.EventsApi(api_client)
resource = "12345" # str | A resource ID to subscribe to. The resource can be a task, project, or goal.
opts = {
    'sync': "de4774f6915eae04714ca93bb2f5ee81", # str | A sync token received from the last request, or none on first sync. Events will be returned from the point in time that the sync token was generated. *Note: On your first request, omit the sync token. The response will be the same as for an expired sync token, and will include a new valid sync token.If the sync token is too old (which may happen from time to time) the API will return a `412 Precondition Failed` error, and include a fresh sync token in the response.*
    'opt_fields': "action,change,change.action,change.added_value,change.field,change.new_value,change.removed_value,created_at,parent,parent.name,resource,resource.name,type,user,user.name", # list[str] | This endpoint returns a compact resource, which excludes some properties by default. To include those optional properties, set this query parameter to a comma-separated list of the properties you wish to include.
}

try:
    # Get events on a resource
    api_response = events_api_instance.get_events(resource, opts)
    for data in api_response:
        pprint(data)
except ApiException as e:
    print("Exception when calling EventsApi->get_events: %s\n" % e)

Without the sync token this part of the API is not that helpful.

trunneml commented 2 months ago

I think removing .items() and returning the full EventIterator in https://github.com/Asana/python-asana/blob/bab9fe81d808ced6788ae5464ae075db8105f184/asana/api/events_api.py#L161 should fix this problem. But this would be a breaking API change.

jv-asana commented 2 months ago

Hi @trunneml,

See my comment for a workaround here. Thanks for pointing this out. I think it's probably best for us not to try to auto paginate our events endpoint and let users handle how they want to call our events endpoint.

The problem with auto paginating our events endpoint is we have to decide when to stop the iteration. So we could either stop the iteration when the Asana API returns has_more is false OR keep calling the next sync token making the get_events method turn into an event stream where it constantly fetches and watches for new events. If we go with the event stream approach we'll need to add some kind of delay otherwise it'll hit the Asana API rate limits.

What's your use case with our events endpoint? And how would you prefer this endpoint behaves for python?