-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add widgets package and get widget token API support. (#257)
- Loading branch information
Showing
7 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/com/workos/widgets/builders/GetTokenOptionsBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/kotlin/com/workos/widgets/types/GetTokenOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |