Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Framework and NylasClient + Calendars test suites #176

Merged
merged 22 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
### Added
* Added support for sending drafts
* Added support for the contacts API
* Added enum support for OAuth prompt

### Changed
* Fixed issue with sending scheduled messages
* Fixed incorrect PKCE code challenge generation
* Fixed provider detect endpoint path
* Fixed scope encoding for OAuth URL
* Fixed typo in 'EventVisibility' enum

## [2.0.0-beta.3] - Released 2023-12-18

Expand Down
10 changes: 10 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ dependencies {

// Render dokka from the Java perspective
dokkaPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.8.20")

// Test dependencies
testImplementation(kotlin("test"))
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
testImplementation("org.mockito:mockito-inline:4.11.0")
testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0")
}

tasks.test {
useJUnitPlatform()
}

tasks.processResources {
Expand Down
40 changes: 26 additions & 14 deletions src/main/kotlin/com/nylas/NylasClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.nylas.interceptors.HttpLoggingInterceptor
import com.nylas.models.*
import com.nylas.resources.*
import com.nylas.util.JsonHelper
import com.squareup.moshi.JsonDataException
import okhttp3.*
import okhttp3.Response
import java.io.IOException
Expand Down Expand Up @@ -372,14 +373,20 @@ class NylasClient(
try {
parsedError = JsonHelper.moshi().adapter(NylasOAuthError::class.java)
.fromJson(responseBody)
} catch (e: IOException) {
throw NylasOAuthError(
error = "unknown",
errorDescription = "Unknown error received from the API: $responseBody",
errorUri = "unknown",
errorCode = "0",
statusCode = response.code(),
)
} catch (ex: Exception) {
when (ex) {
is IOException, is JsonDataException -> {
throw NylasOAuthError(
error = "unknown",
errorDescription = "Unknown error received from the API: $responseBody",
errorUri = "unknown",
errorCode = "0",
statusCode = response.code(),
)
}

else -> throw ex
}
}
} else {
try {
Expand All @@ -389,12 +396,17 @@ class NylasClient(
if (parsedError != null) {
parsedError.requestId = errorResp?.requestId
}
} catch (e: IOException) {
throw NylasApiError(
type = "unknown",
message = "Unknown error received from the API: $responseBody",
statusCode = response.code(),
)
} catch (ex: Exception) {
when (ex) {
is IOException, is JsonDataException -> {
throw NylasApiError(
type = "unknown",
message = "Unknown error received from the API: $responseBody",
statusCode = response.code(),
)
}
else -> throw ex
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/nylas/models/CreateEventRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class CreateEventRequest(
* - [CreateEventRequest.When.Timespan]
*/
@Json(name = "when")
val whenObj: When = When.Time(0),
val whenObj: When,
/**
* Creates an event with the specified title.
*/
Expand Down Expand Up @@ -84,7 +84,7 @@ data class CreateEventRequest(
* Sets the visibility for the event. The calendar default will be used if this field is omitted.
*/
@Json(name = "visibility")
val visibility: EvenVisibility? = null,
val visibility: EventVisibility? = null,
/**
* Sets the maximum number of participants that may attend the event.
*/
Expand Down Expand Up @@ -436,7 +436,7 @@ data class CreateEventRequest(
private var recurrence: List<String>? = null
private var calendarId: String? = null
private var readOnly: Boolean? = null
private var visibility: EvenVisibility? = null
private var visibility: EventVisibility? = null
private var capacity: Int? = null
private var hideParticipant: Boolean? = null

Expand Down Expand Up @@ -537,7 +537,7 @@ data class CreateEventRequest(
* @param visibility The event visibility.
* @return The builder.
*/
fun visibility(visibility: EvenVisibility) = apply { this.visibility = visibility }
fun visibility(visibility: EventVisibility) = apply { this.visibility = visibility }

/**
* Set the maximum number of participants that may attend the event.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/nylas/models/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ data class Event(
* Visibility of the event, if the event is private or public.
*/
@Json(name = "visibility")
val visibility: EvenVisibility? = null,
val visibility: EventVisibility? = null,
/**
* User who created the event.
* Not supported for all providers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.squareup.moshi.Json
/**
* Enum representation of visibility of the event, if the event is private or public.
*/
enum class EvenVisibility {
enum class EventVisibility {
@Json(name = "public")
PUBLIC,

Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/nylas/models/Prompt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.nylas.models

import com.squareup.moshi.Json

enum class Prompt {
@Json(name = "select_provider")
SELECT_PROVIDER,
@Json(name = "detect")
DETECT,
@Json(name = "select_provider,detect")
SELECT_PROVIDER_DETECT,
@Json(name = "detect,select_provider")
DETECT_SELECT_PROVIDER,
}
12 changes: 6 additions & 6 deletions src/main/kotlin/com/nylas/models/SendRsvpQueryParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import com.squareup.moshi.Json
*/
data class SendRsvpQueryParams(
/**
* The RSVP status for the event. Must be yes, no, or maybe
* The ID of the calendar to create the event in.
*/
@Json(name = "status")
val status: RsvpStatus,
@Json(name = "calendar_id")
val calendarId: String,
) : IQueryParams {

/**
* Builder for [SendRsvpQueryParams].
* @param status The RSVP status for the event. Must be yes, no, or maybe
* @param calendarId The ID of the calendar to create the event in.
*/
data class Builder(private val status: RsvpStatus) {
data class Builder(private val calendarId: String) {

/**
* Builds a [SendRsvpQueryParams] instance.
* @return The [SendRsvpQueryParams] instance.
*/
fun build() = SendRsvpQueryParams(status)
fun build() = SendRsvpQueryParams(calendarId)
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/com/nylas/models/TrackingOptions.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package com.nylas.models

import com.squareup.moshi.Json

/**
* Class representing the different tracking options for when a message is sent.
*/
data class TrackingOptions(
/**
* The label to apply to tracked messages.
*/
@Json(name = "label")
val label: String? = null,
/**
* Whether to track links.
*/
@Json(name = "links")
val links: Boolean? = null,
/**
* Whether to track opens.
*/
@Json(name = "opens")
val opens: Boolean? = null,
/**
* Whether to track thread replies.
*/
@Json(name = "thread_replies")
val threadReplies: Boolean? = null,
)
6 changes: 3 additions & 3 deletions src/main/kotlin/com/nylas/models/UpdateEventRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ data class UpdateEventRequest(
* Sets the visibility for the event. The calendar default will be used if this field is omitted.
*/
@Json(name = "visibility")
val visibility: EvenVisibility? = null,
val visibility: EventVisibility? = null,
/**
* Sets the maximum number of participants that may attend the event.
*/
Expand Down Expand Up @@ -519,7 +519,7 @@ data class UpdateEventRequest(
private var recurrence: List<String>? = null
private var calendarId: String? = null
private var readOnly: Boolean? = null
private var visibility: EvenVisibility? = null
private var visibility: EventVisibility? = null
private var capacity: Int? = null
private var hideParticipant: Boolean? = null

Expand Down Expand Up @@ -628,7 +628,7 @@ data class UpdateEventRequest(
* @param visibility Sets the visibility for the event. The calendar default will be used if this field is omitted.
* @return The builder.
*/
fun visibility(visibility: EvenVisibility) = apply { this.visibility = visibility }
fun visibility(visibility: EventVisibility) = apply { this.visibility = visibility }

/**
* Update the capacity of the event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data class UrlForAuthenticationConfig(
* The prompt parameter is used to force the consent screen to be displayed even if the user has already given consent to your application.
*/
@Json(name = "prompt")
val prompt: String? = null,
val prompt: Prompt? = null,
/**
* A space-delimited list of scopes that identify the resources that your application could access on the user's behalf.
* If no scope is given, all of the default integration's scopes are used.
Expand Down Expand Up @@ -66,7 +66,7 @@ data class UrlForAuthenticationConfig(
) {
private var accessType: AccessType = AccessType.ONLINE
private var provider: AuthProvider? = null
private var prompt: String? = null
private var prompt: Prompt? = null
private var scope: List<String>? = null
private var includeGrantScopes: Boolean? = null
private var state: String? = null
Expand All @@ -92,7 +92,7 @@ data class UrlForAuthenticationConfig(
* @param prompt The prompt parameter.
* @return This builder.
*/
fun prompt(prompt: String) = apply { this.prompt = prompt }
fun prompt(prompt: Prompt) = apply { this.prompt = prompt }

/**
* Set the scopes that identify the resources that your application could access on the user's behalf.
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/nylas/resources/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Auth(private val client: NylasClient) {
*/
@Throws(NylasApiError::class, NylasSdkTimeoutError::class)
fun detectProvider(params: ProviderDetectParams): Response<ProviderDetectResponse> {
val path = "v3/grants/connect/providers/detect"
val path = "v3/providers/detect"
val responseType = Types.newParameterizedType(Response::class.java, ProviderDetectResponse::class.java)

return client.executePost(path, responseType, queryParams = params)
Expand Down Expand Up @@ -165,7 +165,11 @@ class Auth(private val client: NylasClient) {
.adapter(UrlForAuthenticationConfig::class.java)
.toJson(config)
JsonHelper.jsonToMap(json).forEach { (key, value) ->
url.addQueryParameter(key, value.toString())
if(key == "scope") {
url.addQueryParameter("scope", config.scope?.joinToString(separator = " "))
} else {
url.addQueryParameter(key, value.toString())
}
}

return url
Expand Down
Loading
Loading