Skip to content

Commit

Permalink
Improve toString methods and refactor PackageSearchApiClient class
Browse files Browse the repository at this point in the history
In this commit, toString methods were added to both ApiPackage and ApiRepository classes. Moreover, in the PackageSearchApiClient class, the obsolete constructor and several method signatures were refactored. After this, HttpRequestBuilder became optional for the majority of methods. The higher default value (from seconds to minutes) was also set to the isOnlineFlow function.
  • Loading branch information
lamba92 committed May 10, 2024
1 parent 20f2d67 commit 7d8bad1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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<T>(method, url, body, requestBuilder, cache).body<R>()

public suspend fun getKnownRepositories(requestBuilder: (HttpRequestBuilder.() -> Unit)?): List<ApiRepository> =
public suspend fun getKnownRepositories(requestBuilder: (HttpRequestBuilder.() -> Unit)? = null): List<ApiRepository> =
httpClient.request(endpoints.knownRepositories) {
method = HttpMethod.Get
header(HttpHeaders.Accept, ContentType.Application.Json)
Expand All @@ -165,7 +155,7 @@ public class PackageSearchApiClient(

public suspend fun getPackageInfoByIds(
ids: Set<String>,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
requestBuilder: (HttpRequestBuilder.() -> Unit)? = null,
): Map<String, ApiPackage> =
defaultRequest<_, List<ApiPackage>>(
method = HttpMethod.Post,
Expand All @@ -176,7 +166,7 @@ public class PackageSearchApiClient(

public suspend fun getPackageInfoByIdHashes(
ids: Set<String>,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
requestBuilder: (HttpRequestBuilder.() -> Unit)? = null,
): Map<String, ApiPackage> =
defaultRequest<_, List<ApiPackage>>(
method = HttpMethod.Post,
Expand All @@ -187,7 +177,7 @@ public class PackageSearchApiClient(

public suspend fun searchPackages(
request: SearchPackagesRequest,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
requestBuilder: (HttpRequestBuilder.() -> Unit)? = null,
): List<ApiPackage> =
defaultRequest<_, List<ApiPackage>>(
method = HttpMethod.Post,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -231,7 +221,7 @@ public class PackageSearchApiClient(

public suspend fun refreshPackagesInfo(
request: RefreshPackagesInfoRequest,
requestBuilder: (HttpRequestBuilder.() -> Unit)?,
requestBuilder: (HttpRequestBuilder.() -> Unit)? = null,
): List<ApiPackage> =
defaultRequest<_, List<ApiPackage>>(
method = HttpMethod.Post,
Expand All @@ -240,7 +230,7 @@ public class PackageSearchApiClient(
requestBuilder = requestBuilder,
)

public fun isOnlineFlow(pollingInterval: Duration): Flow<Boolean> =
public fun isOnlineFlow(pollingInterval: Duration = 1.minutes): Flow<Boolean> =
flow {
while (true) {
val request =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ public data class PackageSearchApiException(
val remoteStackTrace: List<String> = 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public data class ApiMavenPackage(
public override val authors: List<Author>
get() = versions.latest.authors

override fun toString(): String = id

@Serializable
@SerialName("mavenVersion")
public data class MavenVersion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public data class ApiMavenRepository(
) : ApiRepository {
public override val name: String
get() = friendlyName

override fun toString(): String = "GitHub Repo: $url"
}

// @Serializable
Expand Down

0 comments on commit 7d8bad1

Please sign in to comment.