Skip to content

Commit 8a9e501

Browse files
committed
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.
1 parent fd84619 commit 8a9e501

6 files changed

Lines changed: 161 additions & 10 deletions

File tree

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: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

src/main/kotlin/com/nylas/NylasClient.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ open class NylasClient(
184184
overrides: RequestOverrides? = null,
185185
): T {
186186
val url = buildUrl(path, queryParams, overrides)
187+
println("Executing GET request to ${url.build()}")
187188
return executeRequest(url, HttpMethod.GET, null, resultType, overrides)
188189
}
189190

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)