Files

54 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:dio/dio.dart';
import '../../../../data/network/i_api_client.dart';
import '../../../contacts/data/models/user_profile.dart';
class UserProfileService {
static const _prefix = '/api/v1/users';
final IApiClient _client;
UserProfileService(this._client);
Future<UserProfile> getMe() async {
final response = await _client.get<Map<String, dynamic>>('$_prefix/me');
final data = response.data;
if (data == null) {
throw StateError('Invalid getMe response: empty payload');
}
return UserProfile.fromJson(data);
}
Future<UserProfile> updateMe(UserUpdateRequest request) async {
final response = await _client.patch<Map<String, dynamic>>(
'$_prefix/me',
data: request.toJson(),
);
final data = response.data;
if (data == null) {
throw StateError('Invalid updateMe response: empty payload');
}
return UserProfile.fromJson(data);
}
Future<String> uploadAvatar(File file) async {
final formData = FormData.fromMap({
'file': await MultipartFile.fromFile(
file.path,
filename: file.path.split('/').last,
),
});
final response = await _client.post<Map<String, dynamic>>(
'$_prefix/me/avatar',
data: formData,
);
final data = response.data;
if (data == null || data['url'] is! String) {
throw StateError('Invalid uploadAvatar response: missing url');
}
return data['url'] as String;
}
}