diff --git a/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt b/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt index fe239cf..cc71b7e 100644 --- a/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt +++ b/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiClient.kt @@ -38,23 +38,13 @@ import org.jetbrains.packagesearch.api.v3.ApiPackage import org.jetbrains.packagesearch.api.v3.ApiProject import org.jetbrains.packagesearch.api.v3.ApiRepository import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds public class PackageSearchApiClient( public val endpoints: PackageSearchEndpoints, private val httpClient: HttpClient = defaultHttpClient(), ) { - @Suppress("UNUSED_PARAMETER") - @Deprecated( - message = "Use new constructor", - replaceWith = ReplaceWith("PackageSearchApiClient(endpoints, httpClient)"), - ) - public constructor( - endpoints: PackageSearchEndpoints, - httpClient: HttpClient = defaultHttpClient(), - scope: CoroutineScope, - pollingInterval: Duration = 1.seconds, - ) : this(endpoints, httpClient) @Serializable private data class Error(val error: Inner) { @@ -138,7 +128,7 @@ public class PackageSearchApiClient( method: HttpMethod, url: Url, body: T, - noinline requestBuilder: (HttpRequestBuilder.() -> Unit)?, + noinline requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, cache: Boolean = true, ) = httpClient.request(url) { this@request.method = method @@ -152,11 +142,11 @@ public class PackageSearchApiClient( method: HttpMethod, url: Url, body: T, - noinline requestBuilder: (HttpRequestBuilder.() -> Unit)?, + noinline requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, cache: Boolean = true, ) = defaultRawRequest(method, url, body, requestBuilder, cache).body() - public suspend fun getKnownRepositories(requestBuilder: (HttpRequestBuilder.() -> Unit)?): List = + public suspend fun getKnownRepositories(requestBuilder: (HttpRequestBuilder.() -> Unit)? = null): List = httpClient.request(endpoints.knownRepositories) { method = HttpMethod.Get header(HttpHeaders.Accept, ContentType.Application.Json) @@ -165,7 +155,7 @@ public class PackageSearchApiClient( public suspend fun getPackageInfoByIds( ids: Set, - requestBuilder: (HttpRequestBuilder.() -> Unit)?, + requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, ): Map = defaultRequest<_, List>( method = HttpMethod.Post, @@ -176,7 +166,7 @@ public class PackageSearchApiClient( public suspend fun getPackageInfoByIdHashes( ids: Set, - requestBuilder: (HttpRequestBuilder.() -> Unit)?, + requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, ): Map = defaultRequest<_, List>( method = HttpMethod.Post, @@ -187,7 +177,7 @@ public class PackageSearchApiClient( public suspend fun searchPackages( request: SearchPackagesRequest, - requestBuilder: (HttpRequestBuilder.() -> Unit)?, + requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, ): List = defaultRequest<_, List>( method = HttpMethod.Post, @@ -198,7 +188,7 @@ public class PackageSearchApiClient( public suspend fun startScroll( request: SearchPackagesStartScrollRequest, - requestBuilder: (HttpRequestBuilder.() -> Unit)?, + requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, ): SearchPackagesScrollResponse = defaultRequest<_, SearchPackagesScrollResponse>( method = HttpMethod.Post, @@ -209,7 +199,7 @@ public class PackageSearchApiClient( public suspend fun nextScroll( request: SearchPackagesNextScrollRequest, - requestBuilder: (HttpRequestBuilder.() -> Unit)?, + requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, ): SearchPackagesScrollResponse = defaultRequest<_, SearchPackagesScrollResponse>( method = HttpMethod.Post, @@ -231,7 +221,7 @@ public class PackageSearchApiClient( public suspend fun refreshPackagesInfo( request: RefreshPackagesInfoRequest, - requestBuilder: (HttpRequestBuilder.() -> Unit)?, + requestBuilder: (HttpRequestBuilder.() -> Unit)? = null, ): List = defaultRequest<_, List>( method = HttpMethod.Post, @@ -240,7 +230,7 @@ public class PackageSearchApiClient( requestBuilder = requestBuilder, ) - public fun isOnlineFlow(pollingInterval: Duration): Flow = + public fun isOnlineFlow(pollingInterval: Duration = 1.minutes): Flow = flow { while (true) { val request = diff --git a/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiException.kt b/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiException.kt index c924543..a1044dd 100644 --- a/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiException.kt +++ b/http/client/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/http/PackageSearchApiException.kt @@ -10,15 +10,16 @@ public data class PackageSearchApiException( val remoteStackTrace: List = emptyList(), ) : Throwable() { override val message: String - get() = - buildString { - append("Error response for endpoint ${request.method} ${request.url}:") - appendLine("- Headers:") - request.headers.forEach { appendLine(" -> ${it.key}: ${it.value.joinToString()}") } - appendLine("- Status code: ${statusCode.value} ${statusCode.description}") - if (remoteStackTrace.isNotEmpty()) { - append("- Remote stack trace:") - remoteStackTrace.forEach { appendLine(" $it") } - } + get() = buildString { + append("Error response for endpoint ${request.method} ${request.url}:") + appendLine("- Headers:") + request.headers.forEach { appendLine(" -> ${it.key}: ${it.value.joinToString()}") } + appendLine("- Status code: ${statusCode.value} ${statusCode.description}") + if (remoteStackTrace.isNotEmpty()) { + append("- Remote stack trace:") + remoteStackTrace.forEach { appendLine(" $it") } } + } + + override fun toString(): String = message } diff --git a/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiPackage.kt b/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiPackage.kt index e362018..1f93d5d 100644 --- a/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiPackage.kt +++ b/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiPackage.kt @@ -89,6 +89,8 @@ public data class ApiMavenPackage( public override val authors: List get() = versions.latest.authors + override fun toString(): String = id + @Serializable @SerialName("mavenVersion") public data class MavenVersion( diff --git a/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiRepository.kt b/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiRepository.kt index f9a2ccc..fa5dc76 100644 --- a/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiRepository.kt +++ b/src/commonMain/kotlin/org/jetbrains/packagesearch/api/v3/ApiRepository.kt @@ -27,6 +27,8 @@ public data class ApiMavenRepository( ) : ApiRepository { public override val name: String get() = friendlyName + + override fun toString(): String = "GitHub Repo: $url" } // @Serializable