@@ -5,13 +5,15 @@ import com.github.smaugfm.events.IEventDispatcher
5
5
import io.ktor.application.call
6
6
import io.ktor.application.install
7
7
import io.ktor.client.HttpClient
8
+ import io.ktor.client.features.ResponseException
8
9
import io.ktor.client.features.defaultRequest
9
10
import io.ktor.client.features.json.JsonFeature
10
11
import io.ktor.client.features.json.defaultSerializer
11
12
import io.ktor.client.features.json.serializer.KotlinxSerializer
12
13
import io.ktor.client.request.get
13
14
import io.ktor.client.request.header
14
15
import io.ktor.client.request.post
16
+ import io.ktor.client.statement.readText
15
17
import io.ktor.features.ContentNegotiation
16
18
import io.ktor.http.ContentType
17
19
import io.ktor.http.HttpStatusCode
@@ -24,6 +26,7 @@ import io.ktor.routing.routing
24
26
import io.ktor.serialization.json
25
27
import io.ktor.server.engine.embeddedServer
26
28
import io.ktor.server.netty.Netty
29
+ import kotlinx.coroutines.CompletableDeferred
27
30
import kotlinx.coroutines.GlobalScope
28
31
import kotlinx.coroutines.Job
29
32
import kotlinx.coroutines.delay
@@ -59,24 +62,33 @@ class MonoApi(private val token: String) {
59
62
return Json .decodeFromString(infoString)
60
63
}
61
64
62
- suspend fun setWebHook (url : URI ): MonoStatusResponse {
65
+ suspend fun setWebHook (url : URI , port : Int ): MonoStatusResponse {
63
66
require(url.toASCIIString() == url.toString())
64
67
68
+ val waitForWebhook = CompletableDeferred <Unit >()
65
69
val json = defaultSerializer()
66
- val server = embeddedServer(Netty , port = url. port) {
70
+ val server = embeddedServer(Netty , port = port) {
67
71
routing {
68
72
get(url.path) {
69
73
call.response.status(HttpStatusCode .OK )
70
74
call.respondText(" OK\n " , ContentType .Text .Plain )
71
75
logger.info(" Webhook setup successful: $url " )
76
+ waitForWebhook.complete(Unit )
72
77
}
73
78
}
74
79
}
75
- logger.info(" Starting webhook setup server." )
80
+ logger.info(" Starting webhook setup server... " )
76
81
server.start(wait = false )
77
- val statusString = httpClient.post<String >(url(" personal/webhook" )) {
78
- body = json.write(MonoWebHookRequest (url.toString()))
82
+
83
+ val statusString = try {
84
+ httpClient.post<String >(url(" personal/webhook" )) {
85
+ body = json.write(MonoWebHookRequest (url.toString()))
86
+ }
87
+ } catch (e: ResponseException ) {
88
+ logger.error(e.response.readText())
89
+ throw e
79
90
}
91
+ waitForWebhook.await()
80
92
server.stop(serverStopGracePeriod, serverStopGracePeriod)
81
93
return Json .decodeFromString(statusString)
82
94
}
@@ -112,9 +124,10 @@ class MonoApi(private val token: String) {
112
124
fun startMonoWebhookServerAsync (
113
125
context : CoroutineContext ,
114
126
webhook : URI ,
115
- dispatcher : IEventDispatcher
127
+ port : Int ,
128
+ dispatcher : IEventDispatcher ,
116
129
): Job {
117
- val server = embeddedServer(Netty , port = webhook. port) {
130
+ val server = embeddedServer(Netty , port = port) {
118
131
install(ContentNegotiation ) {
119
132
json()
120
133
}
@@ -132,10 +145,7 @@ class MonoApi(private val token: String) {
132
145
}
133
146
}
134
147
135
- suspend fun Collection<MonoApi>.setupWebhook (webhook : URI ) {
136
- this .forEach {
137
- it.setWebHook(webhook)
138
- }
139
- }
148
+ suspend fun Collection<MonoApi>.setupWebhook (webhook : URI , port : Int ) =
149
+ this .forEach { it.setWebHook(webhook, port) }
140
150
}
141
151
}
0 commit comments