|
| 1 | +package com.nylas.examples; |
| 2 | + |
| 3 | +import com.nylas.NylasClient; |
| 4 | +import com.nylas.models.NylasApiError; |
| 5 | +import com.nylas.models.NylasSdkTimeoutError; |
| 6 | +import com.nylas.models.Calendar; |
| 7 | +import com.nylas.models.Event; |
| 8 | +import com.nylas.models.ListEventQueryParams; |
| 9 | +import com.nylas.models.Response; |
| 10 | +import com.nylas.models.ListResponse; |
| 11 | +import com.nylas.models.EventQueryOrderBy; |
| 12 | +import com.nylas.models.EventQueryOrderBy; |
| 13 | +import okhttp3.OkHttpClient; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +public class EventsExample { |
| 25 | + public static void main(String[] args) { |
| 26 | + try { |
| 27 | + // Load configuration from environment variables or .env file |
| 28 | + Map<String, String> config = loadConfig(); |
| 29 | + |
| 30 | + // Initialize the Nylas client with your API key |
| 31 | + NylasClient nylas = new NylasClient( |
| 32 | + config.get("NYLAS_API_KEY"), |
| 33 | + new OkHttpClient.Builder(), |
| 34 | + config.getOrDefault("NYLAS_API_URI", "https://api.us.nylas.com") |
| 35 | + ); |
| 36 | + |
| 37 | + // Run the example workflow |
| 38 | + runEventsExample(nylas, config); |
| 39 | + |
| 40 | + // Exit successfully |
| 41 | + System.exit(0); |
| 42 | + |
| 43 | + } catch (NylasApiError e) { |
| 44 | + System.out.println("\n❌ Nylas API Error: " + e.getMessage()); |
| 45 | + System.out.println(" Status code: " + e.getStatusCode()); |
| 46 | + System.out.println(" Request ID: " + e.getRequestId()); |
| 47 | + System.exit(1); |
| 48 | + } catch (IllegalArgumentException e) { |
| 49 | + System.out.println("\n❌ Configuration Error: " + e.getMessage()); |
| 50 | + System.exit(1); |
| 51 | + } catch (Exception e) { |
| 52 | + System.out.println("\n❌ Unexpected Error: " + e.getMessage()); |
| 53 | + e.printStackTrace(); |
| 54 | + System.exit(1); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Loads configuration from environment variables and .env file |
| 60 | + * @throws IllegalArgumentException if required configuration is missing |
| 61 | + */ |
| 62 | + private static Map<String, String> loadConfig() { |
| 63 | + Map<String, String> config = new HashMap<>(); |
| 64 | + |
| 65 | + // Try loading from environment variables first |
| 66 | + System.getenv().entrySet().stream() |
| 67 | + .filter(entry -> entry.getKey().startsWith("NYLAS_") || entry.getKey().equals("MEETING_LINK")) |
| 68 | + .forEach(entry -> config.put(entry.getKey(), entry.getValue())); |
| 69 | + |
| 70 | + // Then try loading from .env file if needed |
| 71 | + List<String> envPaths = Arrays.asList("examples/.env", ".env"); |
| 72 | + for (String path : envPaths) { |
| 73 | + File envFile = new File(path); |
| 74 | + if (envFile.exists()) { |
| 75 | + System.out.println("📝 Loading configuration from " + envFile.getAbsolutePath()); |
| 76 | + try { |
| 77 | + Files.lines(envFile.toPath()) |
| 78 | + .filter(line -> !line.trim().isEmpty() && !line.startsWith("#")) |
| 79 | + .forEach(line -> { |
| 80 | + String[] parts = line.split("=", 2); |
| 81 | + if (parts.length == 2) { |
| 82 | + String key = parts[0].trim(); |
| 83 | + String value = parts[1].trim(); |
| 84 | + if (!config.containsKey(key)) { |
| 85 | + config.put(key, value); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | + } catch (IOException e) { |
| 90 | + System.out.println("Warning: Failed to load .env file: " + e.getMessage()); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Validate required configuration |
| 96 | + List<String> requiredKeys = Arrays.asList("NYLAS_API_KEY", "MEETING_LINK"); |
| 97 | + List<String> missingKeys = requiredKeys.stream() |
| 98 | + .filter(key -> !config.containsKey(key)) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + |
| 101 | + if (!missingKeys.isEmpty()) { |
| 102 | + throw new IllegalArgumentException( |
| 103 | + "Missing required configuration: " + String.join(", ", missingKeys) + "\n" + |
| 104 | + "Please set these in examples/.env or as environment variables." |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + return config; |
| 109 | + } |
| 110 | + |
| 111 | + private static void runEventsExample(NylasClient nylas, Map<String, String> config) throws NylasApiError, NylasSdkTimeoutError { |
| 112 | + // Get the primary calendar |
| 113 | + Response<Calendar> primaryCalendar = nylas.calendars().find(config.get("NYLAS_GRANT_ID"), "primary"); |
| 114 | + |
| 115 | + // Get the start and end times for the query |
| 116 | + long start = (System.currentTimeMillis() / 1000L) - (60 * 60 * 24 * 30); // 30 days ago in Unix timestamp |
| 117 | + long end = (System.currentTimeMillis() / 1000L) + (60 * 60 * 24 * 30); // 30 days from now in Unix timestamp |
| 118 | + |
| 119 | + // List events |
| 120 | + ListEventQueryParams.Builder eventQueryBuilder = new ListEventQueryParams.Builder(primaryCalendar.getData().getId()) |
| 121 | + .start(String.valueOf(start)) |
| 122 | + .end(String.valueOf(end)) |
| 123 | + .expandRecurring(false) |
| 124 | + .showCancelled(true) |
| 125 | + .orderBy(EventQueryOrderBy.START) |
| 126 | + .limit(200); |
| 127 | + |
| 128 | + // Get the events |
| 129 | + ListResponse<Event> events = nylas.events().list(config.get("NYLAS_GRANT_ID"), eventQueryBuilder.build()); |
| 130 | + |
| 131 | + // Print the events |
| 132 | + System.out.println("Found " + events.getData().size() + " events"); |
| 133 | + for (Event event : events.getData()) { |
| 134 | + System.out.println(event); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments