Line data Source code
1 : import 'package:amadeus_proto/api/models/profile.dart';
2 : import 'package:amadeus_proto/api/utils/api_base.dart';
3 : import 'package:amadeus_proto/api/utils/api_error.dart';
4 : import 'package:either_dart/either.dart';
5 :
6 : extension ProfileRoutes on ApiBase {
7 : static const String _profileRoutesPrefix = "/profile";
8 :
9 2 : Future<Either<ApiError, Profile>> getMyProfile() {
10 6 : if (cache.get<Profile>("profile").shouldReload) {
11 8 : return get("$_profileRoutesPrefix/me", (json) => Profile.fromJson(json)).mapRight(
12 2 : (profile) {
13 6 : cache.get<Profile>("profile").data = profile;
14 : return profile;
15 : },
16 : );
17 : } else {
18 1 : return Future.value(
19 4 : Right(cache.get<Profile>("profile").data),
20 : );
21 : }
22 : }
23 :
24 1 : Future<Either<ApiError, Profile>> updateProfile(Map<String, dynamic> data) {
25 3 : cache.get<Profile>("profile").markAsShouldReload();
26 3 : return patch("$_profileRoutesPrefix/me", (json) => Profile.fromJson(json),
27 : requestBody: data);
28 : }
29 :
30 1 : Future<Either<ApiError, Profile>> getProfile(String id) =>
31 4 : get("$_profileRoutesPrefix/$id", (json) => Profile.fromJson(json));
32 :
33 2 : Future<Either<ApiError, List<Profile>>> searchProfiles(String query) => get(
34 1 : "$_profileRoutesPrefix?username=$query",
35 1 : (json) {
36 1 : if (json is List) {
37 4 : return json.map((e) => Profile.fromJson(e)).toList();
38 : } else {
39 0 : throw Exception("Invalid response format");
40 : }
41 : },
42 : );
43 : }
|