-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathClient.java
More file actions
404 lines (330 loc) · 14.7 KB
/
Client.java
File metadata and controls
404 lines (330 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package io.getstream.client;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.getstream.core.utils.Auth.*;
import com.google.common.collect.Iterables;
import io.getstream.core.Region;
import io.getstream.core.Stream;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.OKHTTPClientAdapter;
import io.getstream.core.http.Response;
import io.getstream.core.http.Token;
import io.getstream.core.models.*;
import io.getstream.core.options.RequestOption;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import io.getstream.core.utils.Auth;
import java8.util.concurrent.CompletableFuture;
public final class Client {
private final String secret;
private final Stream stream;
private Client(String key, String secret, URL baseURL, HTTPClient httpClient) {
this.secret = secret;
this.stream = new Stream(key, baseURL, httpClient);
}
public static Builder builder(String apiKey, String secret) {
return new Builder(apiKey, secret);
}
public CompletableFuture<Activity> updateActivityByID(
String id, Map<String, Object> set, Iterable<String> unset) throws StreamException {
return updateActivityByID(id, set, Iterables.toArray(unset, String.class));
}
public CompletableFuture<Activity> updateActivityByID(ActivityUpdate update)
throws StreamException {
return updateActivityByID(update.getID(), update.getSet(), update.getUnset());
}
public CompletableFuture<Activity> updateActivityByID(
String id, Map<String, Object> set, String[] unset, RequestOption... options)
throws StreamException {
final Token token = buildActivityToken(secret, TokenAction.WRITE);
return stream.updateActivityByID(token, id, set, unset, options);
}
public CompletableFuture<Activity> updateActivityByForeignID(
ForeignIDTimePair foreignIDTimePair, Map<String, Object> set, Iterable<String> unset)
throws StreamException {
checkNotNull(foreignIDTimePair, "No activity to update");
return updateActivityByForeignID(
foreignIDTimePair.getForeignID(), foreignIDTimePair.getTime(), set, unset);
}
public CompletableFuture<Activity> updateActivityByForeignID(
ForeignIDTimePair foreignIDTimePair, Map<String, Object> set, String[] unset)
throws StreamException {
checkNotNull(foreignIDTimePair, "No activity to update");
return updateActivityByForeignID(
foreignIDTimePair.getForeignID(), foreignIDTimePair.getTime(), set, unset);
}
public CompletableFuture<Activity> updateActivityByForeignID(
String foreignID, Date timestamp, Map<String, Object> set, Iterable<String> unset)
throws StreamException {
return updateActivityByForeignID(
foreignID, timestamp, set, Iterables.toArray(unset, String.class));
}
public CompletableFuture<Activity> updateActivityByForeignID(ActivityUpdate update)
throws StreamException {
return updateActivityByForeignID(
update.getForeignID(), update.getTime(), update.getSet(), update.getUnset());
}
public CompletableFuture<Activity> updateActivityByForeignID(
String foreignID, Date timestamp, Map<String, Object> set, String[] unset,
RequestOption... options) throws StreamException {
final Token token = buildActivityToken(secret, TokenAction.WRITE);
return stream.updateActivityByForeignID(token, foreignID, timestamp, set, unset, options);
}
public CompletableFuture<OGData> openGraph(URL url) throws StreamException {
final Token token = buildOpenGraphToken(secret);
return stream.openGraph(token, url);
}
public CompletableFuture<List<Activity>> updateActivitiesByID(Iterable<ActivityUpdate> updates)
throws StreamException {
return updateActivitiesByID(Iterables.toArray(updates, ActivityUpdate.class), new RequestOption[0]);
}
public CompletableFuture<List<Activity>> updateActivitiesByID(ActivityUpdate... updates)
throws StreamException {
return updateActivitiesByID(updates, new RequestOption[0]);
}
public CompletableFuture<List<Activity>> updateActivitiesByID(
ActivityUpdate[] updates, RequestOption... options) throws StreamException {
final Token token = buildActivityToken(secret, TokenAction.WRITE);
return stream.updateActivitiesByID(token, updates, options);
}
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(
Iterable<ActivityUpdate> updates) throws StreamException {
return updateActivitiesByForeignID(Iterables.toArray(updates, ActivityUpdate.class), new RequestOption[0]);
}
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(ActivityUpdate... updates)
throws StreamException {
return updateActivitiesByForeignID(updates, new RequestOption[0]);
}
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(
ActivityUpdate[] updates, RequestOption... options) throws StreamException {
final Token token = buildActivityToken(secret, TokenAction.WRITE);
return stream.updateActivitiesByForeignID(token, updates, options);
}
public static final class Builder {
private static final String DEFAULT_HOST = "stream-io-api.com";
private final String apiKey;
private final String secret;
private HTTPClient httpClient;
private String scheme = "https";
private String region = Region.US_EAST.toString();
private String host = DEFAULT_HOST;
private int port = 443;
public Builder(String apiKey, String secret) {
checkNotNull(apiKey, "API key can't be null");
checkNotNull(secret, "Secret can't be null");
checkArgument(!apiKey.isEmpty(), "API key can't be empty");
checkArgument(!secret.isEmpty(), "Secret can't be empty");
this.apiKey = apiKey;
this.secret = secret;
}
public Builder httpClient(HTTPClient httpClient) {
checkNotNull(httpClient, "HTTP client can't be null");
this.httpClient = httpClient;
return this;
}
public Builder scheme(String scheme) {
checkNotNull(scheme, "Scheme can't be null");
checkArgument(!scheme.isEmpty(), "Scheme can't be empty");
this.scheme = scheme;
return this;
}
public Builder host(String host) {
checkNotNull(host, "Host can't be null");
checkArgument(!host.isEmpty(), "Host can't be empty");
this.host = host;
return this;
}
public Builder port(int port) {
checkArgument(port > 0, "Port has to be a non-zero positive number");
this.port = port;
return this;
}
public Builder region(Region region) {
checkNotNull(region, "Region can't be null");
this.region = region.toString();
return this;
}
public Builder region(String region) {
checkNotNull(region, "Region can't be null");
checkArgument(!region.isEmpty(), "Region can't be empty");
this.region = region;
return this;
}
private String buildHost() {
final StringBuilder sb = new StringBuilder();
if (host.equals(DEFAULT_HOST)) {
sb.append(region).append(".");
}
sb.append(host);
return sb.toString();
}
public Client build() throws MalformedURLException {
if (httpClient == null) {
httpClient = new OKHTTPClientAdapter();
}
return new Client(apiKey, secret, new URL(scheme, buildHost(), port, ""), httpClient);
}
}
public <T> T getHTTPClientImplementation() {
return stream.getHTTPClientImplementation();
}
public Token frontendToken(String userID) {
return buildFrontendToken(secret, userID);
}
public Token frontendToken(String userID, Date expiresAt) {
return buildFrontendToken(secret, userID, expiresAt);
}
public FlatFeed flatFeed(FeedID id) {
return new FlatFeed(this, id);
}
public FlatFeed flatFeed(String slug, String userID) {
return flatFeed(new FeedID(slug, userID));
}
public AggregatedFeed aggregatedFeed(FeedID id) {
return new AggregatedFeed(this, id);
}
public AggregatedFeed aggregatedFeed(String slug, String userID) {
return aggregatedFeed(new FeedID(slug, userID));
}
public NotificationFeed notificationFeed(FeedID id) {
return new NotificationFeed(this, id);
}
public NotificationFeed notificationFeed(String slug, String userID) {
return notificationFeed(new FeedID(slug, userID));
}
public User user(String userID) {
return new User(this, userID);
}
public BatchClient batch() {
return new BatchClient(secret, stream.batch());
}
public CollectionsClient collections() {
return new CollectionsClient(secret, stream.collections());
}
public PersonalizationClient personalization() {
return new PersonalizationClient(secret, stream.personalization());
}
public AnalyticsClient analytics() {
return new AnalyticsClient(secret, stream.analytics());
}
public ReactionsClient reactions() {
return new ReactionsClient(secret, stream.reactions());
}
public ModerationClient moderation() {
return new ModerationClient(secret, stream.moderation());
}
public AuditLogsClient auditLogs() {
return new AuditLogsClient(secret, stream);
}
public FileStorageClient files() {
return new FileStorageClient(secret, stream.files());
}
public ImageStorageClient images() {
return new ImageStorageClient(secret, stream.images());
}
CompletableFuture<Response> getActivities(FeedID feed, RequestOption... options)
throws StreamException {
final Token token = buildFeedToken(secret, feed, TokenAction.READ);
return stream.getActivities(token, feed, options);
}
CompletableFuture<Response> getEnrichedActivities(FeedID feed, RequestOption... options)
throws StreamException {
final Token token = buildFeedToken(secret, feed, TokenAction.READ);
return stream.getEnrichedActivities(token, feed, options);
}
CompletableFuture<Response> addActivity(FeedID feed, Activity activity, RequestOption... options)
throws StreamException {
final Token token = buildFeedToken(secret, feed, TokenAction.WRITE);
return stream.addActivity(token, feed, activity, options);
}
CompletableFuture<Response> addActivities(FeedID feed, Activity... activities)
throws StreamException {
return addActivities(feed, activities, new RequestOption[0]);
}
CompletableFuture<Response> addActivities(
FeedID feed, Activity[] activities, RequestOption... options) throws StreamException {
final Token token = buildFeedToken(secret, feed, TokenAction.WRITE);
return stream.addActivities(token, feed, activities, options);
}
CompletableFuture<Response> removeActivityByID(FeedID feed, String id) throws StreamException {
final Token token = buildFeedToken(secret, feed, TokenAction.DELETE);
return stream.removeActivityByID(token, feed, id);
}
CompletableFuture<Response> removeActivityByForeignID(FeedID feed, String foreignID)
throws StreamException {
final Token token = buildFeedToken(secret, feed, TokenAction.DELETE);
return stream.removeActivityByForeignID(token, feed, foreignID);
}
CompletableFuture<Response> follow(FeedID source, FeedID target, int activityCopyLimit)
throws StreamException {
final Token token = buildFollowToken(secret, source, TokenAction.WRITE);
final Token targetToken = buildFeedToken(secret, target, TokenAction.READ);
return stream.follow(token, targetToken, source, target, activityCopyLimit);
}
CompletableFuture<Response> getFollowers(FeedID feed, RequestOption... options)
throws StreamException {
final Token token = buildFollowToken(secret, feed, TokenAction.READ);
return stream.getFollowers(token, feed, options);
}
CompletableFuture<Response> getFollowed(FeedID feed, RequestOption... options)
throws StreamException {
final Token token = buildFollowToken(secret, feed, TokenAction.READ);
return stream.getFollowed(token, feed, options);
}
CompletableFuture<Response> unfollow(FeedID source, FeedID target, RequestOption... options)
throws StreamException {
final Token token = buildFollowToken(secret, source, TokenAction.DELETE);
return stream.unfollow(token, source, target, options);
}
CompletableFuture<Response> getFollowStats(
FeedID feed, String[] followerSlugs, String[] followingSlugs) throws StreamException {
final Token token = buildFollowToken(secret, TokenAction.READ);
return stream.getFollowStats(token, feed, followerSlugs, followingSlugs);
}
CompletableFuture<Response> updateActivityToTargets(
FeedID feed, Activity activity, FeedID[] add, FeedID[] remove, FeedID[] newTargets)
throws StreamException {
final Token token = buildToTargetUpdateToken(secret, feed, TokenAction.WRITE);
return stream.updateActivityToTargets(token, feed, activity, add, remove, newTargets);
}
CompletableFuture<Response> getUser(String id) throws StreamException {
final Token token = buildUsersToken(secret, TokenAction.READ);
return stream.getUser(token, id, false);
}
CompletableFuture<Response> deleteUser(String id) throws StreamException {
final Token token = buildUsersToken(secret, TokenAction.DELETE);
return stream.deleteUser(token, id);
}
CompletableFuture<Response> getOrCreateUser(String id, Data data) throws StreamException {
final Token token = buildUsersToken(secret, TokenAction.WRITE);
return stream.createUser(token, id, data, true);
}
CompletableFuture<Response> createUser(String id, Data data) throws StreamException {
final Token token = buildUsersToken(secret, TokenAction.WRITE);
return stream.createUser(token, id, data, false);
}
CompletableFuture<Response> updateUser(String id, Data data) throws StreamException {
final Token token = buildUsersToken(secret, TokenAction.WRITE);
return stream.updateUser(token, id, data);
}
CompletableFuture<Response> userProfile(String id) throws StreamException {
final Token token = buildUsersToken(secret, TokenAction.READ);
return stream.getUser(token, id, true);
}
public CompletableFuture<Object> deleteActivities(BatchDeleteActivitiesRequest request) throws StreamException {
final Token token = buildDataPrivacyToken(secret, Auth.TokenAction.WRITE);
return stream.deleteActivities(token, request);
}
public CompletableFuture<Object> deleteReactions(BatchDeleteReactionsRequest request) throws StreamException {
final Token token = buildDataPrivacyToken(secret, Auth.TokenAction.WRITE);
return stream.deleteReactions(token, request);
}
public CompletableFuture<ExportIDsResponse> exportUserActivities(String userId) throws StreamException {
final Token token = buildDataPrivacyToken(secret, Auth.TokenAction.READ);
return stream.exportUserActivities(token, userId);
}
}