Line data Source code
1 : import 'package:stream_feed_dart/src/core/http/stream_http_client.dart'; 2 : import 'package:stream_feed_dart/src/core/http/token.dart'; 3 : import 'package:stream_feed_dart/src/core/models/user.dart'; 4 : import 'package:stream_feed_dart/src/core/util/extension.dart'; 5 : import 'package:stream_feed_dart/src/core/util/routes.dart'; 6 : 7 : class UsersApi { 8 3 : const UsersApi(this.client); 9 : 10 : final StreamHttpClient client; 11 : 12 1 : Future<User> add( 13 : Token token, 14 : String id, 15 : Map<String, Object> data, [ 16 : bool getOrCreate = false, 17 : ]) async { 18 2 : checkArgument(id.isNotEmpty, 'Missing user ID'); 19 1 : final user = User(id: id, data: data); 20 3 : final result = await client.post<Map>( 21 1 : Routes.buildUsersUrl(), 22 2 : headers: {'Authorization': '$token'}, 23 1 : queryParameters: {'get_or_create': getOrCreate}, 24 : data: user, 25 : ); 26 2 : return User.fromJson(result.data as Map<String, dynamic>); 27 : } 28 : 29 1 : Future<User> get(Token token, String id, 30 : [bool withFollowCounts = true]) async { 31 2 : checkArgument(id.isNotEmpty, 'Missing user ID'); 32 3 : final result = await client.get( 33 2 : Routes.buildUsersUrl('$id/'), 34 2 : headers: {'Authorization': '$token'}, 35 1 : queryParameters: {'with_follow_counts': withFollowCounts}, 36 : ); 37 2 : return User.fromJson(result.data); 38 : } 39 : 40 1 : Future<User> update(Token token, String id, Map<String, Object> data) async { 41 2 : checkArgument(id.isNotEmpty, 'Missing user ID'); 42 1 : final updatedUser = User(id: id, data: data); 43 3 : final result = await client.put( 44 2 : Routes.buildUsersUrl('$id/'), 45 2 : headers: {'Authorization': '$token'}, 46 : data: updatedUser, 47 : ); 48 2 : return User.fromJson(result.data); 49 : } 50 : 51 1 : Future<void> delete(Token token, String id) { 52 2 : checkArgument(id.isNotEmpty, 'Missing user ID'); 53 2 : return client.delete( 54 2 : Routes.buildUsersUrl('$id/'), 55 2 : headers: {'Authorization': '$token'}, 56 : ); 57 : } 58 : }