Repository navigation
Expand file tree
/
Copy pathStream.java
More file actions
605 lines (536 loc) · 22.4 KB
/
Copy pathStream.java
File metadata and controls
605 lines (536 loc) · 22.4 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package io.getstream.core;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.getstream.core.utils.Request.*;
import static io.getstream.core.utils.Routes.*;
import static io.getstream.core.utils.Serialization.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Response;
import io.getstream.core.http.Token;
import io.getstream.core.models.*;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.RequestOption;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java8.util.J8Arrays;
import java8.util.concurrent.CompletableFuture;
import java8.util.concurrent.CompletionException;
public final class Stream {
private final String key;
private final URL baseURL;
private final HTTPClient httpClient;
public Stream(String key, URL baseURL, HTTPClient httpClient) {
this.key = key;
this.baseURL = baseURL;
this.httpClient = httpClient;
}
public StreamBatch batch() {
return new StreamBatch(key, baseURL, httpClient);
}
public StreamCollections collections() {
return new StreamCollections(key, baseURL, httpClient);
}
public StreamPersonalization personalization() {
return new StreamPersonalization(key, baseURL, httpClient);
}
public StreamAnalytics analytics() {
return new StreamAnalytics(key, baseURL, httpClient);
}
public StreamReactions reactions() {
return new StreamReactions(key, baseURL, httpClient);
}
public Moderation moderation() {
return new Moderation(key, baseURL, httpClient);
}
public StreamFiles files() {
return new StreamFiles(key, baseURL, httpClient);
}
public StreamImages images() {
return new StreamImages(key, baseURL, httpClient);
}
public CompletableFuture<List<Activity>> updateActivitiesByID(
Token token, ActivityUpdate[] updates, RequestOption... options) throws StreamException {
checkNotNull(updates, "No updates");
checkArgument(updates.length > 0, "No updates");
for (ActivityUpdate update : updates) {
checkNotNull(update.getID(), "No activity to update");
checkNotNull(update.getSet(), "No activity properties to set");
checkNotNull(update.getUnset(), "No activity properties to unset");
}
try {
final byte[] payload =
toJSON(
new Object() {
public final ActivityUpdate[] changes = updates;
});
final URL url = buildActivityUpdateURL(baseURL);
return httpClient
.execute(buildPost(url, key, token, payload, options))
.thenApply(
response -> {
try {
return deserializeContainer(response, "activities", Activity.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Activity> updateActivityByID(
Token token, String id, Map<String, Object> set, String[] unset, RequestOption... options)
throws StreamException {
checkNotNull(id, "No activity to update");
checkNotNull(set, "No activity properties to set");
checkNotNull(unset, "No activity properties to unset");
try {
// XXX: renaming variables so we can unambiguously name payload fields 'id', 'set', 'unset'
String activityID = id;
Map<String, Object> propertiesToSet = set;
String[] propertiesToUnset = unset;
final byte[] payload =
toJSON(
new Object() {
public final String id = activityID;
public final Map<String, Object> set = propertiesToSet;
public final String[] unset = propertiesToUnset;
});
final URL url = buildActivityUpdateURL(baseURL);
return httpClient
.execute(buildPost(url, key, token, payload, options))
.thenApply(
response -> {
try {
return deserialize(response, Activity.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<List<Activity>> updateActivitiesByForeignID(
Token token, ActivityUpdate[] updates, RequestOption... options) throws StreamException {
checkNotNull(updates, "No updates");
checkArgument(updates.length > 0, "No updates");
for (ActivityUpdate update : updates) {
checkNotNull(update.getForeignID(), "No activity to update");
checkNotNull(update.getTime(), "Missing timestamp");
checkNotNull(update.getSet(), "No activity properties to set");
checkNotNull(update.getUnset(), "No activity properties to unset");
}
try {
final byte[] payload =
toJSON(
new Object() {
public final ActivityUpdate[] changes = updates;
});
final URL url = buildActivityUpdateURL(baseURL);
return httpClient
.execute(buildPost(url, key, token, payload, options))
.thenApply(
response -> {
try {
return deserializeContainer(response, "activities", Activity.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Activity> updateActivityByForeignID(
Token token,
String foreignID,
Date timestamp,
Map<String, Object> set,
String[] unset,
RequestOption... options)
throws StreamException {
checkNotNull(foreignID, "No activity to update");
checkNotNull(timestamp, "Missing timestamp");
checkNotNull(set, "No activity properties to set");
checkNotNull(unset, "No activity properties to unset");
try {
// XXX: renaming variables so we can unambiguously name payload fields 'set', 'unset'
Map<String, Object> propertiesToSet = set;
String[] propertiesToUnset = unset;
final byte[] payload =
toJSON(
new Object() {
public final String foreign_id = foreignID;
@JsonFormat(
shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS",
lenient = OptBoolean.FALSE,
timezone = "UTC")
public final Date time = timestamp;
public final Map<String, Object> set = propertiesToSet;
public final String[] unset = propertiesToUnset;
});
final URL url = buildActivityUpdateURL(baseURL);
return httpClient
.execute(buildPost(url, key, token, payload, options))
.thenApply(
response -> {
try {
return deserialize(response, Activity.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<OGData> openGraph(Token token, URL targetURL) throws StreamException {
checkNotNull(targetURL, "Missing url");
try {
final URL url = buildOpenGraphURL(baseURL);
return httpClient
.execute(
buildGet(
url, key, token, new CustomQueryParameter("url", targetURL.toExternalForm())))
.thenApply(
response -> {
try {
return deserialize(response, OGData.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public <T> T getHTTPClientImplementation() {
return httpClient.getImplementation();
}
public CompletableFuture<Response> getActivities(
Token token, FeedID feed, RequestOption... options) throws StreamException {
checkNotNull(options, "Missing request options");
try {
final URL url = buildFeedURL(baseURL, feed, "/");
return httpClient.execute(buildGet(url, key, token, options));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> getEnrichedActivities(
Token token, FeedID feed, RequestOption... options) throws StreamException {
checkNotNull(options, "Missing request options");
try {
final URL url = buildEnrichedFeedURL(baseURL, feed, "/");
return httpClient.execute(buildGet(url, key, token, options));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> addActivity(
Token token, FeedID feed, Activity activity, RequestOption... options) throws StreamException {
checkNotNull(activity, "No activity to add");
try {
final byte[] payload = toJSON(activity);
final URL url = buildFeedURL(baseURL, feed, "/");
return httpClient.execute(buildPost(url, key, token, payload, options));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> addActivities(
Token token, FeedID feed, Activity... activityObjects) throws StreamException {
return addActivities(token, feed, activityObjects, new RequestOption[0]);
}
public CompletableFuture<Response> addActivities(
Token token, FeedID feed, Activity[] activityObjects, RequestOption... options)
throws StreamException {
checkNotNull(activityObjects, "No activities to add");
try {
final byte[] payload =
toJSON(
new Object() {
public final Activity[] activities = activityObjects;
});
final URL url = buildFeedURL(baseURL, feed, "/");
return httpClient.execute(buildPost(url, key, token, payload, options));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> removeActivityByID(Token token, FeedID feed, String id)
throws StreamException {
checkNotNull(id, "No activity id to remove");
try {
final URL url = buildFeedURL(baseURL, feed, '/' + id + '/');
return httpClient.execute(buildDelete(url, key, token));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> removeActivityByForeignID(
Token token, FeedID feed, String foreignID) throws StreamException {
checkNotNull(foreignID, "No activity id to remove");
try {
final URL url = buildFeedURL(baseURL, feed, '/' + foreignID + '/');
return httpClient.execute(
buildDelete(url, key, token, new CustomQueryParameter("foreign_id", "1")));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> follow(
Token token, Token targetToken, FeedID sourceFeed, FeedID targetFeed, int activityCopyLimit)
throws StreamException {
checkNotNull(targetFeed, "No feed to follow");
checkArgument(sourceFeed != targetFeed, "Feed can't follow itself");
checkArgument(activityCopyLimit >= 0, "Activity copy limit should be a non-negative number");
try {
final byte[] payload =
toJSON(
new Object() {
public String target = targetFeed.toString();
public int activity_copy_limit = activityCopyLimit;
public String target_token = targetToken.toString();
});
final URL url = buildFeedURL(baseURL, sourceFeed, "/following/");
return httpClient.execute(buildPost(url, key, token, payload));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> getFollowers(
Token token, FeedID feed, RequestOption... options) throws StreamException {
checkNotNull(options, "Missing request options");
try {
final URL url = buildFeedURL(baseURL, feed, "/followers/");
return httpClient.execute(buildGet(url, key, token, options));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> getFollowed(Token token, FeedID feed, RequestOption... options)
throws StreamException {
checkNotNull(options, "Missing request options");
try {
final URL url = buildFeedURL(baseURL, feed, "/following/");
return httpClient.execute(buildGet(url, key, token, options));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> unfollow(
Token token, FeedID source, FeedID target, RequestOption... options) throws StreamException {
checkNotNull(options, "Missing request options");
checkNotNull(target, "No target feed to unfollow");
try {
final URL url = buildFeedURL(baseURL, source, "/following/" + target + '/');
return httpClient.execute(buildDelete(url, key, token, options));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> getFollowStats(
Token token,
FeedID feed,
String[] followerSlugs,
String[] followingSlugs,
RequestOption... options)
throws StreamException {
try {
final URL url = followStatsPath(baseURL);
final List<CustomQueryParameter> params = new ArrayList<>(4);
final String feedId = String.join(":", feed.getSlug(), feed.getUserID());
params.add(new CustomQueryParameter("followers", feedId));
params.add(new CustomQueryParameter("following", feedId));
if (followerSlugs != null && followerSlugs.length > 0) {
params.add(new CustomQueryParameter("followers_slugs", String.join(",", followerSlugs)));
}
if (followingSlugs != null && followingSlugs.length > 0) {
params.add(new CustomQueryParameter("following_slugs", String.join(",", followingSlugs)));
}
return httpClient.execute(
buildGet(url, key, token, params.toArray(new CustomQueryParameter[0])));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> updateActivityToTargets(
Token token, FeedID feed, Activity activity, FeedID[] add, FeedID[] remove, FeedID[] replace)
throws StreamException {
checkNotNull(activity, "No activity to update");
checkNotNull(activity.getForeignID(), "Activity is required to have foreign ID attribute");
checkNotNull(activity.getTime(), "Activity is required to have time attribute");
checkNotNull(add, "No targets to add");
checkNotNull(remove, "No targets to remove");
checkNotNull(replace, "No targets to set");
boolean modification = replace.length == 0 && (add.length > 0 || remove.length > 0);
boolean replacement = replace.length > 0 && add.length == 0 && remove.length == 0;
checkArgument(
modification || replacement,
"Can't replace and modify activity to targets at the same time");
final String[] addedTargets =
J8Arrays.stream(add).map(id -> id.toString()).toArray(String[]::new);
final String[] removedTargets =
J8Arrays.stream(remove).map(id -> id.toString()).toArray(String[]::new);
final String[] newTargets =
J8Arrays.stream(replace).map(id -> id.toString()).toArray(String[]::new);
try {
final byte[] payload =
toJSON(
new Object() {
public String foreign_id = activity.getForeignID();
@JsonFormat(
shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS",
lenient = OptBoolean.FALSE,
timezone = "UTC")
public Date time = activity.getTime();
public String[] added_targets = addedTargets;
public String[] removed_targets = removedTargets;
public String[] new_targets = newTargets;
});
final URL url = buildToTargetUpdateURL(baseURL, feed);
return httpClient.execute(buildPost(url, key, token, payload));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> getUser(Token token, String id, boolean withFollowCounts)
throws StreamException {
checkNotNull(id, "Missing user ID");
checkArgument(!id.isEmpty(), "Missing user ID");
try {
final URL url = buildUsersURL(baseURL, id + '/');
return httpClient.execute(
buildGet(
url,
key,
token,
new CustomQueryParameter("with_follow_counts", Boolean.toString(withFollowCounts))));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> deleteUser(Token token, String id) throws StreamException {
checkNotNull(id, "Missing user ID");
checkArgument(!id.isEmpty(), "Missing user ID");
try {
final URL url = buildUsersURL(baseURL, id + '/');
return httpClient.execute(buildDelete(url, key, token));
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> createUser(
Token token, String userID, Data userData, boolean getOrCreate) throws StreamException {
checkNotNull(userID, "Missing user ID");
checkNotNull(userData, "Missing user data");
checkArgument(!userID.isEmpty(), "Missing user ID");
try {
final byte[] payload =
toJSON(
new Object() {
public String id = userID;
public Map<String, Object> data = userData.getData();
});
final URL url = buildUsersURL(baseURL);
return httpClient.execute(
buildPost(
url,
key,
token,
payload,
new CustomQueryParameter("get_or_create", Boolean.toString(getOrCreate))));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Response> updateUser(Token token, String userID, Data userData)
throws StreamException {
checkNotNull(userID, "Missing user ID");
checkNotNull(userData, "Missing user data");
checkArgument(!userID.isEmpty(), "Missing user ID");
try {
final byte[] payload =
toJSON(
new Object() {
public Map<String, Object> data = userData.getData();
});
final URL url = buildUsersURL(baseURL, userID + '/');
return httpClient.execute(buildPut(url, key, token, payload));
} catch (JsonProcessingException | MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Object> deleteActivities(Token token, BatchDeleteActivitiesRequest request) throws StreamException {
try {
final URL url = deleteActivitiesURL(baseURL);
final byte[] payload = toJSON(request);
io.getstream.core.http.Request httpRequest = buildPost(url, key, token, payload);
return httpClient.execute(httpRequest).thenApply(response -> null);
} catch (Exception e) {
throw new StreamException(e);
}
}
public CompletableFuture<Object> deleteReactions(Token token, BatchDeleteReactionsRequest request) throws StreamException {
try {
final URL url = deleteReactionsURL(baseURL);
final byte[] payload = toJSON(request);
io.getstream.core.http.Request httpRequest = buildPost(url, key, token, payload);
return httpClient.execute(httpRequest).thenApply(response -> null);
} catch (Exception e) {
throw new StreamException(e);
}
}
public CompletableFuture<ExportIDsResponse> exportUserActivities(Token token, String userId) throws StreamException {
if (userId == null || userId.isEmpty()) {
throw new IllegalArgumentException("User ID can't be null or empty");
}
try {
final URL url = buildExportIDsURL(baseURL, userId);
io.getstream.core.http.Request request = buildGet(url, key, token);
return httpClient
.execute(request)
.thenApply(
response -> {
try {
return deserialize(response, ExportIDsResponse.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<io.getstream.client.QueryAuditLogsResponse> queryAuditLogs(Token token, RequestOption... options) throws StreamException {
try {
final URL url = buildAuditLogsURL(baseURL);
return httpClient
.execute(buildGet(url, key, token, options))
.thenApply(
response -> {
try {
return deserialize(response, io.getstream.client.QueryAuditLogsResponse.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
}