Skip to content

Commit 81f1b14

Browse files
authored
Merge pull request #143 from GetStream/external_ranking_vars
[PBE-1127]add ExternalRankingVars
2 parents e998534 + 66c882c commit 81f1b14

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

example/Example.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ public static void main(String[] args) throws Exception {
221221
// playerFeed.addActivity(activity);
222222
userFeed.addActivity(activity);
223223

224+
// Get activities sorted by the ranking method along with externalRankingVars
225+
Map<String, Object> mp=new LinkedHashMap();
226+
227+
mp.put("boolVal",true);
228+
mp.put("music",1);
229+
mp.put("sports",2.1);
230+
mp.put("string","str");
231+
response = userFeed.feed.getActivities(
232+
new Limit(69),
233+
new Offset(13),
234+
DefaultOptions.DEFAULT_FILTER,
235+
"rank",
236+
new RankingVars(mp)
237+
);
224238
/* -------------------------------------------------------- */
225239

226240
// Batch following many feeds

src/main/java/io/getstream/client/FlatFeed.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import io.getstream.core.models.FeedID;
99
import io.getstream.core.options.*;
1010
import io.getstream.core.utils.DefaultOptions;
11+
1112
import java.io.IOException;
1213
import java.util.List;
14+
1315
import java8.util.concurrent.CompletableFuture;
1416
import java8.util.concurrent.CompletionException;
1517

@@ -104,6 +106,28 @@ limit, offset, filter, DefaultOptions.DEFAULT_MARKER, new Ranking(ranking)
104106
});
105107
}
106108

109+
CompletableFuture<List<Activity>> getActivities(
110+
Limit limit, Offset offset, Filter filter, String ranking, RankingVars rankingVars) throws StreamException {
111+
112+
final RequestOption[] options =
113+
ranking == null
114+
? new RequestOption[] {limit, offset, filter, DefaultOptions.DEFAULT_MARKER}
115+
: new RequestOption[] {
116+
limit, offset, filter, DefaultOptions.DEFAULT_MARKER, new Ranking(ranking), rankingVars
117+
};
118+
return getClient()
119+
.getActivities(getID(), options)
120+
.thenApply(
121+
response -> {
122+
try {
123+
return deserializeContainer(response, Activity.class);
124+
} catch (StreamException | IOException e) {
125+
throw new CompletionException(e);
126+
}
127+
});
128+
}
129+
130+
107131
public <T> CompletableFuture<List<T>> getCustomActivities(Class<T> type) throws StreamException {
108132
return getCustomActivities(
109133
type,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.getstream.core.options;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.getstream.core.http.Request;
5+
import java8.util.concurrent.CompletionException;
6+
7+
import java.io.IOException;
8+
import java.util.Map;
9+
10+
public final class RankingVars implements RequestOption{
11+
12+
private final String rankingVarsJSON;
13+
14+
public RankingVars(Map<String, Object> externalVars) {
15+
String rankingVarsJSON;
16+
try {
17+
ObjectMapper objectMapper = new ObjectMapper();
18+
rankingVarsJSON = objectMapper.writeValueAsString(externalVars);
19+
}
20+
catch (IOException e){
21+
throw new CompletionException(e);
22+
}
23+
24+
this.rankingVarsJSON = rankingVarsJSON;
25+
}
26+
27+
public String getRankingVars() {
28+
return rankingVarsJSON;
29+
}
30+
31+
@Override
32+
public void apply(Request.Builder builder) {
33+
builder.addQueryParameter("ranking_vars", rankingVarsJSON);
34+
}
35+
}
36+

src/test/java/io/getstream/client/ClientTest.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import io.getstream.core.http.HTTPClient;
77
import io.getstream.core.http.Request;
88
import io.getstream.core.http.Response;
9-
import io.getstream.core.options.EnrichmentFlags;
10-
import io.getstream.core.options.Filter;
11-
import io.getstream.core.options.Limit;
12-
import io.getstream.core.options.Offset;
9+
import io.getstream.core.options.*;
10+
1311
import java.net.URL;
12+
import java.util.Map;
13+
import java.util.LinkedHashMap;
14+
15+
import io.getstream.core.utils.DefaultOptions;
1416
import java8.util.concurrent.CompletableFuture;
1517
import org.junit.Test;
1618

@@ -155,6 +157,37 @@ public void enrichedFeedURL() throws Exception {
155157
assertNull(httpClient.lastRequest.getBody());
156158
}
157159

160+
@Test
161+
public void feedURLExternalRanking() throws Exception {
162+
MockHTTPClient httpClient = new MockHTTPClient();
163+
Client client = Client.builder(apiKey, secret).httpClient(httpClient).build();
164+
FlatFeed feed = client.flatFeed("flat", "1");
165+
166+
Map<String, Object> mp=new LinkedHashMap();
167+
168+
mp.put("boolVal",true);
169+
mp.put("music",1);
170+
mp.put("sports",2.1);
171+
mp.put("string","str");
172+
feed.getActivities(
173+
new Limit(69),
174+
new Offset(13),
175+
DefaultOptions.DEFAULT_FILTER,
176+
"rank",
177+
new RankingVars(mp)
178+
);
179+
180+
assertNotNull(httpClient.lastRequest);
181+
URL feedURL =
182+
new URL(
183+
"https://us-east-api.stream-io-api.com:443/api/v1.0/feed/flat/1/?api_key="
184+
+ apiKey
185+
+ "&limit=69&offset=13&ranking=rank&ranking_vars=%7B%22boolVal%22:true,%22music%22:1,%22sports%22:2.1,%22string%22:%22str%22%7D");
186+
assertEquals(httpClient.lastRequest.getURL(), feedURL);
187+
assertEquals(httpClient.lastRequest.getMethod(), Request.Method.GET);
188+
assertNull(httpClient.lastRequest.getBody());
189+
}
190+
158191
@Test
159192
public void feedFollowURL() throws Exception {
160193
MockHTTPClient httpClient = new MockHTTPClient();

0 commit comments

Comments
 (0)