1
1
package com.coder.gateway.sdk
2
2
3
+ import com.coder.gateway.sdk.ex.AuthenticationException
4
+ import com.intellij.openapi.Disposable
3
5
import com.intellij.openapi.components.Service
4
- import io.ktor.client.HttpClient
5
- import io.ktor.client.engine.cio.CIO
6
- import io.ktor.client.plugins.compression.ContentEncoding
7
- import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
8
- import io.ktor.client.plugins.logging.DEFAULT
9
- import io.ktor.client.plugins.logging.LogLevel
10
- import io.ktor.client.plugins.logging.Logger
11
- import io.ktor.client.plugins.logging.Logging
12
- import io.ktor.client.request.post
13
- import io.ktor.client.request.setBody
14
- import io.ktor.client.statement.bodyAsText
15
- import io.ktor.http.ContentType
16
- import io.ktor.http.contentType
17
- import io.ktor.serialization.gson.gson
18
- import kotlinx.coroutines.runBlocking
19
-
20
- @Service
21
- class CoderClientService {
22
- private val httpClient = HttpClient (CIO ) {
23
- install(Logging ) {
24
- logger = Logger .DEFAULT
25
- level = LogLevel .ALL
26
- }
27
- install(ContentEncoding )
28
- install(ContentNegotiation ) {
29
- gson() {
30
- setPrettyPrinting()
31
- }
6
+ import com.intellij.openapi.diagnostic.Logger
7
+ import retrofit2.Retrofit
8
+ import retrofit2.converter.gson.GsonConverterFactory
9
+
10
+ @Service(Service .Level .APP )
11
+ class CoderClientService : Disposable {
12
+ private lateinit var retroRestClient: CoderRestService
13
+
14
+ lateinit var sessionToken: String
15
+
16
+ /* *
17
+ * This must be called before anything else. It will authenticate with coder and retrieve a session token
18
+ * @throws [AuthenticationException] if authentication failed
19
+ */
20
+ fun initClientSession (host : String , port : Int , email : String , password : String ) {
21
+ val hostPath = host.trimEnd(' /' )
22
+ val sessionTokenResponse = Retrofit .Builder ()
23
+ .baseUrl(" http://$hostPath :$port " )
24
+ .addConverterFactory(GsonConverterFactory .create())
25
+ .build()
26
+ .create(CoderAuthenticatonRestService ::class .java).authenticate(LoginRequest (email, password)).execute()
27
+
28
+ if (! sessionTokenResponse.isSuccessful) {
29
+ throw AuthenticationException (" Authentication failed with code:${sessionTokenResponse.code()} , reason: ${sessionTokenResponse.errorBody().toString()} " )
32
30
}
31
+ sessionToken = sessionTokenResponse.body()!! .sessionToken
32
+ retroRestClient = Retrofit .Builder ()
33
+ .baseUrl(" https://$hostPath :$port " )
34
+ .addConverterFactory(GsonConverterFactory .create())
35
+ .build()
36
+ .create(CoderRestService ::class .java)
33
37
}
34
38
35
- suspend fun authenthicateWithPassword (url : String , email : String , password : String ) {
36
- val urlPath = url.trimEnd(' /' )
37
- val response = httpClient.post(" $urlPath /auth/basic/login" ) {
38
- contentType(ContentType .Application .Json )
39
- setBody(LoginRequest (email, password))
40
- }
41
-
42
- println (" >>> ${response.bodyAsText()} " )
43
- }
39
+ override fun dispose () {
44
40
45
- fun dispose () {
46
- httpClient.close()
47
41
}
48
- }
49
-
50
- fun main () {
51
- val coderClient = CoderClientService ()
52
-
53
- runBlocking {
54
- coderClient.authenthicateWithPassword(" http://localhost:7080" , " example@email.com" , " password example" )
55
- }
56
-
57
- coderClient.dispose()
58
42
}
0 commit comments