Skip to content

Commit

Permalink
chore: added Default fetch service
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe Maas committed Oct 29, 2024
1 parent b20a846 commit 9bc7453
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion modules/openid-federation-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<HttpClient> {
return CoroutineScope(context = CoroutineName(FETCH_SERVICE_JS_SCOPE)).async {
return@async HttpClient(Js)
}.asPromise()
}

override fun fetchStatement(endpoint: String): Promise<String> {
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()
}
}
Original file line number Diff line number Diff line change
@@ -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}")
Expand All @@ -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)
}
}

0 comments on commit 9bc7453

Please sign in to comment.