-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
home_remote_datasource.dart
83 lines (74 loc) 路 2.32 KB
/
home_remote_datasource.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:injectable/injectable.dart';
import '../../../../core/error/exceptions.dart' as excptn;
import '../../../../core/util/gql_query.dart';
import '../models/character_model.dart';
import '../models/episode_model.dart';
import '../models/location_model.dart';
abstract class IHomeRemoteDataSource {
Future<List<CharacterModel>> getCharacters(int page);
Future<List<LocationModel>> getLocations(int page);
Future<List<EpisodeModel>> getEpisodes(int page);
}
@LazySingleton(as: IHomeRemoteDataSource)
class HomeRemoteDataSource implements IHomeRemoteDataSource {
HomeRemoteDataSource(this._client);
final GraphQLClient _client;
@override
Future<List<CharacterModel>> getCharacters(int page) async {
try {
final result = await _client.query(QueryOptions(
document: gql(GqlQuery.charactersQuery),
variables: {"page": page},
));
if (result.data == null) {
return [];
}
return result.data['characters']['results']
.map((e) => CharacterModel.fromJson(e))
.cast<CharacterModel>()
.toList();
} on Exception catch (exception) {
print(exception);
throw excptn.ServerException();
}
}
@override
Future<List<EpisodeModel>> getEpisodes(int page) async {
try {
final result = await _client.query(QueryOptions(
document: gql(GqlQuery.episodesQuery),
variables: {"page": page},
));
if (result.data == null) {
return [];
}
return result.data['episodes']['results']
.map((e) => EpisodeModel.fromJson(e))
.cast<EpisodeModel>()
.toList();
} on Exception catch (exception) {
print(exception);
throw excptn.ServerException();
}
}
@override
Future<List<LocationModel>> getLocations(int page) async {
try {
final result = await _client.query(QueryOptions(
document: gql(GqlQuery.locationsQuery),
variables: {"page": page},
));
if (result.data == null) {
return [];
}
return result.data['locations']['results']
.map((e) => LocationModel.fromJson(e))
.cast<LocationModel>()
.toList();
} on Exception catch (exception) {
print(exception);
throw excptn.ServerException();
}
}
}