Skip to content

Commit 9eceef7

Browse files
author
Max Klyga
committed
Version 3.1.3
* Add batch partial update methods to client
1 parent 3d38bd5 commit 9eceef7

File tree

6 files changed

+239
-13
lines changed

6 files changed

+239
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Add the following dependency to your pom.xml:
1212
<dependency>
1313
<groupId>io.getstream.client</groupId>
1414
<artifactId>stream-java</artifactId>
15-
<version>3.1.2</version>
15+
<version>3.1.3</version>
1616
</dependency>
1717
```
1818

1919
or in your build.gradle:
2020

2121
```gradle
22-
compile 'io.getstream.client:stream-java:3.1.2'
22+
compile 'io.getstream.client:stream-java:3.1.3'
2323
```
2424

2525
In case you want to download the artifact and put it manually into your project,

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group 'io.getstream.client'
12-
version = '3.1.2'
12+
version = '3.1.3'
1313

1414
dependencies {
1515
testCompile 'org.junit.jupiter:junit-jupiter-api:5.3.1'

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.net.MalformedURLException;
1515
import java.net.URL;
1616
import java.util.Date;
17+
import java.util.List;
1718
import java.util.Map;
1819
import java.util.concurrent.CompletableFuture;
1920

@@ -38,6 +39,10 @@ public CompletableFuture<Activity> updateActivityByID(String id, Map<String, Obj
3839
return updateActivityByID(id, set, Iterables.toArray(unset, String.class));
3940
}
4041

42+
public CompletableFuture<Activity> updateActivityByID(ActivityUpdate update) throws StreamException {
43+
return updateActivityByID(update.getID(), update.getSet(), update.getUnset());
44+
}
45+
4146
public CompletableFuture<Activity> updateActivityByID(String id, Map<String, Object> set, String[] unset) throws StreamException {
4247
final Token token = buildActivityToken(secret, TokenAction.WRITE);
4348
return stream.updateActivityByID(token, id, set, unset);
@@ -57,6 +62,10 @@ public CompletableFuture<Activity> updateActivityByForeignID(String foreignID, D
5762
return updateActivityByForeignID(foreignID, timestamp, set, Iterables.toArray(unset, String.class));
5863
}
5964

65+
public CompletableFuture<Activity> updateActivityByForeignID(ActivityUpdate update) throws StreamException {
66+
return updateActivityByForeignID(update.getForeignID(), update.getTime(), update.getSet(), update.getUnset());
67+
}
68+
6069
public CompletableFuture<Activity> updateActivityByForeignID(String foreignID, Date timestamp, Map<String, Object> set, String[] unset) throws StreamException {
6170
final Token token = buildActivityToken(secret, TokenAction.WRITE);
6271
return stream.updateActivityByForeignID(token, foreignID, timestamp, set, unset);
@@ -67,6 +76,24 @@ public CompletableFuture<OGData> openGraph(URL url) throws StreamException {
6776
return stream.openGraph(token, url);
6877
}
6978

79+
public CompletableFuture<List<Activity>> updateActivitiesByID(Iterable<ActivityUpdate> updates) throws StreamException {
80+
return updateActivitiesByID(Iterables.toArray(updates, ActivityUpdate.class));
81+
}
82+
83+
public CompletableFuture<List<Activity>> updateActivitiesByID(ActivityUpdate... updates) throws StreamException {
84+
final Token token = buildActivityToken(secret, TokenAction.WRITE);
85+
return stream.updateActivitiesByID(token, updates);
86+
}
87+
88+
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(Iterable<ActivityUpdate> updates) throws StreamException {
89+
return updateActivitiesByForeignID(Iterables.toArray(updates, ActivityUpdate.class));
90+
}
91+
92+
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(ActivityUpdate... updates) throws StreamException {
93+
final Token token = buildActivityToken(secret, TokenAction.WRITE);
94+
return stream.updateActivitiesByForeignID(token, updates);
95+
}
96+
7097
public static final class Builder {
7198
private static final String DEFAULT_HOST = "stream-io-api.com";
7299

src/main/java/io/getstream/core/Stream.java

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import io.getstream.core.http.HTTPClient;
88
import io.getstream.core.http.Response;
99
import io.getstream.core.http.Token;
10-
import io.getstream.core.models.Activity;
11-
import io.getstream.core.models.Data;
12-
import io.getstream.core.models.FeedID;
13-
import io.getstream.core.models.OGData;
10+
import io.getstream.core.models.*;
1411
import io.getstream.core.options.CustomQueryParameter;
1512
import io.getstream.core.options.RequestOption;
1613

@@ -20,6 +17,7 @@
2017
import java.net.URL;
2118
import java.util.Arrays;
2219
import java.util.Date;
20+
import java.util.List;
2321
import java.util.Map;
2422
import java.util.concurrent.CompletableFuture;
2523
import java.util.concurrent.CompletionException;
@@ -28,8 +26,7 @@
2826
import static com.google.common.base.Preconditions.checkNotNull;
2927
import static io.getstream.core.utils.Request.*;
3028
import static io.getstream.core.utils.Routes.*;
31-
import static io.getstream.core.utils.Serialization.deserialize;
32-
import static io.getstream.core.utils.Serialization.toJSON;
29+
import static io.getstream.core.utils.Serialization.*;
3330

3431
public final class Stream {
3532
private final String key;
@@ -70,6 +67,33 @@ public StreamImages images() {
7067
return new StreamImages(key, baseURL, httpClient);
7168
}
7269

70+
public CompletableFuture<List<Activity>> updateActivitiesByID(Token token, ActivityUpdate[] updates) throws StreamException {
71+
checkNotNull(updates, "No updates");
72+
checkArgument(updates.length > 0, "No updates");
73+
for (ActivityUpdate update : updates) {
74+
checkNotNull(update.getID(), "No activity to update");
75+
checkNotNull(update.getSet(), "No activity properties to set");
76+
checkNotNull(update.getUnset(), "No activity properties to unset");
77+
}
78+
79+
try {
80+
final byte[] payload = toJSON(new Object() {
81+
public final ActivityUpdate[] changes = updates;
82+
});
83+
final URL url = buildActivityUpdateURL(baseURL);
84+
return httpClient.execute(buildPost(url, key, token, payload))
85+
.thenApply(response -> {
86+
try {
87+
return deserializeContainer(response, "activities", Activity.class);
88+
} catch (StreamException | IOException e) {
89+
throw new CompletionException(e);
90+
}
91+
});
92+
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
93+
throw new StreamException(e);
94+
}
95+
}
96+
7397
public CompletableFuture<Activity> updateActivityByID(Token token, String id, Map<String, Object> set, String[] unset) throws StreamException {
7498
checkNotNull(id, "No activity to update");
7599
checkNotNull(set, "No activity properties to set");
@@ -99,6 +123,34 @@ public CompletableFuture<Activity> updateActivityByID(Token token, String id, Ma
99123
}
100124
}
101125

126+
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(Token token, ActivityUpdate[] updates) throws StreamException {
127+
checkNotNull(updates, "No updates");
128+
checkArgument(updates.length > 0, "No updates");
129+
for (ActivityUpdate update : updates) {
130+
checkNotNull(update.getForeignID(), "No activity to update");
131+
checkNotNull(update.getTime(), "Missing timestamp");
132+
checkNotNull(update.getSet(), "No activity properties to set");
133+
checkNotNull(update.getUnset(), "No activity properties to unset");
134+
}
135+
136+
try {
137+
final byte[] payload = toJSON(new Object() {
138+
public final ActivityUpdate[] changes = updates;
139+
});
140+
final URL url = buildActivityUpdateURL(baseURL);
141+
return httpClient.execute(buildPost(url, key, token, payload))
142+
.thenApply(response -> {
143+
try {
144+
return deserializeContainer(response, "activities", Activity.class);
145+
} catch (StreamException | IOException e) {
146+
throw new CompletionException(e);
147+
}
148+
});
149+
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
150+
throw new StreamException(e);
151+
}
152+
}
153+
102154
public CompletableFuture<Activity> updateActivityByForeignID(Token token, String foreignID, Date timestamp, Map<String, Object> set, String[] unset) throws StreamException {
103155
checkNotNull(foreignID, "No activity to update");
104156
checkNotNull(timestamp, "Missing timestamp");
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package io.getstream.core.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.OptBoolean;
7+
import com.google.common.collect.ImmutableMap;
8+
import com.google.common.collect.Lists;
9+
10+
import java.util.Date;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
@JsonInclude(JsonInclude.Include.NON_NULL)
15+
public class ActivityUpdate {
16+
private final String id;
17+
private final String foreignID;
18+
private final Date time;
19+
private final Map<String, Object> set;
20+
private final List<String> unset;
21+
22+
ActivityUpdate(Builder builder) {
23+
if (builder.id != null) {
24+
id = builder.id;
25+
foreignID = null;
26+
time = null;
27+
} else {
28+
id = null;
29+
foreignID = builder.foreignID;
30+
time = builder.time;
31+
}
32+
set = builder.set;
33+
unset = builder.unset;
34+
}
35+
36+
public String getID() {
37+
return id;
38+
}
39+
40+
@JsonProperty("foreign_id")
41+
public String getForeignID() {
42+
return foreignID;
43+
}
44+
45+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", lenient = OptBoolean.FALSE, timezone = "UTC")
46+
public Date getTime() {
47+
return time;
48+
}
49+
50+
public Map<String, Object> getSet() {
51+
return set;
52+
}
53+
54+
public List<String> getUnset() {
55+
return unset;
56+
}
57+
58+
public static Builder builder() {
59+
return new Builder();
60+
}
61+
62+
public static final class Builder {
63+
private String id;
64+
private String foreignID;
65+
private Date time;
66+
private Map<String, Object> set;
67+
private List<String> unset;
68+
69+
public Builder id(String id) {
70+
this.id = id;
71+
return this;
72+
}
73+
74+
public Builder foreignID(String foreignID) {
75+
this.foreignID = foreignID;
76+
return this;
77+
}
78+
79+
public Builder time(Date time) {
80+
this.time = time;
81+
return this;
82+
}
83+
84+
public Builder foreignIDTimePair(ForeignIDTimePair pair) {
85+
foreignID = pair.getForeignID();
86+
time = pair.getTime();
87+
return this;
88+
}
89+
90+
public Builder set(Map<String, Object> set) {
91+
this.set = ImmutableMap.copyOf(set);
92+
return this;
93+
}
94+
95+
public Builder set(Iterable<Map.Entry<String, Object>> set) {
96+
this.set = ImmutableMap.copyOf(set);
97+
return this;
98+
}
99+
100+
public Builder unset(Iterable<String> unset) {
101+
this.unset = Lists.newArrayList(unset);
102+
return this;
103+
}
104+
105+
public Builder unset(String... unset) {
106+
this.unset = Lists.newArrayList(unset);
107+
return this;
108+
}
109+
110+
public ActivityUpdate build() {
111+
return new ActivityUpdate(this);
112+
}
113+
}
114+
}

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.google.common.collect.ImmutableMap;
44
import io.getstream.core.http.OKHTTPClientAdapter;
5-
import io.getstream.core.models.Activity;
6-
import io.getstream.core.models.FeedID;
7-
import io.getstream.core.models.FollowRelation;
8-
import io.getstream.core.models.ForeignIDTimePair;
5+
import io.getstream.core.models.*;
96
import okhttp3.OkHttpClient;
107
import org.junit.jupiter.api.Test;
118

@@ -103,6 +100,42 @@ void partiallyUpdateActivityByForeignID() {
103100
});
104101
}
105102

103+
@Test
104+
void partiallyUpdateActivitiesByID() {
105+
List<Activity>[] result = new List[1];
106+
assertDoesNotThrow(() -> {
107+
Client client = Client.builder(apiKey, secret).build();
108+
109+
ActivityUpdate update = ActivityUpdate.builder()
110+
.id("1657b300-a648-11d5-8080-800020fde6c3")
111+
.set(ImmutableMap.of("value", "message"))
112+
.unset(Collections.emptyList())
113+
.build();
114+
115+
result[0] = client.updateActivitiesByID(update).join();
116+
});
117+
}
118+
119+
@Test
120+
void partiallyUpdateActivitiesByForeignID() {
121+
List<Activity>[] result = new List[1];
122+
assertDoesNotThrow(() -> {
123+
Client client = Client.builder(apiKey, secret).build();
124+
125+
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
126+
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
127+
128+
ActivityUpdate update = ActivityUpdate.builder()
129+
.foreignID("foreignID")
130+
.time(isoFormat.parse("2001-09-11T00:01:02.000000"))
131+
.set(ImmutableMap.of("value", "message"))
132+
.unset(Collections.emptyList())
133+
.build();
134+
135+
result[0] = client.updateActivitiesByForeignID(update).join();
136+
});
137+
}
138+
106139
@Test
107140
void getActivitiesByID() {
108141
List<Activity>[] result = new List[1];

0 commit comments

Comments
 (0)