From 8a9e50102a9c8521d57e9faaea5ea05c5816beee Mon Sep 17 00:00:00 2001 From: Aaron de Mello Date: Tue, 20 May 2025 09:45:25 -0400 Subject: [PATCH 1/3] Fixed ListEventQueryParams to use a more specific type for orderBy parameter and correct JSON name. - Updated example names in build.gradle.kts and Makefile for clarity. - Added new Java Events example to the list of available examples. - Enhanced logging in NylasClient to print the URL of GET requests. - Fixed ListEventQueryParams to use a more specific type for orderBy parameter and correct JSON name. --- examples/Makefile | 12 +- examples/build.gradle.kts | 5 +- .../com/nylas/examples/EventsExample.java | 137 ++++++++++++++++++ src/main/kotlin/com/nylas/NylasClient.kt | 1 + .../com/nylas/models/EventQueryOrderBy.kt | 8 + .../com/nylas/models/ListEventQueryParams.kt | 8 +- 6 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 examples/src/main/java/com/nylas/examples/EventsExample.java create mode 100644 src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt diff --git a/examples/Makefile b/examples/Makefile index 142aeda1..850da837 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -7,17 +7,21 @@ help: @echo "Available targets:" @echo " help - Show this help message" @echo " list - List available examples" - @echo " java - Run the Java Notetaker example" - @echo " kotlin - Run the Kotlin Notetaker example" + @echo " java-notetaker - Run the Java Notetaker example" + @echo " java-events - Run the Java Events example" + @echo " kotlin-notetaker - Run the Kotlin Notetaker example" # List available examples list: @cd .. && ./gradlew :examples:listExamples # Run the Java example -java: +java-notetaker: @cd .. && ./gradlew :examples:run -PmainClass=com.nylas.examples.NotetakerExample +java-events: + @cd .. && ./gradlew :examples:run -PmainClass=com.nylas.examples.EventsExample + # Run the Kotlin example -kotlin: +kotlin-notetaker: @cd .. && ./gradlew :examples:run -PmainClass=com.nylas.examples.KotlinNotetakerExampleKt \ No newline at end of file diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index c56eb75f..963cf2e4 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -51,8 +51,9 @@ tasks.test { tasks.register("listExamples") { doLast { println("Available examples:") - println("- Java: com.nylas.examples.NotetakerExample") - println("- Kotlin: com.nylas.examples.KotlinNotetakerExampleKt") + println("- Java-Notetaker: com.nylas.examples.NotetakerExample") + println("- Java-Events: com.nylas.examples.EventsExample") + println("- Kotlin-Notetaker: com.nylas.examples.KotlinNotetakerExampleKt") println("\nRun an example with: ./gradlew :examples:run -PmainClass=") } } diff --git a/examples/src/main/java/com/nylas/examples/EventsExample.java b/examples/src/main/java/com/nylas/examples/EventsExample.java new file mode 100644 index 00000000..d4d42fa1 --- /dev/null +++ b/examples/src/main/java/com/nylas/examples/EventsExample.java @@ -0,0 +1,137 @@ +package com.nylas.examples; + +import com.nylas.NylasClient; +import com.nylas.models.NylasApiError; +import com.nylas.models.NylasSdkTimeoutError; +import com.nylas.models.Calendar; +import com.nylas.models.Event; +import com.nylas.models.ListEventQueryParams; +import com.nylas.models.Response; +import com.nylas.models.ListResponse; +import com.nylas.models.EventQueryOrderBy; +import com.nylas.models.EventQueryOrderBy; +import okhttp3.OkHttpClient; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class EventsExample { + public static void main(String[] args) { + try { + // Load configuration from environment variables or .env file + Map config = loadConfig(); + + // Initialize the Nylas client with your API key + NylasClient nylas = new NylasClient( + config.get("NYLAS_API_KEY"), + new OkHttpClient.Builder(), + config.getOrDefault("NYLAS_API_URI", "https://api.us.nylas.com") + ); + + // Run the example workflow + runEventsExample(nylas, config); + + // Exit successfully + System.exit(0); + + } catch (NylasApiError e) { + System.out.println("\nāŒ Nylas API Error: " + e.getMessage()); + System.out.println(" Status code: " + e.getStatusCode()); + System.out.println(" Request ID: " + e.getRequestId()); + System.exit(1); + } catch (IllegalArgumentException e) { + System.out.println("\nāŒ Configuration Error: " + e.getMessage()); + System.exit(1); + } catch (Exception e) { + System.out.println("\nāŒ Unexpected Error: " + e.getMessage()); + e.printStackTrace(); + System.exit(1); + } + } + + /** + * Loads configuration from environment variables and .env file + * @throws IllegalArgumentException if required configuration is missing + */ + private static Map loadConfig() { + Map config = new HashMap<>(); + + // Try loading from environment variables first + System.getenv().entrySet().stream() + .filter(entry -> entry.getKey().startsWith("NYLAS_") || entry.getKey().equals("MEETING_LINK")) + .forEach(entry -> config.put(entry.getKey(), entry.getValue())); + + // Then try loading from .env file if needed + List envPaths = Arrays.asList("examples/.env", ".env"); + for (String path : envPaths) { + File envFile = new File(path); + if (envFile.exists()) { + System.out.println("šŸ“ Loading configuration from " + envFile.getAbsolutePath()); + try { + Files.lines(envFile.toPath()) + .filter(line -> !line.trim().isEmpty() && !line.startsWith("#")) + .forEach(line -> { + String[] parts = line.split("=", 2); + if (parts.length == 2) { + String key = parts[0].trim(); + String value = parts[1].trim(); + if (!config.containsKey(key)) { + config.put(key, value); + } + } + }); + } catch (IOException e) { + System.out.println("Warning: Failed to load .env file: " + e.getMessage()); + } + } + } + + // Validate required configuration + List requiredKeys = Arrays.asList("NYLAS_API_KEY", "MEETING_LINK"); + List missingKeys = requiredKeys.stream() + .filter(key -> !config.containsKey(key)) + .collect(Collectors.toList()); + + if (!missingKeys.isEmpty()) { + throw new IllegalArgumentException( + "Missing required configuration: " + String.join(", ", missingKeys) + "\n" + + "Please set these in examples/.env or as environment variables." + ); + } + + return config; + } + + private static void runEventsExample(NylasClient nylas, Map config) throws NylasApiError, NylasSdkTimeoutError { + // Get the primary calendar + Response primaryCalendar = nylas.calendars().find(config.get("NYLAS_GRANT_ID"), "primary"); + + // Get the start and end times for the query + long start = (System.currentTimeMillis() / 1000L) - (60 * 60 * 24 * 30); // 30 days ago in Unix timestamp + long end = (System.currentTimeMillis() / 1000L) + (60 * 60 * 24 * 30); // 30 days from now in Unix timestamp + + // List events + ListEventQueryParams.Builder eventQueryBuilder = new ListEventQueryParams.Builder(primaryCalendar.getData().getId()) + .start(String.valueOf(start)) + .end(String.valueOf(end)) + .expandRecurring(false) + .showCancelled(true) + .orderBy(EventQueryOrderBy.START) + .limit(200); + + // Get the events + ListResponse events = nylas.events().list(config.get("NYLAS_GRANT_ID"), eventQueryBuilder.build()); + + // Print the events + System.out.println("Found " + events.getData().size() + " events"); + for (Event event : events.getData()) { + System.out.println(event); + } + } +} diff --git a/src/main/kotlin/com/nylas/NylasClient.kt b/src/main/kotlin/com/nylas/NylasClient.kt index 786ffa4a..2248c8a0 100644 --- a/src/main/kotlin/com/nylas/NylasClient.kt +++ b/src/main/kotlin/com/nylas/NylasClient.kt @@ -184,6 +184,7 @@ open class NylasClient( overrides: RequestOverrides? = null, ): T { val url = buildUrl(path, queryParams, overrides) + println("Executing GET request to ${url.build()}") return executeRequest(url, HttpMethod.GET, null, resultType, overrides) } diff --git a/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt b/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt new file mode 100644 index 00000000..6d35f07d --- /dev/null +++ b/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt @@ -0,0 +1,8 @@ +package com.nylas.models + +import com.squareup.moshi.Json + +enum class EventQueryOrderBy { + @Json(name = "start") + START, +} \ No newline at end of file diff --git a/src/main/kotlin/com/nylas/models/ListEventQueryParams.kt b/src/main/kotlin/com/nylas/models/ListEventQueryParams.kt index da2f059a..3b7f81ec 100644 --- a/src/main/kotlin/com/nylas/models/ListEventQueryParams.kt +++ b/src/main/kotlin/com/nylas/models/ListEventQueryParams.kt @@ -80,8 +80,8 @@ data class ListEventQueryParams( * Order results by the specified field. * Currently only start is supported. */ - @Json(name = "participants") - val orderBy: String? = null, + @Json(name = "order_by") + val orderBy: EventQueryOrderBy? = null, /** * Filter for events with the specified ical_uid. * You cannot apply other filters if you use this parameter. @@ -145,7 +145,7 @@ data class ListEventQueryParams( private var metadataPair: Map? = null private var expandRecurring: Boolean? = null private var busy: Boolean? = null - private var orderBy: String? = null + private var orderBy: EventQueryOrderBy? = null private var icalUid: String? = null private var masterEventId: String? = null private var updatedBefore: Long? = null @@ -243,7 +243,7 @@ data class ListEventQueryParams( * @param orderBy The field to order results by. * @return The builder. */ - fun orderBy(orderBy: String?) = apply { this.orderBy = orderBy } + fun orderBy(orderBy: EventQueryOrderBy?) = apply { this.orderBy = orderBy } /** * Sets the ical_uid to filter for events with. From 5f699418455a767bff9dc01c30facb98f44770a7 Mon Sep 17 00:00:00 2001 From: Aaron de Mello Date: Tue, 20 May 2025 09:46:21 -0400 Subject: [PATCH 2/3] Updated changelog --- CHANGELOG.md | 3 +++ examples/src/main/java/com/nylas/examples/EventsExample.java | 1 - src/main/kotlin/com/nylas/NylasClient.kt | 1 - 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c6bbd80..0ad5fb3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Nylas Java SDK Changelog +### Unreleased +* Fixed ListEventQueryParams to use a more specific type for orderBy parameter and correct JSON name. + ### [2.8.0] - Release 2025-04-30 * Added support for Notetaker APIs * Added support for Notetaker via the calendar and event APIs diff --git a/examples/src/main/java/com/nylas/examples/EventsExample.java b/examples/src/main/java/com/nylas/examples/EventsExample.java index d4d42fa1..94c281f2 100644 --- a/examples/src/main/java/com/nylas/examples/EventsExample.java +++ b/examples/src/main/java/com/nylas/examples/EventsExample.java @@ -9,7 +9,6 @@ import com.nylas.models.Response; import com.nylas.models.ListResponse; import com.nylas.models.EventQueryOrderBy; -import com.nylas.models.EventQueryOrderBy; import okhttp3.OkHttpClient; import java.io.File; diff --git a/src/main/kotlin/com/nylas/NylasClient.kt b/src/main/kotlin/com/nylas/NylasClient.kt index 2248c8a0..786ffa4a 100644 --- a/src/main/kotlin/com/nylas/NylasClient.kt +++ b/src/main/kotlin/com/nylas/NylasClient.kt @@ -184,7 +184,6 @@ open class NylasClient( overrides: RequestOverrides? = null, ): T { val url = buildUrl(path, queryParams, overrides) - println("Executing GET request to ${url.build()}") return executeRequest(url, HttpMethod.GET, null, resultType, overrides) } From f22d25f826dc8cebdfbfca4056ce8c85b1ba1c84 Mon Sep 17 00:00:00 2001 From: Aaron de Mello Date: Tue, 20 May 2025 09:51:14 -0400 Subject: [PATCH 3/3] Fix lint errors --- src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt b/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt index 6d35f07d..f8e8ef5e 100644 --- a/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt +++ b/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt @@ -5,4 +5,4 @@ import com.squareup.moshi.Json enum class EventQueryOrderBy { @Json(name = "start") START, -} \ No newline at end of file +}