@@ -6,9 +6,7 @@ import com.coder.gateway.sdk.convertors.ArchConverter
6
6
import com.coder.gateway.sdk.convertors.InstantConverter
7
7
import com.coder.gateway.sdk.convertors.OSConverter
8
8
import com.coder.gateway.sdk.convertors.UUIDConverter
9
- import com.coder.gateway.sdk.ex.AuthenticationResponseException
10
- import com.coder.gateway.sdk.ex.TemplateResponseException
11
- import com.coder.gateway.sdk.ex.WorkspaceResponseException
9
+ import com.coder.gateway.sdk.ex.APIResponseException
12
10
import com.coder.gateway.sdk.v2.CoderV2RestFacade
13
11
import com.coder.gateway.sdk.v2.models.BuildInfo
14
12
import com.coder.gateway.sdk.v2.models.CreateWorkspaceBuildRequest
@@ -138,19 +136,11 @@ open class CoderRestClient(
138
136
.build().create(CoderV2RestFacade ::class .java)
139
137
}
140
138
141
- private fun <T > error (
142
- action : String ,
143
- res : retrofit2.Response <T >,
144
- ): String {
145
- val details = res.errorBody()?.charStream()?.use { it.readText() } ? : " no details provided"
146
- return " Unable to $action : url=$url , code=${res.code()} , details=$details "
147
- }
148
-
149
139
/* *
150
140
* Authenticate and load information about the current user and the build
151
141
* version.
152
142
*
153
- * @throws [AuthenticationResponseException] if authentication failed .
143
+ * @throws [APIResponseException] .
154
144
*/
155
145
fun authenticate (): User {
156
146
me = me()
@@ -160,25 +150,25 @@ open class CoderRestClient(
160
150
161
151
/* *
162
152
* Retrieve the current user.
163
- * @throws [AuthenticationResponseException] if authentication failed .
153
+ * @throws [APIResponseException] .
164
154
*/
165
155
fun me (): User {
166
156
val userResponse = retroRestClient.me().execute()
167
157
if (! userResponse.isSuccessful) {
168
- throw AuthenticationResponseException (error( " authenticate" , userResponse) )
158
+ throw APIResponseException ( " authenticate" , url, userResponse)
169
159
}
170
160
171
161
return userResponse.body()!!
172
162
}
173
163
174
164
/* *
175
165
* Retrieves the available workspaces created by the user.
176
- * @throws WorkspaceResponseException if workspaces could not be retrieved .
166
+ * @throws [ApiResponseException] .
177
167
*/
178
168
fun workspaces (): List <Workspace > {
179
169
val workspacesResponse = retroRestClient.workspaces(" owner:me" ).execute()
180
170
if (! workspacesResponse.isSuccessful) {
181
- throw WorkspaceResponseException (error( " retrieve workspaces" , workspacesResponse) )
171
+ throw APIResponseException ( " retrieve workspaces" , url, workspacesResponse)
182
172
}
183
173
184
174
return workspacesResponse.body()!! .workspaces
@@ -203,48 +193,56 @@ open class CoderRestClient(
203
193
* does not include agents when the workspace is off so this can be used to
204
194
* get them instead, just like `coder config-ssh` does (otherwise we risk
205
195
* removing hosts from the SSH config when they are off).
196
+ * @throws [ApiResponseException].
206
197
*/
207
198
fun resources (workspace : Workspace ): List <WorkspaceResource > {
208
199
val resourcesResponse = retroRestClient.templateVersionResources(workspace.latestBuild.templateVersionID).execute()
209
200
if (! resourcesResponse.isSuccessful) {
210
- throw WorkspaceResponseException (error( " retrieve resources for ${workspace.name} " , resourcesResponse) )
201
+ throw APIResponseException ( " retrieve resources for ${workspace.name} " , url, resourcesResponse)
211
202
}
212
203
return resourcesResponse.body()!!
213
204
}
214
205
215
206
fun buildInfo (): BuildInfo {
216
207
val buildInfoResponse = retroRestClient.buildInfo().execute()
217
208
if (! buildInfoResponse.isSuccessful) {
218
- throw java.lang. IllegalStateException (error( " retrieve build information" , buildInfoResponse) )
209
+ throw APIResponseException ( " retrieve build information" , url, buildInfoResponse)
219
210
}
220
211
return buildInfoResponse.body()!!
221
212
}
222
213
214
+ /* *
215
+ * @throws [ApiResponseException].
216
+ */
223
217
private fun template (templateID : UUID ): Template {
224
218
val templateResponse = retroRestClient.template(templateID).execute()
225
219
if (! templateResponse.isSuccessful) {
226
- throw TemplateResponseException (error( " retrieve template with ID $templateID " , templateResponse) )
220
+ throw APIResponseException ( " retrieve template with ID $templateID " , url, templateResponse)
227
221
}
228
222
return templateResponse.body()!!
229
223
}
230
224
225
+ /* *
226
+ * @throws [ApiResponseException].
227
+ */
231
228
fun startWorkspace (workspace : Workspace ): WorkspaceBuild {
232
229
val buildRequest = CreateWorkspaceBuildRequest (null , WorkspaceTransition .START )
233
230
val buildResponse = retroRestClient.createWorkspaceBuild(workspace.id, buildRequest).execute()
234
231
if (buildResponse.code() != HttpURLConnection .HTTP_CREATED ) {
235
- throw WorkspaceResponseException (error( " start workspace ${workspace.name} " , buildResponse) )
232
+ throw APIResponseException ( " start workspace ${workspace.name} " , url, buildResponse)
236
233
}
237
-
238
234
return buildResponse.body()!!
239
235
}
240
236
237
+ /* *
238
+ * @throws [ApiResponseException].
239
+ */
241
240
fun stopWorkspace (workspace : Workspace ): WorkspaceBuild {
242
241
val buildRequest = CreateWorkspaceBuildRequest (null , WorkspaceTransition .STOP )
243
242
val buildResponse = retroRestClient.createWorkspaceBuild(workspace.id, buildRequest).execute()
244
243
if (buildResponse.code() != HttpURLConnection .HTTP_CREATED ) {
245
- throw WorkspaceResponseException (error( " stop workspace ${workspace.name} " , buildResponse) )
244
+ throw APIResponseException ( " stop workspace ${workspace.name} " , url, buildResponse)
246
245
}
247
-
248
246
return buildResponse.body()!!
249
247
}
250
248
@@ -256,17 +254,16 @@ open class CoderRestClient(
256
254
* 2. The agent gets a new ID and token on each START build. Many template
257
255
* authors are not diligent about making sure the agent gets restarted
258
256
* with this information when we do two START builds in a row.
257
+ * @throws [ApiResponseException].
259
258
*/
260
259
fun updateWorkspace (workspace : Workspace ): WorkspaceBuild {
261
260
val template = template(workspace.templateID)
262
-
263
261
val buildRequest =
264
262
CreateWorkspaceBuildRequest (template.activeVersionID, WorkspaceTransition .START )
265
263
val buildResponse = retroRestClient.createWorkspaceBuild(workspace.id, buildRequest).execute()
266
264
if (buildResponse.code() != HttpURLConnection .HTTP_CREATED ) {
267
- throw WorkspaceResponseException (error( " update workspace ${workspace.name} " , buildResponse) )
265
+ throw APIResponseException ( " update workspace ${workspace.name} " , url, buildResponse)
268
266
}
269
-
270
267
return buildResponse.body()!!
271
268
}
272
269
0 commit comments