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

[PBE-1127]add ExternalRankingVars #143

Merged
merged 2 commits into from
Jan 9, 2024
Merged
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
14 changes: 14 additions & 0 deletions example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ public static void main(String[] args) throws Exception {
// playerFeed.addActivity(activity);
userFeed.addActivity(activity);

// Get activities sorted by the ranking method along with externalRankingVars
Map<String, Object> mp=new LinkedHashMap();

mp.put("boolVal",true);
mp.put("music",1);
mp.put("sports",2.1);
mp.put("string","str");
response = userFeed.feed.getActivities(
new Limit(69),
new Offset(13),
DefaultOptions.DEFAULT_FILTER,
"rank",
new RankingVars(mp)
);
/* -------------------------------------------------------- */

// Batch following many feeds
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/getstream/client/FlatFeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import io.getstream.core.models.FeedID;
import io.getstream.core.options.*;
import io.getstream.core.utils.DefaultOptions;

import java.io.IOException;
import java.util.List;

import java8.util.concurrent.CompletableFuture;
import java8.util.concurrent.CompletionException;

Expand Down Expand Up @@ -104,6 +106,28 @@ limit, offset, filter, DefaultOptions.DEFAULT_MARKER, new Ranking(ranking)
});
}

CompletableFuture<List<Activity>> getActivities(
Limit limit, Offset offset, Filter filter, String ranking, RankingVars rankingVars) throws StreamException {

final RequestOption[] options =
ranking == null
? new RequestOption[] {limit, offset, filter, DefaultOptions.DEFAULT_MARKER}
: new RequestOption[] {
limit, offset, filter, DefaultOptions.DEFAULT_MARKER, new Ranking(ranking), rankingVars
};
return getClient()
.getActivities(getID(), options)
.thenApply(
response -> {
try {
return deserializeContainer(response, Activity.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
}


public <T> CompletableFuture<List<T>> getCustomActivities(Class<T> type) throws StreamException {
return getCustomActivities(
type,
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/getstream/core/options/RankingVars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.getstream.core.options;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.getstream.core.http.Request;
import java8.util.concurrent.CompletionException;

import java.io.IOException;
import java.util.Map;

public final class RankingVars implements RequestOption{

private final String rankingVarsJSON;

public RankingVars(Map<String, Object> externalVars) {
String rankingVarsJSON;
try {
ObjectMapper objectMapper = new ObjectMapper();
rankingVarsJSON = objectMapper.writeValueAsString(externalVars);
}
catch (IOException e){
throw new CompletionException(e);
}

this.rankingVarsJSON = rankingVarsJSON;
}

public String getRankingVars() {
return rankingVarsJSON;
}

@Override
public void apply(Request.Builder builder) {
builder.addQueryParameter("ranking_vars", rankingVarsJSON);
}
}

41 changes: 37 additions & 4 deletions src/test/java/io/getstream/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Request;
import io.getstream.core.http.Response;
import io.getstream.core.options.EnrichmentFlags;
import io.getstream.core.options.Filter;
import io.getstream.core.options.Limit;
import io.getstream.core.options.Offset;
import io.getstream.core.options.*;

import java.net.URL;
import java.util.Map;
import java.util.LinkedHashMap;

import io.getstream.core.utils.DefaultOptions;
import java8.util.concurrent.CompletableFuture;
import org.junit.Test;

Expand Down Expand Up @@ -155,6 +157,37 @@ public void enrichedFeedURL() throws Exception {
assertNull(httpClient.lastRequest.getBody());
}

@Test
public void feedURLExternalRanking() throws Exception {
MockHTTPClient httpClient = new MockHTTPClient();
Client client = Client.builder(apiKey, secret).httpClient(httpClient).build();
FlatFeed feed = client.flatFeed("flat", "1");

Map<String, Object> mp=new LinkedHashMap();

mp.put("boolVal",true);
mp.put("music",1);
mp.put("sports",2.1);
mp.put("string","str");
feed.getActivities(
new Limit(69),
new Offset(13),
DefaultOptions.DEFAULT_FILTER,
"rank",
new RankingVars(mp)
);

assertNotNull(httpClient.lastRequest);
URL feedURL =
new URL(
"https://us-east-api.stream-io-api.com:443/api/v1.0/feed/flat/1/?api_key="
+ apiKey
+ "&limit=69&offset=13&ranking=rank&ranking_vars=%7B%22boolVal%22:true,%22music%22:1,%22sports%22:2.1,%22string%22:%22str%22%7D");
assertEquals(httpClient.lastRequest.getURL(), feedURL);
assertEquals(httpClient.lastRequest.getMethod(), Request.Method.GET);
assertNull(httpClient.lastRequest.getBody());
}

@Test
public void feedFollowURL() throws Exception {
MockHTTPClient httpClient = new MockHTTPClient();
Expand Down
Loading