Skip to content

Commit 3031a85

Browse files
committed
Save the session token for ease of access
1 parent 0914c9b commit 3031a85

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import java.time.Instant
2525
class CoderRestClientService {
2626
private lateinit var retroRestClient: CoderV2RestFacade
2727
lateinit var me: User
28+
lateinit var sessionToken: String
2829

2930
/**
3031
* This must be called before anything else. It will authenticate with coder and retrieve a session token
@@ -53,14 +54,15 @@ class CoderRestClientService {
5354
.build()
5455
.create(CoderV2RestFacade::class.java)
5556

56-
val sessionTokenResponse = retroRestClient.authenticate(LoginWithPasswordRequest(email, password)).execute()
57+
val loginResponse = retroRestClient.authenticate(LoginWithPasswordRequest(email, password)).execute()
5758

58-
if (!sessionTokenResponse.isSuccessful) {
59-
throw AuthenticationException("Authentication failed with code:${sessionTokenResponse.code()}, reason: ${sessionTokenResponse.message()}")
59+
if (!loginResponse.isSuccessful) {
60+
throw AuthenticationException("Authentication failed with code:${loginResponse.code()}, reason: ${loginResponse.message()}")
6061
}
6162

62-
val userResponse = retroRestClient.me().execute()
63+
sessionToken = loginResponse.body()?.sessionToken!!
6364

65+
val userResponse = retroRestClient.me().execute()
6466
if (!userResponse.isSuccessful) {
6567
throw IllegalStateException("Could not retrieve information about logged use:${userResponse.code()}, reason: ${userResponse.message()}")
6668
}
@@ -85,5 +87,10 @@ class CoderRestClientService {
8587

8688
return sshKeysResponse.body()!!
8789
}
90+
}
8891

92+
fun main() {
93+
val restClient = CoderRestClientService()
94+
restClient.initClientSession(UriScheme.HTTPS, "dev.coder.com", 443, "faur@coder.com", "coder123")
95+
println(restClient.workspaces())
8996
}

0 commit comments

Comments
 (0)