1+ #!/usr/bin/env python3
2+ """
3+ Nylas SDK Example: Using Import Events
4+
5+ This example demonstrates how to use the 'list_import_events' method to import and
6+ synchronize events from a calendar within a given time frame.
7+
8+ Required Environment Variables:
9+ NYLAS_API_KEY: Your Nylas API key
10+ NYLAS_GRANT_ID: Your Nylas grant ID
11+
12+ Usage:
13+ First, install the SDK in development mode:
14+ cd /path/to/nylas-python
15+ pip install -e .
16+
17+ Then set environment variables and run:
18+ export NYLAS_API_KEY="your_api_key"
19+ export NYLAS_GRANT_ID="your_grant_id"
20+ python examples/import_events_demo/import_events_example.py
21+ """
22+
23+ import os
24+ import sys
25+ import json
26+ import time
27+ from datetime import datetime , timedelta
28+ from nylas import Client
29+
30+
31+ def get_env_or_exit (var_name : str ) -> str :
32+ """Get an environment variable or exit if not found."""
33+ value = os .getenv (var_name )
34+ if not value :
35+ print (f"Error: { var_name } environment variable is required" )
36+ sys .exit (1 )
37+ return value
38+
39+
40+ def print_data (data : list , title : str ) -> None :
41+ """Pretty print the data with a title."""
42+ print (f"\n { title } :" )
43+ for item in data :
44+ # Convert to dict and pretty print
45+ item_dict = item .to_dict ()
46+ print (json .dumps (item_dict , indent = 2 ))
47+
48+
49+ def demonstrate_basic_import (client : Client , grant_id : str ) -> None :
50+ """Demonstrate basic usage of list_import_events with primary calendar."""
51+ print ("\n === Basic Import Events ===" )
52+
53+ print ("\n Fetching events from primary calendar:" )
54+ events = client .events .list_import_events (
55+ identifier = grant_id ,
56+ query_params = {"calendar_id" : "primary" , "limit" : 2 }
57+ )
58+ print_data (events .data , "Basic import events" )
59+
60+
61+ def demonstrate_time_filtered_import (client : Client , grant_id : str ) -> None :
62+ """Demonstrate import events with time filtering."""
63+ print ("\n === Time Filtered Import Events ===" )
64+
65+ # Get timestamps for a one-month period
66+ now = int (time .time ())
67+ one_month_ago = now - (30 * 24 * 60 * 60 ) # 30 days ago
68+ one_month_future = now + (30 * 24 * 60 * 60 ) # 30 days in future
69+
70+ # Format dates for display
71+ from_date = datetime .fromtimestamp (one_month_ago ).strftime ("%Y-%m-%d" )
72+ to_date = datetime .fromtimestamp (one_month_future ).strftime ("%Y-%m-%d" )
73+
74+ print (f"\n Fetching events from { from_date } to { to_date } :" )
75+ events = client .events .list_import_events (
76+ identifier = grant_id ,
77+ query_params = {
78+ "calendar_id" : "primary" ,
79+ "start" : one_month_ago ,
80+ "end" : one_month_future
81+ }
82+ )
83+ print_data (events .data , f"Events from { from_date } to { to_date } " )
84+
85+
86+ def demonstrate_limit (client : Client , grant_id : str ) -> None :
87+ """Demonstrate import events with limit parameter."""
88+ print ("\n === Import Events with Max Results ===" )
89+
90+ print ("\n Fetching events with limit=5:" )
91+ events = client .events .list_import_events (
92+ identifier = grant_id ,
93+ query_params = {
94+ "calendar_id" : "primary" ,
95+ "limit" : 5
96+ }
97+ )
98+ print_data (events .data , "Events with limit=5" )
99+
100+
101+ def demonstrate_field_selection (client : Client , grant_id : str ) -> None :
102+ """Demonstrate import events with field selection."""
103+ print ("\n === Import Events with Field Selection ===" )
104+
105+ print ("\n Fetching events with select parameter (only id, title, and when):" )
106+ events = client .events .list_import_events (
107+ identifier = grant_id ,
108+ query_params = {
109+ "calendar_id" : "primary" ,
110+ "limit" : 2 ,
111+ "select" : "id,title,when"
112+ }
113+ )
114+ print_data (events .data , "Events with selected fields only" )
115+
116+
117+ def demonstrate_pagination (client : Client , grant_id : str ) -> None :
118+ """Demonstrate pagination for import events."""
119+ print ("\n === Import Events with Pagination ===" )
120+
121+ # First page
122+ print ("\n Fetching first page of events (limit=3):" )
123+ first_page = client .events .list_import_events (
124+ identifier = grant_id ,
125+ query_params = {
126+ "calendar_id" : "primary" ,
127+ "limit" : 3
128+ }
129+ )
130+ print_data (first_page .data , "First page of events" )
131+
132+ # If there's a next page, fetch it
133+ if hasattr (first_page , 'next_cursor' ) and first_page .next_cursor :
134+ print ("\n Fetching second page of events:" )
135+ second_page = client .events .list_import_events (
136+ identifier = grant_id ,
137+ query_params = {
138+ "calendar_id" : "primary" ,
139+ "limit" : 3 ,
140+ "page_token" : first_page .next_cursor
141+ }
142+ )
143+ print_data (second_page .data , "Second page of events" )
144+ else :
145+ print ("\n No second page available - not enough events to paginate" )
146+
147+
148+ def demonstrate_full_example (client : Client , grant_id : str ) -> None :
149+ """Demonstrate a full example with all parameters."""
150+ print ("\n === Full Import Events Example ===" )
151+
152+ # Get timestamps for the current year
153+ now = datetime .now ()
154+ start_of_year = datetime (now .year , 1 , 1 ).timestamp ()
155+ end_of_year = datetime (now .year , 12 , 31 , 23 , 59 , 59 ).timestamp ()
156+
157+ print (f"\n Fetching events for { now .year } with all parameters:" )
158+ events = client .events .list_import_events (
159+ identifier = grant_id ,
160+ query_params = {
161+ "calendar_id" : "primary" ,
162+ "limit" : 10 ,
163+ "start" : int (start_of_year ),
164+ "end" : int (end_of_year ),
165+ "select" : "id,title,description,when,participants,location"
166+ }
167+ )
168+ print_data (events .data , f"Events for { now .year } with all parameters" )
169+
170+
171+ def main ():
172+ """Main function demonstrating the import events method."""
173+ # Get required environment variables
174+ api_key = get_env_or_exit ("NYLAS_API_KEY" )
175+ grant_id = get_env_or_exit ("NYLAS_GRANT_ID" )
176+
177+ # Initialize Nylas client
178+ client = Client (
179+ api_key = api_key ,
180+ )
181+
182+ print ("\n Demonstrating Import Events Functionality" )
183+ print ("========================================" )
184+
185+ # Demonstrate different ways to use list_import_events
186+ demonstrate_basic_import (client , grant_id )
187+ demonstrate_time_filtered_import (client , grant_id )
188+ demonstrate_limit (client , grant_id )
189+ demonstrate_field_selection (client , grant_id )
190+ demonstrate_pagination (client , grant_id )
191+ demonstrate_full_example (client , grant_id )
192+
193+ print ("\n Example completed!" )
194+
195+
196+ if __name__ == "__main__" :
197+ main ()
0 commit comments