diff --git a/CHANGELOG.md b/CHANGELOG.md index bcc85f80..3f40189a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,13 @@ # Nylas Java SDK Changelog + ## [Unreleased] ### Added -* Added support for `name` attribute in Scheduler Configuration class to allow custom naming of Scheduling Pages +* Support for `name` attribute in Scheduler Configuration class to allow custom naming of Scheduling Pages + +### 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 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..94c281f2 --- /dev/null +++ b/examples/src/main/java/com/nylas/examples/EventsExample.java @@ -0,0 +1,136 @@ +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 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/models/EventQueryOrderBy.kt b/src/main/kotlin/com/nylas/models/EventQueryOrderBy.kt new file mode 100644 index 00000000..f8e8ef5e --- /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, +} 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.