Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit a00949b

Browse files
author
Clément Le Provost
committed
Merge branch 'master' of github.com:algolia/algoliasearch-client-android
2 parents d352673 + 2b20642 commit a00949b

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/Index.java

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,23 @@ public Request getObjectsAsync(final @NonNull List<String> objectIDs, @NonNull C
364364
}.start();
365365
}
366366

367+
/**
368+
* Get several objects from this index (asynchronously), optionally restricting the retrieved content (asynchronously).
369+
*
370+
* @param objectIDs Identifiers of objects to retrieve.
371+
* @param attributesToRetrieve List of attributes to retrieve.
372+
* @param completionHandler The listener that will be notified of the request's outcome.
373+
* @return A cancellable request.
374+
*/
375+
public Request getObjectsAsync(final @NonNull List<String> objectIDs, final List<String> attributesToRetrieve, @NonNull CompletionHandler completionHandler) {
376+
return getClient().new AsyncTaskRequest(completionHandler) {
377+
@NonNull
378+
@Override JSONObject run() throws AlgoliaException {
379+
return getObjects(objectIDs, attributesToRetrieve);
380+
}
381+
}.start();
382+
}
383+
367384
/**
368385
* Wait until the publication of a task on the server (helper).
369386
* All server tasks are asynchronous. This method helps you check that a task is published.
@@ -649,15 +666,8 @@ protected JSONObject getObject(String objectID) throws AlgoliaException {
649666
*/
650667
protected JSONObject getObject(String objectID, List<String> attributesToRetrieve) throws AlgoliaException {
651668
try {
652-
StringBuilder params = new StringBuilder();
653-
params.append("?attributes=");
654-
for (int i = 0; i < attributesToRetrieve.size(); ++i) {
655-
if (i > 0) {
656-
params.append(",");
657-
}
658-
params.append(URLEncoder.encode(attributesToRetrieve.get(i), "UTF-8"));
659-
}
660-
return client.getRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8") + params.toString(), false);
669+
String params = encodeAttributes(attributesToRetrieve, true);
670+
return client.getRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8") + params, false);
661671
} catch (UnsupportedEncodingException e) {
662672
throw new RuntimeException(e);
663673
}
@@ -670,22 +680,53 @@ protected JSONObject getObject(String objectID, List<String> attributesToRetriev
670680
* @throws AlgoliaException
671681
*/
672682
protected JSONObject getObjects(List<String> objectIDs) throws AlgoliaException {
683+
return getObjects(objectIDs, null);
684+
}
685+
686+
/**
687+
* Get several objects from this index
688+
*
689+
* @param objectIDs the array of unique identifier of objects to retrieve
690+
* @param attributesToRetrieve contains the list of attributes to retrieve.
691+
* @throws AlgoliaException
692+
*/
693+
protected JSONObject getObjects(List<String> objectIDs, List<String> attributesToRetrieve) throws AlgoliaException {
673694
try {
674695
JSONArray requests = new JSONArray();
675696
for (String id : objectIDs) {
676697
JSONObject request = new JSONObject();
677698
request.put("indexName", this.indexName);
678699
request.put("objectID", id);
700+
request.put("attributesToRetrieve", encodeAttributes(attributesToRetrieve, false));
679701
requests.put(request);
680702
}
681703
JSONObject body = new JSONObject();
682704
body.put("requests", requests);
683705
return client.postRequest("/1/indexes/*/objects", body.toString(), true);
684-
} catch (JSONException e) {
706+
} catch (JSONException | UnsupportedEncodingException e) {
685707
throw new AlgoliaException(e.getMessage());
686708
}
687709
}
688710

711+
@Nullable
712+
private String encodeAttributes(List<String> attributesToRetrieve, boolean forURL) throws UnsupportedEncodingException {
713+
if (attributesToRetrieve == null) {
714+
return null;
715+
}
716+
717+
StringBuilder params = new StringBuilder();
718+
if (forURL) {
719+
params.append("?attributesToRetrieve=");
720+
}
721+
for (int i = 0; i < attributesToRetrieve.size(); ++i) {
722+
if (i > 0) {
723+
params.append(",");
724+
}
725+
params.append(URLEncoder.encode(attributesToRetrieve.get(i), "UTF-8"));
726+
}
727+
return params.toString();
728+
}
729+
689730
/**
690731
* Update partially an object (only update attributes passed in argument)
691732
*
@@ -993,7 +1034,8 @@ Map<String, List<String>> computeDisjunctiveRefinements(@NonNull List<String> di
9931034
* @param refinements The current refinements, mapping facet names to a list of values.
9941035
* @return A list of queries suitable for {@link Index#multipleQueries}.
9951036
*/
996-
private @NonNull List<Query> computeDisjunctiveFacetingQueries(@NonNull Query query, @NonNull List<String> disjunctiveFacets, @NonNull Map<String, List<String>> refinements) {
1037+
private @NonNull
1038+
List<Query> computeDisjunctiveFacetingQueries(@NonNull Query query, @NonNull List<String> disjunctiveFacets, @NonNull Map<String, List<String>> refinements) {
9971039
// Retain only refinements corresponding to the disjunctive facets.
9981040
Map<String, List<String>> disjunctiveRefinements = computeDisjunctiveRefinements(disjunctiveFacets, refinements);
9991041

algoliasearch/src/test/java/com/algolia/search/saas/IndexTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.net.Socket;
3838
import java.util.ArrayList;
3939
import java.util.Arrays;
40+
import java.util.Collections;
4041
import java.util.HashMap;
4142
import java.util.List;
4243
import java.util.Map;
@@ -870,14 +871,44 @@ public void doRequestCompleted(JSONObject content, AlgoliaException error) {
870871
@Test
871872
public void testUserAgent() throws Exception {
872873
// Test the default value.
873-
String userAgent = (String)Whitebox.getInternalState(client, "userAgentRaw");
874+
String userAgent = (String) Whitebox.getInternalState(client, "userAgentRaw");
874875
assertTrue(userAgent.matches("^Algolia for Android \\([0-9.]+\\); Android \\(([0-9.]+|unknown)\\)$"));
875876

876877
// Manipulate the list.
877878
assertFalse(client.hasUserAgent(new Client.LibraryVersion("toto", "6.6.6")));
878879
client.addUserAgent(new Client.LibraryVersion("toto", "6.6.6"));
879880
assertTrue(client.hasUserAgent(new Client.LibraryVersion("toto", "6.6.6")));
880-
userAgent = (String)Whitebox.getInternalState(client, "userAgentRaw");
881+
userAgent = (String) Whitebox.getInternalState(client, "userAgentRaw");
881882
assertTrue(userAgent.matches("^.*; toto \\(6.6.6\\)$"));
882883
}
884+
885+
@Test
886+
public void testGetObjectAttributes() throws AlgoliaException {
887+
for (String id : ids) {
888+
JSONObject object = index.getObject(id);
889+
assertEquals("The retrieved object should have two attributes.", 2, object.names().length());
890+
object = index.getObject(id, Collections.singletonList("objectID"));
891+
assertEquals("The retrieved object should have only one attribute.", 1, object.names().length());
892+
assertTrue("The retrieved object should have one objectID attribute.", object.optString("objectID", "").length() > 0);
893+
}
894+
}
895+
896+
@Test
897+
public void testGetObjectsAttributes() throws AlgoliaException {
898+
try {
899+
JSONArray results = index.getObjects(ids).getJSONArray("results");
900+
for (int i = 0; i < results.length(); i++) {
901+
JSONObject object = results.getJSONObject(i);
902+
assertEquals("The retrieved object should have two attributes.", 2, object.names().length());
903+
}
904+
results = index.getObjects(ids, Collections.singletonList("objectID")).getJSONArray("results");
905+
for (int i = 0; i < results.length(); i++) {
906+
JSONObject object = results.getJSONObject(i);
907+
assertEquals("The retrieved object should have only one attribute.", 1, object.names().length());
908+
assertTrue("The retrieved object should have one objectID attribute.", object.optString("objectID", "").length() > 0);
909+
}
910+
} catch (JSONException e) {
911+
fail(e.getMessage());
912+
}
913+
}
883914
}

0 commit comments

Comments
 (0)