Skip to content

Commit

Permalink
Add widgets package and get widget token API support. (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd authored Nov 15, 2024
1 parent 69c4356 commit 4e6e49f
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/com/workos/WorkOS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.workos.portal.PortalApi
import com.workos.sso.SsoApi
import com.workos.usermanagement.UserManagementApi
import com.workos.webhooks.WebhooksApi
import com.workos.widgets.WidgetsApi
import org.apache.http.client.utils.URIBuilder
import java.io.IOException
import java.lang.IllegalArgumentException
Expand Down Expand Up @@ -119,6 +120,12 @@ class WorkOS(
@JvmField
val webhooks = WebhooksApi()

/**
* Module for interacting with the Widgets API.
*/
@JvmField
val widgets = WidgetsApi(this)

/**
* Module for interacting with the FGA API.
*/
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/com/workos/widgets/WidgetsApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.workos.widgets

import com.workos.WorkOS
import com.workos.common.http.RequestConfig
import com.workos.widgets.models.WidgetToken
import com.workos.widgets.types.GetTokenOptions

/**
* The WidgetsApi class provides convenience methods for working with the WorkOS
* Widgets product.
*/
class WidgetsApi(private val workos: WorkOS) {
/**
* Generates a widget token.
*/
fun getToken(options: GetTokenOptions): WidgetToken {
return workos.post(
"/widgets/token",
WidgetToken::class.java,
RequestConfig.builder().data(options).build()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.workos.widgets.builders

import com.workos.widgets.models.WidgetScope
import com.workos.widgets.types.GetTokenOptions

/**
* Builder for options when generating a widget token.
*
* @param organizationId The ID of the organization to generate a token for.
* @param userId The ID of the user to generate a token for.
* @param scopes The scopes to generate a token for.
*/
class GetTokenOptionsBuilder @JvmOverloads constructor(
private var organizationId: String,
private var userId: String,
private var scopes: List<WidgetScope>,
) {
/**
* Organization ID
*/
fun organizationId(value: String) = apply { organizationId = value }

/**
* User ID
*/
fun userId(value: String) = apply { userId = value }

/**
* Widget scopes
*/
fun scopes(value: List<WidgetScope>) = apply { scopes = value }

/**
* Generates the GetTokenOptions object.
*/
fun build(): GetTokenOptions {
return GetTokenOptions(
organizationId = this.organizationId,
userId = this.userId,
scopes = this.scopes,
)
}

/**
* @suppress
*/
companion object {
@JvmStatic
fun create(organizationId: String, userId: String, scopes: List<WidgetScope>): GetTokenOptionsBuilder {
return GetTokenOptionsBuilder(organizationId, userId, scopes)
}
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/com/workos/widgets/models/WidgetScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.workos.widgets.models

import com.fasterxml.jackson.annotation.JsonValue

/**
* WidgetScope of the widget token.
*
* @param value The string value of the widgetscope.
*/
enum class WidgetScope(@JsonValue val value: String) {
/**
* Manage users via the users table widget.
*/
UsersTableManagement("widgets:users-table:manage"),
}
14 changes: 14 additions & 0 deletions src/main/kotlin/com/workos/widgets/models/WidgetToken.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.workos.widgets.models

import com.fasterxml.jackson.annotation.JsonCreator

/**
* The response object from creating a widget token.
*
* @param token The generated widget token.
*/
data class WidgetToken
@JsonCreator constructor(
@JvmField
val token: String
)
30 changes: 30 additions & 0 deletions src/main/kotlin/com/workos/widgets/types/GetTokenOptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.workos.widgets.types

import com.fasterxml.jackson.annotation.JsonProperty
import com.workos.widgets.models.WidgetScope

class GetTokenOptions(
/**
* The ID of the organization to generate a token for.
*/
@JsonProperty("organization_id")
val organizationId: String,

/**
* The ID of the user to generate a token for.
*/
@JsonProperty("user_id")
val userId: String,

/**
* The scopes to generate a token for.
*/
@JsonProperty("scopes")
val scopes: List<WidgetScope>,
) {
init {
require(organizationId.isNotBlank()) { "Organization ID is required" }
require(userId.isNotBlank()) { "User ID is required" }
require(scopes.isNotEmpty()) { "At least one scope is required" }
}
}
43 changes: 43 additions & 0 deletions src/test/kotlin/com/workos/test/widgets/WidgetsApiTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.workos.test.widgets

import com.workos.test.TestBase
import com.workos.widgets.builders.GetTokenOptionsBuilder
import com.workos.widgets.models.WidgetScope
import kotlin.test.Test
import kotlin.test.assertEquals

class WidgetsApiTest : TestBase() {
private fun prepareGetTokenTest(body: String): String {
val token = "abc123456"

stubResponse(
url = "/widgets/token",
responseBody = """{
"token": "$token"
}""",
requestBody = body
)

return token
}

@Test
fun getTokenShouldReturnPayload() {
val workos = createWorkOSClient()

val token = prepareGetTokenTest(
"""{
"organization_id": "organizationId",
"user_id": "userId",
"scopes": ["widgets:users-table:manage"]
}"""
)

val options = GetTokenOptionsBuilder("organizationId", "userId", listOf(WidgetScope.UsersTableManagement))
.build()

val response = workos.widgets.getToken(options)

assertEquals(response.token, token)
}
}

0 comments on commit 4e6e49f

Please sign in to comment.