Skip to content

Commit

Permalink
#18 Integrate CacheSystem inside API-Client
Browse files Browse the repository at this point in the history
Integrate CacheSystem inside API-Client
  • Loading branch information
fscarponi authored Sep 9, 2024
2 parents cf373e3 + 9926f77 commit 1500bff
Show file tree
Hide file tree
Showing 22 changed files with 15,965 additions and 148 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test APIClient Cache

on:
push:
branches:
[main, master]
pull_request:
branches:
[main, master]

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: '17'
- name: Run Test APIClient Cache
run: ./gradlew :http:client:jvmTest --tests "CacheTests"
5 changes: 0 additions & 5 deletions build-systems/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ kotlin {
api(packageSearchApiModelsVersions.ktor.client.js)
}
}
appleMain {
dependencies {
api(packageSearchApiModelsVersions.ktor.client.darwin)
}
}
jvmTest {
dependencies {
implementation(packageSearchApiModelsVersions.kotlinx.coroutines.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,22 @@ public class HttpClientMavenPomProvider(
groupId: String,
artifactId: String,
version: String,
): ProjectObjectModel {
return getPomFromMultipleRepositories(groupId, artifactId, version).first()
}
): ProjectObjectModel =
getPomFromMultipleRepositories(groupId, artifactId, version).first()


override suspend fun getPomFromMultipleRepositories(
groupId: String,
artifactId: String,
version: String,
): Flow<ProjectObjectModel> {
return mirrors.asFlow().map {
): Flow<ProjectObjectModel> =
mirrors.asFlow().map {
it.getPom(groupId, artifactId, version)
}
}

override suspend fun getPomByUrl(url: Url): ProjectObjectModel {
return httpClient.get(url).bodyAsPom(xml)
}
override suspend fun getPomByUrl(url: Url): ProjectObjectModel =
httpClient.get(url).bodyAsPom(xml)


private suspend fun MavenUrlBuilder.getPom(
groupId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public class PomResolver(
artifactId: String,
version: String,
): ProjectObjectModel? =
pomProvider.getPomFromMultipleRepositories(groupId, artifactId, version)
pomProvider
.getPomFromMultipleRepositories(groupId, artifactId, version)
.firstOrNull()
?.let { resolve(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ public object GoogleMavenCentralMirror : MavenUrlBuilder {
)
}

internal data class DependencyKey(val groupId: String, val artifactId: String)
internal data class DependencyKey(
val groupId: String,
val artifactId: String
)

/**
* Evaluates the value of the provided project property within the given model accessor.
Expand Down
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ kotlin {
api(packageSearchApiModelsVersions.datetime)
api(packageSearchApiModelsVersions.kotlinx.serialization.json)
api(packageSearchApiModelsVersions.krypto)
api(kotlinxDocumentStore.core)
}
}
jsMain {
dependencies {
api(npm(packageSearchApiModelsVersions.date.fns))
api(kotlinxDocumentStore.browser)
}
}
jvmMain{
dependencies{
api(kotlinxDocumentStore.mvstore)
}
}
jvmTest {
Expand Down
15 changes: 2 additions & 13 deletions buildSrc/src/main/kotlin/build-config.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,12 @@ plugins {

kotlin {
explicitApi()
jvm {
jvmToolchain(11)
}
jvm()
jvmToolchain(17)
js(IR) {
browser()
nodejs()
}
iosX64()
iosArm64()
iosSimulatorArm64()
macosArm64()
macosX64()
watchosArm64()
watchosX64()
tvosArm64()
tvosX64()
tvosSimulatorArm64()
targets.all {
compilations.all {
kotlinOptions {
Expand Down
18 changes: 18 additions & 0 deletions http/client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.gradle.kotlin.dsl.kotlin

plugins {
`build-config`
}
Expand All @@ -23,10 +25,26 @@ kotlin {
api(packageSearchApiModelsVersions.kotlinx.serialization.json)
}
}
commonTest {
dependencies {
api(packageSearchApiModelsVersions.kotlinx.coroutines.test)
api(packageSearchApiModelsVersions.ktor.client.mock)
api(packageSearchApiModelsVersions.ktor.client.logging)
api(packageSearchApiModelsVersions.mvstore)
}
}
jvmMain {
dependencies {
api(packageSearchApiModelsVersions.logback.classic)
}
}
jvmTest {
dependencies {
api(kotlin("test-junit5"))
api(kotlinxDocumentStore.mvstore)
api(packageSearchApiModelsVersions.junit.jupiter.engine)

}
}
}
}
23 changes: 23 additions & 0 deletions http/client/src/commonMain/kotlin/cache/CacheEntries.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cache

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours


internal val DEFAULT_EXPIRATION_TIME: Duration = 12.hours

@Serializable
internal data class CacheEntry<K, V>(
val key: K,
val value: V,
val createdAt: Instant = Clock.System.now()
) {

internal fun isExpired(expirationTime: Duration): Boolean =
createdAt + expirationTime < Clock.System.now()


}
Loading

0 comments on commit 1500bff

Please sign in to comment.