From 9bc7453e3e451159db4be699697663b3d3b6f65f Mon Sep 17 00:00:00 2001 From: Zoe Maas Date: Tue, 29 Oct 2024 14:23:54 +0100 Subject: [PATCH] chore: added Default fetch service --- gradle/libs.versions.toml | 1 + .../openid-federation-client/build.gradle.kts | 2 +- .../sphereon/oid/fed/client/fetch/Fetch.js.kt | 27 +++++++++++++++++++ .../oid/fed/client/fetch/Fetch.jvm.kt | 21 +++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0456ccf1..5ceb00ef 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -56,6 +56,7 @@ ktor-client-mock-js = { module = "io.ktor:ktor-client-mock-js", version.ref = "k ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" } ktor-client-cio-jvm = { module = "io.ktor:ktor-client-cio-jvm", version.ref = "ktor" } ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" } +ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktor" } kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect" } springboot-actuator = { group = "org.springframework.boot", name = "spring-boot-starter-actuator" } diff --git a/modules/openid-federation-client/build.gradle.kts b/modules/openid-federation-client/build.gradle.kts index e643499a..fdae948b 100644 --- a/modules/openid-federation-client/build.gradle.kts +++ b/modules/openid-federation-client/build.gradle.kts @@ -68,7 +68,7 @@ kotlin { val jvmMain by getting { dependencies { - implementation(libs.ktor.client.cio) + implementation(libs.ktor.client.java) implementation(libs.nimbus.jose.jwt) } } diff --git a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt index 5d701ac7..efb91b73 100644 --- a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt +++ b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt @@ -3,6 +3,11 @@ package com.sphereon.oid.fed.client.fetch import com.sphereon.oid.fed.client.crypto.AbstractCryptoService import com.sphereon.oid.fed.client.service.DefaultCallbacks import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.engine.js.Js +import io.ktor.client.request.get +import io.ktor.http.HttpHeaders +import io.ktor.http.headers import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.asPromise @@ -70,3 +75,25 @@ actual fun fetchService(platformCallback: IFetchCallbackMarkerType): IFetchServi @JsExport actual external interface IFetchCallbackMarkerType + +@JsExport +class DefaultFetchJSImpl : IFetchCallbackServiceJS { + + private val FETCH_SERVICE_JS_SCOPE = "FetchServiceJS" + + override fun getHttpClient(): Promise { + return CoroutineScope(context = CoroutineName(FETCH_SERVICE_JS_SCOPE)).async { + return@async HttpClient(Js) + }.asPromise() + } + + override fun fetchStatement(endpoint: String): Promise { + return CoroutineScope(context = CoroutineName(FETCH_SERVICE_JS_SCOPE)).async { + return@async getHttpClient().await().get(endpoint) { + headers { + append(HttpHeaders.Accept, "application/entity-statement+jwt") + } + }.body() as String + }.asPromise() + } +} diff --git a/modules/openid-federation-client/src/jvmMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.jvm.kt b/modules/openid-federation-client/src/jvmMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.jvm.kt index 942b20ef..a84dcb73 100644 --- a/modules/openid-federation-client/src/jvmMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.jvm.kt +++ b/modules/openid-federation-client/src/jvmMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.jvm.kt @@ -1,5 +1,12 @@ package com.sphereon.oid.fed.client.fetch +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.engine.java.* +import io.ktor.client.request.get +import io.ktor.http.HttpHeaders +import io.ktor.http.headers + actual fun fetchService(platformCallback: IFetchCallbackMarkerType): IFetchService { if (platformCallback !is IFetchCallbackService) { throw IllegalArgumentException("Platform callback is not of type IFetchCallbackService, but ${platformCallback.javaClass.canonicalName}") @@ -8,3 +15,17 @@ actual fun fetchService(platformCallback: IFetchCallbackMarkerType): IFetchServi } actual interface IFetchCallbackMarkerType + +class DefaultFetchJvmImpl : IFetchCallbackService { + override suspend fun fetchStatement(endpoint: String): String { + return getHttpClient().get(endpoint) { + headers { + append(HttpHeaders.Accept, "application/entity-statement+jwt") + } + }.body() as String + } + + override suspend fun getHttpClient(): HttpClient { + return HttpClient(Java) + } +}