Line data Source code
1 : import 'dart:convert';
2 :
3 : import 'package:dio/dio.dart';
4 : import 'package:stream_feed_dart/src/core/http/stream_http_client.dart';
5 : import 'package:stream_feed_dart/src/core/http/token.dart';
6 : import 'package:stream_feed_dart/src/core/models/activity.dart';
7 : import 'package:stream_feed_dart/src/core/models/enriched_activity.dart';
8 : import 'package:stream_feed_dart/src/core/models/feed_id.dart';
9 : import 'package:stream_feed_dart/src/core/models/follow.dart';
10 : import 'package:stream_feed_dart/src/core/models/foreign_id_time_pair.dart';
11 : import 'package:stream_feed_dart/src/core/util/extension.dart';
12 : import 'package:stream_feed_dart/src/core/util/routes.dart';
13 :
14 : class BatchApi {
15 2 : const BatchApi(this.client);
16 :
17 : final StreamHttpClient client;
18 :
19 0 : Future<Response> addToMany(
20 : Token token, Activity activity, Iterable<FeedId> feedIds) async {
21 0 : checkArgument(feedIds.isNotEmpty, 'No feeds to add to');
22 0 : return client.post(
23 0 : Routes.addToManyUrl,
24 0 : headers: {'Authorization': '$token'},
25 0 : data: json.encode({
26 0 : 'feeds': feedIds.map((e) => e.toString()).toList(),
27 : 'activity': activity,
28 : }),
29 : );
30 : }
31 :
32 0 : Future<Response> followMany(
33 : Token token, int activityCopyLimit, Iterable<Follow> follows) async {
34 0 : checkArgument(
35 0 : activityCopyLimit >= 0, 'Activity copy limit must be non negative');
36 :
37 0 : checkArgument(follows.isNotEmpty, 'No feeds to follow');
38 0 : return client.post(
39 0 : Routes.followManyUrl,
40 0 : headers: {'Authorization': '$token'},
41 0 : queryParameters: {'activity_copy_limit': activityCopyLimit},
42 : data: follows,
43 : );
44 : }
45 :
46 0 : Future<Response> unfollowMany(
47 : Token token, Iterable<UnFollow> unfollows) async {
48 0 : checkArgument(unfollows.isNotEmpty, 'No feeds to unfollow');
49 0 : return client.post(
50 0 : Routes.unfollowManyUrl,
51 0 : headers: {'Authorization': '$token'},
52 : data: unfollows,
53 : );
54 : }
55 :
56 0 : Future<List<Activity>> getActivitiesById(
57 : Token token, Iterable<String> ids) async {
58 0 : checkArgument(ids.isNotEmpty, 'No activities to get');
59 0 : final result = await client.get<Map>(
60 0 : Routes.activitesUrl,
61 0 : headers: {'Authorization': '$token'},
62 0 : queryParameters: {'ids': ids.join(',')},
63 : );
64 0 : final data = (result.data!['results'] as List)
65 0 : .map((e) => Activity.fromJson(e))
66 0 : .toList(growable: false);
67 : return data;
68 : }
69 :
70 0 : Future<List<Activity>> getActivitiesByForeignId(
71 : Token token, Iterable<ForeignIdTimePair> pairs) async {
72 0 : checkArgument(pairs.isNotEmpty, 'No activities to get');
73 0 : final result = await client.get(
74 0 : Routes.activitesUrl,
75 0 : headers: {'Authorization': '$token'},
76 0 : queryParameters: {
77 0 : 'foreign_ids': pairs.map((it) => it.foreignID).join(','),
78 : 'timestamps':
79 0 : pairs.map((it) => it.time.toUtc().toIso8601String()).join(','),
80 : },
81 : );
82 0 : final data = (result.data['results'] as List)
83 0 : .map((e) => Activity.fromJson(e))
84 0 : .toList(growable: false);
85 : return data;
86 : }
87 :
88 0 : Future<List<EnrichedActivity>> getEnrichedActivitiesById(
89 : Token token, Iterable<String> ids) async {
90 0 : checkArgument(ids.isNotEmpty, 'No activities to get');
91 0 : final result = await client.get(
92 0 : Routes.enrichedActivitiesUrl,
93 0 : headers: {'Authorization': '$token'},
94 0 : queryParameters: {'ids': ids.join(',')},
95 : );
96 0 : final data = (result.data['results'] as List)
97 0 : .map((e) => EnrichedActivity.fromJson(e))
98 0 : .toList(growable: false);
99 : return data;
100 : }
101 :
102 0 : Future<List<EnrichedActivity>> getEnrichedActivitiesByForeignId(
103 : Token token, Iterable<ForeignIdTimePair> pairs) async {
104 0 : checkArgument(pairs.isNotEmpty, 'No activities to get');
105 0 : final result = await client.get(
106 0 : Routes.enrichedActivitiesUrl,
107 0 : headers: {'Authorization': '$token'},
108 0 : queryParameters: {
109 0 : 'foreign_ids': pairs.map((it) => it.foreignID).join(','),
110 : 'timestamps':
111 0 : pairs.map((it) => it.time.toUtc().toIso8601String()).join(','),
112 : },
113 : );
114 0 : final data = (result.data['results'] as List)
115 0 : .map((e) => EnrichedActivity.fromJson(e))
116 0 : .toList(growable: false);
117 : return data;
118 : }
119 :
120 0 : Future<Response> updateActivities(
121 : Token token, Iterable<Activity> activities) async {
122 0 : checkArgument(activities.isNotEmpty, 'No activities to update');
123 0 : return client.post(
124 0 : Routes.activitesUrl,
125 0 : headers: {'Authorization': '$token'},
126 0 : data: {'activities': activities},
127 : );
128 : }
129 : }
|