Skip to content

Commit cdaf544

Browse files
committed
impl: add http client stubs
Adds the http client stubs necessary to download the application name via the REST API.
1 parent 925dcb9 commit cdaf544

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/main/kotlin/com/coder/toolbox/sdk/CoderRestClient.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.coder.toolbox.sdk.ex.APIResponseException
1010
import com.coder.toolbox.sdk.interceptors.Interceptors
1111
import com.coder.toolbox.sdk.v2.CoderV2RestFacade
1212
import com.coder.toolbox.sdk.v2.models.ApiErrorResponse
13+
import com.coder.toolbox.sdk.v2.models.Appearance
1314
import com.coder.toolbox.sdk.v2.models.BuildInfo
1415
import com.coder.toolbox.sdk.v2.models.CreateWorkspaceBuildRequest
1516
import com.coder.toolbox.sdk.v2.models.Template
@@ -45,6 +46,7 @@ open class CoderRestClient(
4546

4647
lateinit var me: User
4748
lateinit var buildVersion: String
49+
lateinit var appName: String
4850

4951
init {
5052
setupSession()
@@ -94,14 +96,15 @@ open class CoderRestClient(
9496
suspend fun initializeSession(): User {
9597
me = me()
9698
buildVersion = buildInfo().version
99+
appName = appearance().applicationName
97100
return me
98101
}
99102

100103
/**
101104
* Retrieve the current user.
102105
* @throws [APIResponseException].
103106
*/
104-
suspend fun me(): User {
107+
internal suspend fun me(): User {
105108
val userResponse = retroRestClient.me()
106109
if (!userResponse.isSuccessful) {
107110
throw APIResponseException(
@@ -117,6 +120,25 @@ open class CoderRestClient(
117120
}
118121
}
119122

123+
/**
124+
* Retrieves the visual dashboard configuration.
125+
*/
126+
internal suspend fun appearance(): Appearance {
127+
val appearanceResponse = retroRestClient.appearance()
128+
if (!appearanceResponse.isSuccessful) {
129+
throw APIResponseException(
130+
"initializeSession",
131+
url,
132+
appearanceResponse.code(),
133+
appearanceResponse.parseErrorBody(moshi)
134+
)
135+
}
136+
137+
return requireNotNull(appearanceResponse.body()) {
138+
"Successful response returned null body or visual dashboard configuration"
139+
}
140+
}
141+
120142
/**
121143
* Retrieves the available workspaces created by the user.
122144
* @throws [APIResponseException].

src/main/kotlin/com/coder/toolbox/sdk/v2/CoderV2RestFacade.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coder.toolbox.sdk.v2
22

3+
import com.coder.toolbox.sdk.v2.models.Appearance
34
import com.coder.toolbox.sdk.v2.models.BuildInfo
45
import com.coder.toolbox.sdk.v2.models.CreateWorkspaceBuildRequest
56
import com.coder.toolbox.sdk.v2.models.Template
@@ -23,6 +24,12 @@ interface CoderV2RestFacade {
2324
@GET("api/v2/users/me")
2425
suspend fun me(): Response<User>
2526

27+
/**
28+
* Returns the configuration of the visual dashboard.
29+
*/
30+
@GET("api/v2/appearance")
31+
suspend fun appearance(): Response<Appearance>
32+
2633
/**
2734
* Retrieves all workspaces the authenticated user has access to.
2835
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coder.toolbox.sdk.v2.models
2+
3+
import com.squareup.moshi.Json
4+
import com.squareup.moshi.JsonClass
5+
6+
@JsonClass(generateAdapter = true)
7+
data class Appearance(
8+
@property:Json(name = "application_name") val applicationName: String
9+
)

0 commit comments

Comments
 (0)