@@ -25,6 +25,8 @@ import kotlin.time.Duration.Companion.minutes
25
25
import kotlin.time.Duration.Companion.seconds
26
26
import kotlin.time.toJavaDuration
27
27
28
+ private const val CAN_T_HANDLE_URI_TITLE = " Can't handle URI"
29
+
28
30
@Suppress(" UnstableApiUsage" )
29
31
open class CoderProtocolHandler (
30
32
private val context : CoderToolboxContext ,
@@ -46,35 +48,18 @@ open class CoderProtocolHandler(
46
48
reInitialize : suspend (CoderRestClient , CoderCLIManager ) -> Unit
47
49
) {
48
50
context.popupPluginMainPage()
51
+ context.logger.info(" Handling $uri ..." )
49
52
val params = uri.toQueryParameters()
50
53
if (params.isEmpty()) {
51
54
// probably a plugin installation scenario
52
55
return
53
56
}
54
57
55
- val deploymentURL = params.url() ? : askUrl()
56
- if (deploymentURL.isNullOrBlank()) {
57
- context.logger.error(" Query parameter \" $URL \" is missing from URI $uri " )
58
- context.showErrorPopup(MissingArgumentException (" Can't handle URI because query parameter \" $URL \" is missing" ))
59
- return
60
- }
61
-
62
- val queryToken = params.token()
63
- val restClient = try {
64
- authenticate(deploymentURL, queryToken)
65
- } catch (ex: Exception ) {
66
- context.logger.error(ex, " Query parameter \" $TOKEN \" is missing from URI $uri " )
67
- context.showErrorPopup(IllegalStateException (humanizeConnectionError(deploymentURL.toURL(), true , ex)))
68
- return
69
- }
70
-
58
+ val deploymentURL = resolveDeploymentUrl(params) ? : return
59
+ val token = resolveToken(params) ? : return
71
60
// TODO: Show a dropdown and ask for the workspace if missing. Right now it's not possible because dialogs are quite limited
72
- val workspaceName = params.workspace()
73
- if (workspaceName.isNullOrBlank()) {
74
- context.logger.error(" Query parameter \" $WORKSPACE \" is missing from URI $uri " )
75
- context.showErrorPopup(MissingArgumentException (" Can't handle URI because query parameter \" $WORKSPACE \" is missing" ))
76
- return
77
- }
61
+ val workspaceName = resolveWorkspace(params) ? : return
62
+ val restClient = buildRestClient(deploymentURL, token) ? : return
78
63
79
64
val workspaces = restClient.workspaces()
80
65
val workspace = workspaces.firstOrNull { it.name == workspaceName }
@@ -244,6 +229,56 @@ open class CoderProtocolHandler(
244
229
}
245
230
}
246
231
232
+ private suspend fun resolveDeploymentUrl (params : Map <String , String >): String? {
233
+ val deploymentURL = params.url() ? : askUrl()
234
+ if (deploymentURL.isNullOrBlank()) {
235
+ context.logAndShowError(CAN_T_HANDLE_URI_TITLE , " Query parameter \" $URL \" is missing from URI" )
236
+ return null
237
+ }
238
+ return deploymentURL
239
+ }
240
+
241
+ private suspend fun resolveToken (params : Map <String , String >): String? {
242
+ val token = params.token()
243
+ if (token.isNullOrBlank()) {
244
+ context.logAndShowError(CAN_T_HANDLE_URI_TITLE , " Query parameter \" $TOKEN \" is missing from URI" )
245
+ return null
246
+ }
247
+ return token
248
+ }
249
+
250
+ private suspend fun resolveWorkspace (params : Map <String , String >): String? {
251
+ val workspace = params.workspace()
252
+ if (workspace.isNullOrBlank()) {
253
+ context.logAndShowError(CAN_T_HANDLE_URI_TITLE , " Query parameter \" $WORKSPACE \" is missing from URI" )
254
+ return null
255
+ }
256
+ return workspace
257
+ }
258
+
259
+ private suspend fun buildRestClient (deploymentURL : String , token : String ): CoderRestClient ? {
260
+ try {
261
+ return authenticate(deploymentURL, token)
262
+ } catch (ex: Exception ) {
263
+ context.logAndShowError(CAN_T_HANDLE_URI_TITLE , humanizeConnectionError(deploymentURL.toURL(), true , ex))
264
+ return null
265
+ }
266
+ }
267
+
268
+ /* *
269
+ * Returns an authenticated Coder CLI.
270
+ */
271
+ private suspend fun authenticate (deploymentURL : String , token : String ): CoderRestClient {
272
+ val client = CoderRestClient (
273
+ context,
274
+ deploymentURL.toURL(),
275
+ if (settings.requireTokenAuth) token else null ,
276
+ PluginManager .pluginInfo.version
277
+ )
278
+ client.authenticate()
279
+ return client
280
+ }
281
+
247
282
private suspend fun CoderRestClient.waitForReady (workspace : Workspace ): Boolean {
248
283
var status = workspace.latestBuild.status
249
284
try {
@@ -285,43 +320,6 @@ open class CoderProtocolHandler(
285
320
context.i18n.ptrl(" Enter the full URL of your Coder deployment" )
286
321
)
287
322
}
288
-
289
- /* *
290
- * Return an authenticated Coder CLI, asking for the token.
291
- * Throw MissingArgumentException if the user aborts. Any network or invalid
292
- * token error may also be thrown.
293
- */
294
- private suspend fun authenticate (
295
- deploymentURL : String ,
296
- tryToken : String?
297
- ): CoderRestClient {
298
- val token =
299
- if (settings.requireTokenAuth) {
300
- // Try the provided token immediately on the first attempt.
301
- if (! tryToken.isNullOrBlank()) {
302
- tryToken
303
- } else {
304
- context.popupPluginMainPage()
305
- // Otherwise ask for a new token, showing the previous token.
306
- dialogUi.askToken(deploymentURL.toURL())
307
- }
308
- } else {
309
- null
310
- }
311
-
312
- if (settings.requireTokenAuth && token == null ) { // User aborted.
313
- throw MissingArgumentException (" Token is required" )
314
- }
315
- val client = CoderRestClient (
316
- context,
317
- deploymentURL.toURL(),
318
- token,
319
- PluginManager .pluginInfo.version
320
- )
321
- client.authenticate()
322
- return client
323
- }
324
-
325
323
}
326
324
327
325
/* *
0 commit comments