Skip to content

Commit f2bacd3

Browse files
authored
Fixed ListEventQueryParams to use a more specific type for orderBy parameter and correct JSON name. (#276)
# What did you do? - 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. # License <!-- Your PR comment must contain the following line for us to merge the PR. --> I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.
1 parent 9d72614 commit f2bacd3

File tree

6 files changed

+164
-11
lines changed

6 files changed

+164
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Nylas Java SDK Changelog
22

3+
34
## [Unreleased]
45

56
### Added
6-
* Added support for `name` attribute in Scheduler Configuration class to allow custom naming of Scheduling Pages
7+
* Support for `name` attribute in Scheduler Configuration class to allow custom naming of Scheduling Pages
8+
9+
### Fixed
10+
* ListEventQueryParams to use a more specific type for orderBy parameter and correct JSON name.
711

812
### [2.8.0] - Release 2025-04-30
913
* Added support for Notetaker APIs

examples/Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ help:
77
@echo "Available targets:"
88
@echo " help - Show this help message"
99
@echo " list - List available examples"
10-
@echo " java - Run the Java Notetaker example"
11-
@echo " kotlin - Run the Kotlin Notetaker example"
10+
@echo " java-notetaker - Run the Java Notetaker example"
11+
@echo " java-events - Run the Java Events example"
12+
@echo " kotlin-notetaker - Run the Kotlin Notetaker example"
1213

1314
# List available examples
1415
list:
1516
@cd .. && ./gradlew :examples:listExamples
1617

1718
# Run the Java example
18-
java:
19+
java-notetaker:
1920
@cd .. && ./gradlew :examples:run -PmainClass=com.nylas.examples.NotetakerExample
2021

22+
java-events:
23+
@cd .. && ./gradlew :examples:run -PmainClass=com.nylas.examples.EventsExample
24+
2125
# Run the Kotlin example
22-
kotlin:
26+
kotlin-notetaker:
2327
@cd .. && ./gradlew :examples:run -PmainClass=com.nylas.examples.KotlinNotetakerExampleKt

examples/build.gradle.kts

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

src/main/kotlin/com/nylas/models/ListEventQueryParams.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ data class ListEventQueryParams(
8080
* Order results by the specified field.
8181
* Currently only start is supported.
8282
*/
83-
@Json(name = "participants")
84-
val orderBy: String? = null,
83+
@Json(name = "order_by")
84+
val orderBy: EventQueryOrderBy? = null,
8585
/**
8686
* Filter for events with the specified ical_uid.
8787
* You cannot apply other filters if you use this parameter.
@@ -145,7 +145,7 @@ data class ListEventQueryParams(
145145
private var metadataPair: Map<String, String>? = null
146146
private var expandRecurring: Boolean? = null
147147
private var busy: Boolean? = null
148-
private var orderBy: String? = null
148+
private var orderBy: EventQueryOrderBy? = null
149149
private var icalUid: String? = null
150150
private var masterEventId: String? = null
151151
private var updatedBefore: Long? = null
@@ -243,7 +243,7 @@ data class ListEventQueryParams(
243243
* @param orderBy The field to order results by.
244244
* @return The builder.
245245
*/
246-
fun orderBy(orderBy: String?) = apply { this.orderBy = orderBy }
246+
fun orderBy(orderBy: EventQueryOrderBy?) = apply { this.orderBy = orderBy }
247247

248248
/**
249249
* Sets the ical_uid to filter for events with.

0 commit comments

Comments
 (0)