Skip to content

Commit

Permalink
fix: fetch class
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Oct 12, 2024
1 parent 5a85d7b commit 1c32dee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlin.js.JsExport
import kotlin.jvm.JvmStatic

interface ICallbackService<PlatformCallbackType> {
fun register(platformCallback: PlatformCallbackType?): ICallbackService<PlatformCallbackType>
Expand All @@ -19,27 +14,23 @@ interface IFetchService {
}

interface IFetchServiceInternal {
fun fetchStatement(
suspend fun fetchStatement(
endpoint: String
): Deferred<String>
): String
}

interface IFetchCallbackService : ICallbackService<IFetchService>, IFetchService, IFetchServiceInternal

@JsExport
object FetchServiceObject : IFetchCallbackService {
@JvmStatic
private lateinit var platformCallback: IFetchService
private lateinit var httpClient: HttpClient

override fun fetchStatement(endpoint: String): Deferred<String> {
return GlobalScope.async {
httpClient.get(endpoint) {
headers {
append(HttpHeaders.Accept, "application/entity-statement+jwt")
}
}.body()
}
override suspend fun fetchStatement(endpoint: String): String {
return httpClient.get(endpoint) {
headers {
append(HttpHeaders.Accept, "application/entity-statement+jwt")
}
}.body()
}

override fun getHttpClient(): HttpClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ class SimpleCache<K, V> {
): MutableList<String>? {

val entityConfigurationJwt =
fetchService.fetchStatement(getEntityConfigurationEndpoint(entityIdentifier)).await()
fetchService.fetchStatement(getEntityConfigurationEndpoint(entityIdentifier))

val decodedEntityConfiguration = decodeJWTComponents(entityConfigurationJwt)


// need to verify JWT

val entityStatement: EntityConfigurationStatement =
Expand Down Expand Up @@ -88,7 +87,7 @@ class SimpleCache<K, V> {
if (cache.get(authorityConfigurationEndpoint) != null) return null

val authorityEntityConfigurationJwt =
fetchService.fetchStatement(authorityConfigurationEndpoint).await() ?: return null
fetchService.fetchStatement(authorityConfigurationEndpoint)
cache.put(authorityConfigurationEndpoint, authorityEntityConfigurationJwt)

val authorityEntityConfiguration: EntityConfigurationStatement =
Expand All @@ -105,7 +104,7 @@ class SimpleCache<K, V> {
val subordinateStatementEndpoint =
getSubordinateStatementEndpoint(authorityEntityFetchEndpoint, entityIdentifier)

val subordinateStatementJwt = fetchService.fetchStatement(subordinateStatementEndpoint).await()
val subordinateStatementJwt = fetchService.fetchStatement(subordinateStatementEndpoint)
val subordinateStatement: SubordinateStatement =
mapEntityStatement(subordinateStatementJwt, SubordinateStatement::class)
?: return null
Expand Down

0 comments on commit 1c32dee

Please sign in to comment.