Skip to content

Commit 51351d6

Browse files
committed
feat: Add convenience method to use getObjects with a serializer
1 parent 1306a1d commit 51351d6

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

Diff for: CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
## [Unreleased]
2+
3+
### Added
4+
5+
- Convenience `getObjects` method with serializer (#392)
6+
17
# 2.1.5
28

39
### feat:
410
- **Search**: add basic support of extensions (#398)
511

6-
712
# 2.1.4
813

914
### Fixed:

Diff for: client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointIndexing.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,19 @@ public interface EndpointIndexing {
261261
objectIDs: List<ObjectID>,
262262
attributesToRetrieve: List<Attribute>? = null,
263263
requestOptions: RequestOptions? = null
264-
): ResponseObjects
264+
): ResponseObjects<JsonObject?>
265+
266+
/**
267+
* Get multiple records using their [ObjectID].
268+
*
269+
* @see getObject
270+
*/
271+
public suspend fun <T : Indexable> getObjects(
272+
serializer: KSerializer<T>,
273+
objectIDs: List<ObjectID>,
274+
attributesToRetrieve: List<Attribute>? = null,
275+
requestOptions: RequestOptions? = null
276+
): ResponseObjects<T?>
265277

266278
/**
267279
* Update one or more attributes of an existing record.

Diff for: client/src/commonMain/kotlin/com/algolia/search/endpoint/EndpointMultipleIndex.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.algolia.search.model.response.ResponseObjects
1818
import com.algolia.search.model.response.ResponseSearches
1919
import com.algolia.search.model.search.Query
2020
import com.algolia.search.transport.RequestOptions
21+
import kotlinx.serialization.json.JsonObject
2122

2223
public interface EndpointMultipleIndex {
2324

@@ -82,7 +83,7 @@ public interface EndpointMultipleIndex {
8283
public suspend fun multipleGetObjects(
8384
requests: List<RequestObjects>,
8485
requestOptions: RequestOptions? = null
85-
): ResponseObjects
86+
): ResponseObjects<JsonObject?>
8687

8788
/**
8889
* Perform several indexing operations in one API call.

Diff for: client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointIndexing.kt

+25-2
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,40 @@ internal class EndpointIndexingImpl(
174174
}
175175
}
176176

177-
override suspend fun getObjects(
177+
private suspend fun getObjectsInternal(
178178
objectIDs: List<ObjectID>,
179179
attributesToRetrieve: List<Attribute>?,
180180
requestOptions: RequestOptions?,
181-
): ResponseObjects {
181+
): ResponseObjects<JsonObject?> {
182182
val requests = objectIDs.map { RequestObjects(indexName, it, attributesToRetrieve) }
183183
val body = JsonNoDefaults.encodeToString(RequestRequestObjects.serializer(), RequestRequestObjects(requests))
184184

185185
return transport.request(HttpMethod.Post, CallType.Read, "${Route.IndexesV1}/*/objects", requestOptions, body)
186186
}
187187

