LCOV - code coverage report
Current view: top level - core/api - reactions_api.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 64 66 97.0 %
Date: 2021-04-14 08:03:52 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:dio/dio.dart';
       2             : import 'package:stream_feed_dart/src/core/http/stream_http_client.dart';
       3             : import 'package:stream_feed_dart/src/core/http/token.dart';
       4             : import 'package:stream_feed_dart/src/core/lookup_attribute.dart';
       5             : import 'package:stream_feed_dart/src/core/models/filter.dart';
       6             : import 'package:stream_feed_dart/src/core/models/paginated.dart';
       7             : import 'package:stream_feed_dart/src/core/models/reaction.dart';
       8             : import 'package:stream_feed_dart/src/core/util/extension.dart';
       9             : import 'package:stream_feed_dart/src/core/util/routes.dart';
      10             : 
      11             : class ReactionsApi {
      12           3 :   const ReactionsApi(this.client);
      13             : 
      14             :   final StreamHttpClient client;
      15             : 
      16           1 :   Future<Reaction> add(Token token, Reaction reaction) async {
      17           2 :     checkArgument(reaction.activityId != null || reaction.parent != null,
      18             :         'Reaction has to either have an activity ID or parent');
      19           3 :     checkArgument(reaction.activityId == null || reaction.parent == null,
      20             :         "Reaction can't have both activity ID and parent");
      21           1 :     if (reaction.activityId != null) {
      22           3 :       checkArgument(reaction.activityId!.isNotEmpty,
      23             :           "Reaction activity ID can't be empty");
      24             :     }
      25           1 :     if (reaction.parent != null) {
      26           0 :       checkArgument(
      27           0 :           reaction.parent!.isNotEmpty, "Reaction parent can't be empty");
      28             :     }
      29           2 :     checkNotNull(reaction.kind, "Reaction kind can't be null");
      30           3 :     checkArgument(reaction.kind!.isNotEmpty, "Reaction kind can't be empty");
      31           3 :     final result = await client.post<Map>(
      32           1 :       Routes.buildReactionsUrl(),
      33           2 :       headers: {'Authorization': '$token'},
      34             :       data: reaction,
      35             :     );
      36           2 :     return Reaction.fromJson(result.data as Map<String, dynamic>);
      37             :   }
      38             : 
      39           1 :   Future<Reaction> get(Token token, String id) async {
      40           2 :     checkArgument(id.isNotEmpty, "Reaction id can't be empty");
      41           3 :     final result = await client.get<Map>(
      42           2 :       Routes.buildReactionsUrl('$id/'),
      43           2 :       headers: {'Authorization': '$token'},
      44             :     );
      45           2 :     return Reaction.fromJson(result.data as Map<String, dynamic>);
      46             :   }
      47             : 
      48           1 :   Future<Response> delete(Token token, String id) async {
      49           2 :     checkArgument(id.isNotEmpty, "Reaction id can't be empty");
      50           2 :     return client.delete(
      51           2 :       Routes.buildReactionsUrl('$id/'),
      52           2 :       headers: {'Authorization': '$token'},
      53             :     );
      54             :   }
      55             : 
      56           1 :   Future<List<Reaction>> filter(
      57             :     Token token,
      58             :     LookupAttribute lookupAttr,
      59             :     String lookupValue,
      60             :     Filter filter,
      61             :     int limit,
      62             :     String kind,
      63             :   ) async {
      64           2 :     checkArgument(lookupValue.isNotEmpty, "Lookup value can't be empty");
      65           3 :     final result = await client.get<Map>(
      66           3 :       Routes.buildReactionsUrl('${lookupAttr.attr}/$lookupValue/$kind'),
      67           2 :       headers: {'Authorization': '$token'},
      68           1 :       queryParameters: {
      69           2 :         'limit': limit.toString(),
      70           1 :         ...filter.params,
      71           2 :         'with_activity_data': lookupAttr == LookupAttribute.activityId,
      72             :       },
      73             :     );
      74           2 :     final data = (result.data!['results'] as List)
      75           3 :         .map((e) => Reaction.fromJson(e))
      76           1 :         .toList(growable: false);
      77             :     return data;
      78             :   }
      79             : 
      80           1 :   Future<PaginatedReactions> paginatedFilter(
      81             :     Token token,
      82             :     LookupAttribute lookupAttr,
      83             :     String lookupValue,
      84             :     Filter filter,
      85             :     int limit,
      86             :     String kind,
      87             :   ) async {
      88           2 :     checkArgument(lookupValue.isNotEmpty, "Lookup value can't be empty");
      89             : 
      90           3 :     final result = await client.get(
      91           3 :       Routes.buildReactionsUrl('${lookupAttr.attr}/$lookupValue/$kind'),
      92           2 :       headers: {'Authorization': '$token'},
      93           1 :       queryParameters: {
      94           2 :         'limit': limit.toString(),
      95           1 :         ...filter.params,
      96           2 :         'with_activity_data': lookupAttr == LookupAttribute.activityId,
      97             :       },
      98             :     );
      99           2 :     return PaginatedReactions.fromJson(result.data);
     100             :   }
     101             : 
     102           1 :   Future<PaginatedReactions> nextPaginatedFilter(
     103             :       Token token, String next) async {
     104           2 :     checkArgument(next.isNotEmpty, "Next url can't be empty");
     105           3 :     final result = await client.get(
     106             :       next,
     107           2 :       headers: {'Authorization': '$token'},
     108             :     );
     109           2 :     return PaginatedReactions.fromJson(result.data);
     110             :   }
     111             : 
     112           1 :   Future<Response> update(Token token, Reaction updatedReaction) async {
     113           3 :     checkArgument(updatedReaction.id!.isNotEmpty, "Reaction id can't be empty");
     114           1 :     final targetFeedIds = updatedReaction.targetFeeds!
     115           3 :         .map((e) => e.toString())
     116           1 :         .toList(growable: false);
     117           1 :     final reactionId = updatedReaction.id;
     118           1 :     final data = updatedReaction.data;
     119           2 :     return client.put(
     120           2 :       Routes.buildReactionsUrl('$reactionId/'),
     121           2 :       headers: {'Authorization': '$token'},
     122           1 :       data: {
     123           2 :         if (data != null && data.isNotEmpty) 'data': data,
     124           2 :         if (targetFeedIds.isNotEmpty) 'target_feeds': targetFeedIds,
     125             :       },
     126             :     );
     127             :   }
     128             : }

Generated by: LCOV version 1.15