Skip to content

Commit de20984

Browse files
committed
Simplify creating a default REST client
1 parent 1a64fb4 commit de20984

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

src/main/kotlin/com/coder/gateway/CoderGatewayConnectionProvider.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import com.coder.gateway.models.TokenSource
66
import com.coder.gateway.models.WorkspaceAgentModel
77
import com.coder.gateway.sdk.CoderCLIManager
88
import com.coder.gateway.sdk.CoderRestClient
9-
import com.coder.gateway.sdk.defaultProxy
10-
import com.coder.gateway.sdk.defaultVersion
9+
import com.coder.gateway.sdk.DefaultCoderRestClient
1110
import com.coder.gateway.sdk.ex.AuthenticationResponseException
1211
import com.coder.gateway.sdk.toURL
1312
import com.coder.gateway.sdk.v2.models.Workspace
@@ -66,7 +65,7 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
6665
WorkspaceStatus.CANCELING, WorkspaceStatus.CANCELED ->
6766
// TODO: Turn on the workspace.
6867
throw IllegalArgumentException("The workspace \"$workspaceName\" is ${workspace.latestBuild.status.toString().lowercase()}; please start the workspace and try again")
69-
WorkspaceStatus.FAILED, WorkspaceStatus.DELETING, WorkspaceStatus.DELETED, ->
68+
WorkspaceStatus.FAILED, WorkspaceStatus.DELETING, WorkspaceStatus.DELETED ->
7069
throw IllegalArgumentException("The workspace \"$workspaceName\" is ${workspace.latestBuild.status.toString().lowercase()}; unable to connect")
7170
WorkspaceStatus.RUNNING -> Unit // All is well
7271
}
@@ -142,7 +141,7 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
142141
if (token == null) { // User aborted.
143142
throw IllegalArgumentException("Unable to connect to $deploymentURL, $TOKEN is missing")
144143
}
145-
val client = CoderRestClient(deploymentURL, token.first, defaultVersion(), settings, defaultProxy())
144+
val client = DefaultCoderRestClient(deploymentURL, token.first)
146145
return try {
147146
Pair(client, client.me().username)
148147
} catch (ex: AuthenticationResponseException) {

src/main/kotlin/com/coder/gateway/sdk/CoderRestClientService.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.google.gson.Gson
1919
import com.google.gson.GsonBuilder
2020
import com.intellij.ide.plugins.PluginManagerCore
2121
import com.intellij.openapi.components.Service
22+
import com.intellij.openapi.components.service
2223
import com.intellij.openapi.diagnostic.Logger
2324
import com.intellij.openapi.extensions.PluginId
2425
import com.intellij.openapi.util.SystemInfo
@@ -75,8 +76,8 @@ class CoderRestClientService {
7576
*
7677
* @throws [AuthenticationResponseException] if authentication failed.
7778
*/
78-
fun initClientSession(url: URL, token: String, settings: CoderSettingsState): User {
79-
client = CoderRestClient(url, token, defaultVersion(), settings, defaultProxy())
79+
fun initClientSession(url: URL, token: String): User {
80+
client = DefaultCoderRestClient(url, token)
8081
me = client.me()
8182
buildVersion = client.buildInfo().version
8283
isReady = true
@@ -95,22 +96,19 @@ data class ProxyValues (
9596
val selector: ProxySelector,
9697
)
9798

98-
fun defaultProxy(): ProxyValues {
99-
val inst = HttpConfigurable.getInstance()
100-
return ProxyValues(
101-
inst.proxyLogin,
102-
inst.plainProxyPassword,
103-
inst.PROXY_AUTHENTICATION,
104-
inst.onlyBySettingsSelector
105-
)
106-
}
107-
108-
fun defaultVersion(): String {
109-
// This is the id from the plugin.xml.
110-
return PluginManagerCore.getPlugin(PluginId.getId("com.coder.gateway"))!!.version
111-
}
112-
113-
class CoderRestClient @JvmOverloads constructor(
99+
/**
100+
* A client instance that hooks into global JetBrains services for default
101+
* settings. Exists only so we can use the base client in tests.
102+
*/
103+
class DefaultCoderRestClient(url: URL, token: String) : CoderRestClient(url, token,
104+
PluginManagerCore.getPlugin(PluginId.getId("com.coder.gateway"))!!.version,
105+
service(),
106+
ProxyValues(HttpConfigurable.getInstance().proxyLogin,
107+
HttpConfigurable.getInstance().plainProxyPassword,
108+
HttpConfigurable.getInstance().PROXY_AUTHENTICATION,
109+
HttpConfigurable.getInstance().onlyBySettingsSelector))
110+
111+
open class CoderRestClient @JvmOverloads constructor(
114112
var url: URL, var token: String,
115113
private val pluginVersion: String,
116114
private val settings: CoderSettingsState,

src/main/kotlin/com/coder/gateway/views/CoderGatewayRecentWorkspaceConnectionsView.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import com.coder.gateway.icons.CoderIcons
99
import com.coder.gateway.models.RecentWorkspaceConnection
1010
import com.coder.gateway.models.WorkspaceAgentModel
1111
import com.coder.gateway.sdk.CoderRestClient
12-
import com.coder.gateway.sdk.defaultProxy
13-
import com.coder.gateway.sdk.defaultVersion
12+
import com.coder.gateway.sdk.DefaultCoderRestClient
1413
import com.coder.gateway.sdk.toURL
1514
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
1615
import com.coder.gateway.sdk.v2.models.toAgentModels
1716
import com.coder.gateway.services.CoderRecentWorkspaceConnectionsService
18-
import com.coder.gateway.services.CoderSettingsState
1917
import com.coder.gateway.toWorkspaceParams
2018
import com.intellij.icons.AllIcons
2119
import com.intellij.ide.BrowserUtil
@@ -32,7 +30,6 @@ import com.intellij.ui.SearchTextField
3230
import com.intellij.ui.components.ActionLink
3331
import com.intellij.ui.components.JBScrollPane
3432
import com.intellij.ui.dsl.builder.*
35-
import com.intellij.util.io.readText
3633
import com.intellij.util.ui.JBFont
3734
import com.intellij.util.ui.JBUI
3835
import com.intellij.util.ui.UIUtil
@@ -70,7 +67,6 @@ data class DeploymentInfo(
7067
)
7168

7269
class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback: (Component) -> Unit) : GatewayRecentConnections, Disposable {
73-
private val settings: CoderSettingsState = service()
7470
private val recentConnectionsService = service<CoderRecentWorkspaceConnectionsService>()
7571
private val cs = CoroutineScope(Dispatchers.Main)
7672

@@ -256,7 +252,7 @@ class CoderGatewayRecentWorkspaceConnectionsView(private val setContentCallback:
256252
deployments[dir] ?: try {
257253
val url = Path.of(dir).resolve("url").toFile().readText()
258254
val token = Path.of(dir).resolve("session").toFile().readText()
259-
DeploymentInfo(CoderRestClient(url.toURL(), token, defaultVersion(), settings, defaultProxy()))
255+
DeploymentInfo(DefaultCoderRestClient(url.toURL(), token))
260256
} catch (e: Exception) {
261257
logger.error("Unable to create client from $dir", e)
262258
DeploymentInfo(error = "Error trying to read $dir: ${e.message}")

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
533533
*/
534534
private fun authenticate(url: URL, token: String) {
535535
logger.info("Authenticating to $url...")
536-
clientService.initClientSession(url, token, settings)
536+
clientService.initClientSession(url, token)
537537

538538
try {
539539
logger.info("Checking compatibility with Coder version ${clientService.buildVersion}...")

0 commit comments

Comments
 (0)