This repository was archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathapi_manager.dart
More file actions
43 lines (37 loc) · 1.56 KB
/
api_manager.dart
File metadata and controls
43 lines (37 loc) · 1.56 KB
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
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:mentorship_client/failure.dart';
import 'package:mentorship_client/remote/services/auth_service.dart';
import 'package:mentorship_client/remote/services/comment_service.dart';
import 'package:mentorship_client/remote/services/relation_service.dart';
import 'package:mentorship_client/remote/services/task_service.dart';
import 'package:mentorship_client/remote/services/user_service.dart';
import 'package:mentorship_client/typedefs.dart';
/// Singleton class that gathers all services in one place.
class ApiManager {
static final instance = ApiManager._internal();
final AuthService authService = AuthService.create();
final UserService userService = UserService.create();
final RelationService relationService = RelationService.create();
final TaskService taskService = TaskService.create();
final CommentService commentService = CommentService.create();
ApiManager._internal();
/// Convenience method to reduce boilerplate. Invokes API function
/// and catches all possible errors.
static Future<T> callSafely<T>(ApiFunction apiFunction) async {
try {
final response = await apiFunction();
if (!response.isSuccessful) {
Logger.root.severe("Error: ${response.error}");
throw Failure.fromJson(response.error);
}
return response.body;
} on SocketException {
throw Failure("No internet connection");
} on HttpException {
throw Failure("HttpException");
} on Exception catch (e) {
throw Failure(e.toString());
}
}
}