Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "2.0.0"
kotlin("jvm") version "2.2.21"
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}

Expand Down
9 changes: 7 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
kotlin("plugin.serialization") version "2.0.0"
kotlin("plugin.serialization") version "2.2.21"
id("com.vanniktech.maven.publish") version "0.34.0"
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
id("org.jetbrains.dokka") version "1.9.20"
Expand All @@ -20,7 +21,11 @@ java {
}

tasks {
withType<KotlinCompile> { kotlinOptions { jvmTarget = "17" } }
withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
}
}
}

// Defined in gradle.properties
Expand Down
1 change: 0 additions & 1 deletion core/src/main/kotlin/cohort/CohortApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import io.ktor.client.request.headers
import io.ktor.client.request.parameter
import io.ktor.client.statement.bodyAsChannel
import io.ktor.http.HttpStatusCode
import io.ktor.util.toByteArray
import io.ktor.utils.io.jvm.javaio.toInputStream
import kotlinx.serialization.Serializable
import okio.buffer
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/kotlin/project/ProjectApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package project
import com.amplitude.project.DeploymentsResponse
import com.amplitude.project.ProjectApiV1
import com.amplitude.util.json
import com.sun.net.httpserver.HttpServer
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respond
import io.ktor.http.HttpMethod
Expand All @@ -13,6 +14,7 @@ import kotlinx.serialization.encodeToString
import org.junit.Test
import test.deployment
import test.toSerialDeployment
import java.net.InetSocketAddress
import kotlin.test.assertEquals

class ProjectApiTest {
Expand Down Expand Up @@ -68,4 +70,28 @@ class ProjectApiTest {
assertEquals("/api/1/deployments", request.url.encodedPath)
assertEquals("Bearer $managementKey", request.headers["Authorization"])
}

@Test
fun `get deployments over okhttp engine with coroutines 1_11`(): Unit =
runBlocking {
val expected = listOf(deployment("okhttp"))
val responseBody =
json.encodeToString(
DeploymentsResponse(expected.map { it.toSerialDeployment() }),
).toByteArray()
val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0)
server.createContext("/api/1/deployments") { exchange ->
exchange.responseHeaders.add("Content-Type", "application/json")
exchange.sendResponseHeaders(200, responseBody.size.toLong())
exchange.responseBody.use { it.write(responseBody) }
}
server.start()

try {
val api = ProjectApiV1("http://127.0.0.1:${server.address.port}/", managementKey)
assertEquals(expected, api.getDeployments())
} finally {
server.stop(0)
}
}
}
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# kotlin
kotlin.code.style=official
ktorVersion=2.3.12
kotlinVersion=2.0.0
coroutinesVersion=1.9.0-RC
serializationVersion=1.7.1
ktorVersion=3.3.3
kotlinVersion=2.2.21
coroutinesVersion=1.11.0
serializationVersion=1.9.0

# logging & metrics
logbackVersion=1.4.6
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
34 changes: 21 additions & 13 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
application
id("io.ktor.plugin") version "2.3.4"
kotlin("jvm") version "2.0.0"
kotlin("plugin.serialization") version "2.0.0"
id("io.ktor.plugin") version "3.3.3"
kotlin("jvm") version "2.2.21"
kotlin("plugin.serialization") version "2.2.21"
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
}

Expand All @@ -20,7 +21,11 @@ java {
}

tasks {
withType<KotlinCompile> { kotlinOptions { jvmTarget = "17" } }
withType<KotlinCompile> {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
}
}
}

// Defined in gradle.properties
Expand All @@ -44,6 +49,6 @@ dependencies {
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("io.micrometer:micrometer-registry-prometheus:$prometheusVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktorVersion")
testImplementation("io.ktor:ktor-server-test-host-jvm:$ktorVersion")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion")
}
9 changes: 5 additions & 4 deletions service/src/main/kotlin/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.request.ApplicationRequest
import io.ktor.server.request.receiveText
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import io.ktor.server.response.respondBytes
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.routing
import io.ktor.util.decodeBase64String
import io.ktor.util.toByteArray
import io.micrometer.prometheus.PrometheusConfig
import io.micrometer.prometheus.PrometheusMeterRegistry
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -121,7 +121,7 @@ fun Application.proxyServer(evaluationProxy: EvaluationProxy) {
}
install(
createApplicationPlugin("shutdown") {
val plugin = ShutDownUrl("/shutdown") { 0 }
val plugin = ShutDownUrl("/shutdown", exitCode = { 0 })
onCall { call ->
if (call.request.uri == plugin.url) {
evaluationProxy.shutdown()
Expand Down Expand Up @@ -314,8 +314,9 @@ private fun ApplicationRequest.getUserFromHeader(): Map<String, Any?> {
/**
* Get the user from the body. Used for SDK/REST POST requests.
*/
private suspend fun ApplicationRequest.getUserFromBody(): Map<String, Any?> {
val userJson = this.receiveChannel().toByteArray().toString(Charsets.UTF_8)
@VisibleForTesting
internal suspend fun ApplicationRequest.getUserFromBody(): Map<String, Any?> {
val userJson = call.receiveText()
return json.decodeFromString<JsonObject>(userJson).toAnyMap()
}

Expand Down
2 changes: 1 addition & 1 deletion service/src/main/kotlin/plugins/Monitoring.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.metrics.micrometer.MicrometerMetrics
import io.ktor.server.plugins.callloging.CallLogging
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.server.request.path
import io.ktor.server.routing.get
import io.micrometer.prometheus.PrometheusMeterRegistry
Expand Down
28 changes: 28 additions & 0 deletions service/src/test/kotlin/ServerTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import com.amplitude.getApiAndSecretKey
import com.amplitude.getUserFromBody
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.http.Headers
import io.ktor.server.application.call
import io.ktor.server.response.respondText
import io.ktor.server.routing.post
import io.ktor.server.routing.routing
import io.ktor.server.testing.testApplication
import io.ktor.util.encodeBase64
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -18,4 +27,23 @@ class ServerTest {
assertEquals(apiKey, result.first)
assertEquals(secretKey, result.second)
}

@Test
fun `test get user from body`() =
testApplication {
application {
routing {
post("/") {
val user = call.request.getUserFromBody()
call.respondText(user["user_id"].toString())
}
}
}

val response =
client.post("/") {
setBody("""{"user_id":"test-user"}""")
}
assertEquals("test-user", response.bodyAsText())
}
}
Loading