188+
override suspend fun getObjects(
189+
objectIDs: List<ObjectID>,
190+
attributesToRetrieve: List<Attribute>?,
191+
requestOptions: RequestOptions?,
192+
): ResponseObjects<JsonObject?> {
193+
return getObjectsInternal(objectIDs, attributesToRetrieve, requestOptions)
194+
}
195+
196+
override suspend fun <T : Indexable> getObjects(
197+
serializer: KSerializer<T>,
198+
objectIDs: List<ObjectID>,
199+
attributesToRetrieve: List<Attribute>?,
200+
requestOptions: RequestOptions?,
201+
): ResponseObjects<T?> {
202+
return getObjectsInternal(objectIDs, attributesToRetrieve, requestOptions).run {
203+
ResponseObjects(results.map { optionalJsonObject ->
204+
optionalJsonObject?.let { jsonObject ->
205+
JsonNonStrict.decodeFromJsonElement(serializer, jsonObject)
206+
}
207+
}, messageOrNull)
208+
}
209+
}
210+
188211
override suspend fun partialUpdateObject(
189212
objectID: ObjectID,
190213
partial: Partial,

Diff for: client/src/commonMain/kotlin/com/algolia/search/endpoint/internal/EndpointMultipleIndex.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.algolia.search.transport.RequestOptions
2727
import com.algolia.search.transport.internal.Transport
2828
import io.ktor.http.HttpMethod
2929
import kotlinx.serialization.builtins.ListSerializer
30+
import kotlinx.serialization.json.JsonObject
3031
import kotlinx.serialization.json.buildJsonObject
3132

3233
internal class EndpointMultipleIndexImpl(
@@ -54,7 +55,7 @@ internal class EndpointMultipleIndexImpl(
5455
override suspend fun multipleGetObjects(
5556
requests: List<RequestObjects>,
5657
requestOptions: RequestOptions?,
57-
): ResponseObjects {
58+
): ResponseObjects<JsonObject?> {
5859
val body = JsonNoDefaults.encodeToString(RequestRequestObjects.serializer(), RequestRequestObjects(requests))
5960

6061
return transport.request(HttpMethod.Post, CallType.Read, "${Route.IndexesV1}/*/objects", requestOptions, body)

Diff for: client/src/commonMain/kotlin/com/algolia/search/model/response/ResponseObjects.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package com.algolia.search.model.response
33
import com.algolia.search.serialize.internal.Key
44
import kotlinx.serialization.SerialName
55
import kotlinx.serialization.Serializable
6-
import kotlinx.serialization.json.JsonObject
76

87
@Serializable
9-
public data class ResponseObjects(
8+
public data class ResponseObjects<T>(
109
/**
1110
* List of requested records. If a record is not found, it will be marked as null in the list.
1211
*/
13-
@SerialName(Key.Results) val results: List<JsonObject?>,
12+
@SerialName(Key.Results) val results: List<T?>,
1413
/**
1514
* Optional error message in case of failure to retrieve a requested record.
1615
*/

Diff for: client/src/commonTest/kotlin/documentation/methods/indexing/DocGetObjects.kt

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ internal class DocGetObjects {
6161
fun snippet3() {
6262
runTest {
6363
index.getObjects(listOf(ObjectID("myID1"), ObjectID("myID2")))
64+
index.getObjects(Contact.serializer(), listOf(ObjectID("myID1"), ObjectID("myID2")))
6465
}
6566
}
6667

@@ -71,6 +72,7 @@ internal class DocGetObjects {
7172
val attributes = listOf(Attribute("firstname"), Attribute("lastname"))
7273

7374
index.getObjects(objectIDs, attributes)
75+
index.getObjects(Contact.serializer(), objectIDs, attributes)
7476
}
7577
}
7678

@@ -82,6 +84,7 @@ internal class DocGetObjects {
8284
}
8385

8486
index.getObjects(listOf(ObjectID("myID1"), ObjectID("myID2")), requestOptions = requestOptions)
87+
index.getObjects(Contact.serializer(), listOf(ObjectID("myID1"), ObjectID("myID2")), requestOptions = requestOptions)
8588
}
8689
}
8790
}

Diff for: client/src/commonTest/kotlin/suite/TestSuiteIndexing.kt

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ internal class TestSuiteIndexing {
7676
getObjects(objectIDs).results
7777
.filterNotNull()
7878
.map { Json.decodeFromJsonElement(Data.serializer(), it) } shouldEqual batches
79+
getObjects(Data.serializer(), objectIDs).results
80+
.filterNotNull() shouldEqual batches
7981
browse().nbHits shouldEqual 1007
8082
revisions += replaceObject(Data.serializer(), updateA)
8183
revisions += partialUpdateObject(dataE.objectID, Partial.Increment(attributeValue, 1))

0 commit comments

Comments
 (0)