Skip to content

Commit

Permalink
233 - Fix http headers for http requests. (#134)
Browse files Browse the repository at this point in the history
* Fix http headers for http requests.

Previously the function io.ktor.http.headers was used which is very wrong, since it does not register any header.

* Fix http headers for http requests.

Previously the function io.ktor.http.headers was used which is very wrong, since it does not register any header.
  • Loading branch information
lamba92 authored Mar 1, 2024
1 parent 4364f25 commit 037b01a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import io.ktor.client.engine.java.Java
import io.ktor.client.plugins.DefaultRequest
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logging
import io.ktor.http.headers
import io.ktor.client.request.headers
import java.util.concurrent.CompletableFuture
import kotlin.io.path.absolutePathString
import kotlin.io.path.div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.jetbrains.packagesearch.plugin.core.nitrite.NitriteFilters
import com.jetbrains.packagesearch.plugin.core.nitrite.coroutines.CoroutineObjectRepository
import com.jetbrains.packagesearch.plugin.core.nitrite.insert
import com.jetbrains.packagesearch.plugin.core.utils.suspendSafe
import io.ktor.client.request.HttpRequestBuilder
import korlibs.crypto.SHA256
import kotlin.random.Random
import kotlin.time.Duration
Expand Down Expand Up @@ -32,19 +33,28 @@ class PackageSearchApiPackageCache(

private val cachesMutex = Mutex()

override suspend fun getPackageInfoByIds(ids: Set<String>) =
override suspend fun getPackageInfoByIds(
ids: Set<String>,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
): Map<String, ApiPackage> =
getPackages(
ids = ids,
useHashes = false
)

override suspend fun getPackageInfoByIdHashes(ids: Set<String>): Map<String, ApiPackage> =
override suspend fun getPackageInfoByIdHashes(
ids: Set<String>,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
): Map<String, ApiPackage> =
getPackages(
ids = ids,
useHashes = true
)

override suspend fun searchPackages(request: SearchPackagesRequest): List<ApiPackage> {
override suspend fun searchPackages(
request: SearchPackagesRequest,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
): List<ApiPackage> {
val sha = SHA256.digest(Json.encodeToString(request).toByteArray()).base64
val contextName = "${Random.nextInt()} | ${this::class}#searchPackages"
logDebug(contextName) { "Searching for packages | searchSha = $sha" }
Expand All @@ -66,7 +76,7 @@ class PackageSearchApiPackageCache(
.also { searchCache.insert(ApiSearchEntry(it, sha, request)) }
}

override suspend fun getKnownRepositories(): List<ApiRepository> {
override suspend fun getKnownRepositories(requestBuilder: (HttpRequestBuilder.() -> Unit)?): List<ApiRepository> {
val cached = repositoryCache.find().singleOrNull()
val isOnlineStatus = isOnline()
if (cached != null && (Clock.System.now() < cached.lastUpdate + maxAge || !isOnlineStatus)) {
Expand Down Expand Up @@ -129,7 +139,7 @@ class PackageSearchApiPackageCache(

else -> {
// retrieve the packages from the network
val networkResults = runCatching { apiCall(missingIds) }
val networkResults = runCatching { apiCall(missingIds, null) }
.suspendSafe()
.onFailure { logDebug("${this::class.qualifiedName}#getPackages", it) }
if (networkResults.isSuccess) {
Expand Down

0 comments on commit 037b01a

Please sign in to comment.