Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add convenience method to use getObjects with a serializer #392

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
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