diff --git a/CHANGELOG.md b/CHANGELOG.md index d96e91175..b09992c06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,14 @@ +## [Unreleased] + +### Added + +- Convenience `getObjects` method with serializer (#392) + # 2.1.5 ### feat: - **Search**: add basic support of extensions (#398) - # 2.1.4 ### Fixed: diff --git a/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointIndexing.kt b/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointIndexing.kt index f8d26813d..1a58f8f93 100644 --- a/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointIndexing.kt +++ b/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointIndexing.kt @@ -261,7 +261,19 @@ public interface EndpointIndexing { objectIDs: List, attributesToRetrieve: List? = null, requestOptions: RequestOptions? = null - ): ResponseObjects + ): ResponseObjects + + /** + * Get multiple records using their [ObjectID]. + * + * @see getObject + */ + public suspend fun getObjects( + serializer: KSerializer, + objectIDs: List, + attributesToRetrieve: List? = null, + requestOptions: RequestOptions? = null + ): ResponseObjects /** * Update one or more attributes of an existing record. diff --git a/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointMultipleIndex.kt b/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointMultipleIndex.kt index f3170e14c..4d2d0bf70 100644 --- a/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointMultipleIndex.kt +++ b/client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointMultipleIndex.kt @@ -18,6 +18,7 @@ import com.algolia.search.model.response.ResponseObjects import com.algolia.search.model.response.ResponseSearches import com.algolia.search.model.search.Query import com.algolia.search.transport.RequestOptions +import kotlinx.serialization.json.JsonObject public interface EndpointMultipleIndex { @@ -82,7 +83,7 @@ public interface EndpointMultipleIndex { public suspend fun multipleGetObjects( requests: List, requestOptions: RequestOptions? = null - ): ResponseObjects + ): ResponseObjects /** * Perform several indexing operations in one API call. diff --git a/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointIndexing.kt b/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointIndexing.kt index 5e2681612..dd04593d9 100644 --- a/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointIndexing.kt +++ b/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointIndexing.kt @@ -174,17 +174,40 @@ internal class EndpointIndexingImpl( } } - override suspend fun getObjects( + private suspend fun getObjectsInternal( objectIDs: List, attributesToRetrieve: List?, requestOptions: RequestOptions?, - ): ResponseObjects { + ): ResponseObjects { val requests = objectIDs.map { RequestObjects(indexName, it, attributesToRetrieve) } val body = JsonNoDefaults.encodeToString(RequestRequestObjects.serializer(), RequestRequestObjects(requests)) return transport.request(HttpMethod.Post, CallType.Read, "${Route.IndexesV1}/*/objects", requestOptions, body) } + override suspend fun getObjects( + objectIDs: List, + attributesToRetrieve: List?, + requestOptions: RequestOptions?, + ): ResponseObjects { + return getObjectsInternal(objectIDs, attributesToRetrieve, requestOptions) + } + + override suspend fun getObjects( + serializer: KSerializer, + objectIDs: List, + attributesToRetrieve: List?, + requestOptions: RequestOptions?, + ): ResponseObjects { + return getObjectsInternal(objectIDs, attributesToRetrieve, requestOptions).run { + ResponseObjects(results.map { optionalJsonObject -> + optionalJsonObject?.let { jsonObject -> + JsonNonStrict.decodeFromJsonElement(serializer, jsonObject) + } + }, messageOrNull) + } + } + override suspend fun partialUpdateObject( objectID: ObjectID, partial: Partial, diff --git a/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointMultipleIndex.kt b/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointMultipleIndex.kt index 50d32514a..ddf7b4d82 100644 --- a/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointMultipleIndex.kt +++ b/client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointMultipleIndex.kt @@ -27,6 +27,7 @@ import com.algolia.search.transport.RequestOptions import com.algolia.search.transport.internal.Transport import io.ktor.http.HttpMethod import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.buildJsonObject internal class EndpointMultipleIndexImpl( @@ -54,7 +55,7 @@ internal class EndpointMultipleIndexImpl( override suspend fun multipleGetObjects( requests: List, requestOptions: RequestOptions?, - ): ResponseObjects { + ): ResponseObjects { val body = JsonNoDefaults.encodeToString(RequestRequestObjects.serializer(), RequestRequestObjects(requests)) return transport.request(HttpMethod.Post, CallType.Read, "${Route.IndexesV1}/*/objects", requestOptions, body) diff --git a/client/src/commonMain/kotlin/com/algolia/search/model/response/ResponseObjects.kt b/client/src/commonMain/kotlin/com/algolia/search/model/response/ResponseObjects.kt index 220a78db7..18fdd4c60 100644 --- a/client/src/commonMain/kotlin/com/algolia/search/model/response/ResponseObjects.kt +++ b/client/src/commonMain/kotlin/com/algolia/search/model/response/ResponseObjects.kt @@ -3,14 +3,13 @@ package com.algolia.search.model.response import com.algolia.search.serialize.internal.Key import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import kotlinx.serialization.json.JsonObject @Serializable -public data class ResponseObjects( +public data class ResponseObjects( /** * List of requested records. If a record is not found, it will be marked as null in the list. */ - @SerialName(Key.Results) val results: List, + @SerialName(Key.Results) val results: List, /** * Optional error message in case of failure to retrieve a requested record. */ diff --git a/client/src/commonTest/kotlin/documentation/methods/indexing/DocGetObjects.kt b/client/src/commonTest/kotlin/documentation/methods/indexing/DocGetObjects.kt index d8eb37f03..5a3ab5ffb 100644 --- a/client/src/commonTest/kotlin/documentation/methods/indexing/DocGetObjects.kt +++ b/client/src/commonTest/kotlin/documentation/methods/indexing/DocGetObjects.kt @@ -61,6 +61,7 @@ internal class DocGetObjects { fun snippet3() { runTest { index.getObjects(listOf(ObjectID("myID1"), ObjectID("myID2"))) + index.getObjects(Contact.serializer(), listOf(ObjectID("myID1"), ObjectID("myID2"))) } } @@ -71,6 +72,7 @@ internal class DocGetObjects { val attributes = listOf(Attribute("firstname"), Attribute("lastname")) index.getObjects(objectIDs, attributes) + index.getObjects(Contact.serializer(), objectIDs, attributes) } } @@ -82,6 +84,7 @@ internal class DocGetObjects { } index.getObjects(listOf(ObjectID("myID1"), ObjectID("myID2")), requestOptions = requestOptions) + index.getObjects(Contact.serializer(), listOf(ObjectID("myID1"), ObjectID("myID2")), requestOptions = requestOptions) } } } diff --git a/client/src/commonTest/kotlin/suite/TestSuiteIndexing.kt b/client/src/commonTest/kotlin/suite/TestSuiteIndexing.kt index 69d5099b1..89705bf00 100644 --- a/client/src/commonTest/kotlin/suite/TestSuiteIndexing.kt +++ b/client/src/commonTest/kotlin/suite/TestSuiteIndexing.kt @@ -76,6 +76,8 @@ internal class TestSuiteIndexing { getObjects(objectIDs).results .filterNotNull() .map { Json.decodeFromJsonElement(Data.serializer(), it) } shouldEqual batches + getObjects(Data.serializer(), objectIDs).results + .filterNotNull() shouldEqual batches browse().nbHits shouldEqual 1007 revisions += replaceObject(Data.serializer(), updateA) revisions += partialUpdateObject(dataE.objectID, Partial.Increment(attributeValue, 1))