Skip to content

Commit

Permalink
feat: Add convenience method to use getObjects with a serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ema987 committed Feb 6, 2023
1 parent 7437aa6 commit 900c5c9
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [Unreleased]

### Added

- Convenience `getObjects` method with serializer (#392)

# 2.1.3

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,19 @@ public interface EndpointIndexing {
objectIDs: List<ObjectID>,
attributesToRetrieve: List<Attribute>? = null,
requestOptions: RequestOptions? = null
): ResponseObjects
): ResponseObjects<JsonObject?>

/**
* Get multiple records using their [ObjectID].
*
* @see getObject
*/
public suspend fun <T : Indexable> getObjects(
serializer: KSerializer<T>,
objectIDs: List<ObjectID>,
attributesToRetrieve: List<Attribute>? = null,
requestOptions: RequestOptions? = null
): ResponseObjects<T?>

/**
* Update one or more attributes of an existing record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -82,7 +83,7 @@ public interface EndpointMultipleIndex {
public suspend fun multipleGetObjects(
requests: List<RequestObjects>,
requestOptions: RequestOptions? = null
): ResponseObjects
): ResponseObjects<JsonObject?>

/**
* Perform several indexing operations in one API call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,40 @@ internal class EndpointIndexingImpl(
}
}

override suspend fun getObjects(
private suspend fun getObjectsInternal(
objectIDs: List<ObjectID>,
attributesToRetrieve: List<Attribute>?,
requestOptions: RequestOptions?,
): ResponseObjects {
): ResponseObjects<JsonObject?> {
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<ObjectID>,
attributesToRetrieve: List<Attribute>?,
requestOptions: RequestOptions?,
): ResponseObjects<JsonObject?> {
return getObjectsInternal(objectIDs, attributesToRetrieve, requestOptions)
}

override suspend fun <T : Indexable> getObjects(
serializer: KSerializer<T>,
objectIDs: List<ObjectID>,
attributesToRetrieve: List<Attribute>?,
requestOptions: RequestOptions?,
): ResponseObjects<T?> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -54,7 +55,7 @@ internal class EndpointMultipleIndexImpl(
override suspend fun multipleGetObjects(
requests: List<RequestObjects>,
requestOptions: RequestOptions?,
): ResponseObjects {
): ResponseObjects<JsonObject?> {
val body = JsonNoDefaults.encodeToString(RequestRequestObjects.serializer(), RequestRequestObjects(requests))

return transport.request(HttpMethod.Post, CallType.Read, "${Route.IndexesV1}/*/objects", requestOptions, body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
/**
* 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<JsonObject?>,
@SerialName(Key.Results) val results: List<T?>,
/**
* Optional error message in case of failure to retrieve a requested record.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
}

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

index.getObjects(objectIDs, attributes)
index.getObjects(Contact.serializer(), objectIDs, attributes)
}
}

Expand All @@ -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)
}
}
}
2 changes: 2 additions & 0 deletions client/src/commonTest/kotlin/suite/TestSuiteIndexing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 900c5c9

Please sign in to comment.