Skip to content

Commit fa7360a

Browse files
author
PedRaM
committed
update some codes
1 parent 16a689e commit fa7360a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+831
-251
lines changed

Diff for: lib/core/utils/top_level_functions.dart

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
import 'dart:convert';
1+
// import 'dart:convert';
22

3-
import 'package:flutter/foundation.dart';
3+
// import 'package:flutter/foundation.dart';
44

5-
import '../error/exceptions.dart';
6-
import 'typedefs.dart';
5+
// import '../error/exceptions.dart';
6+
// import 'typedefs.dart';
77

8-
Future<T> callApi<T>({
9-
required ConvertToModel<T> converter,
10-
required Request request,
11-
bool isFile = false,
12-
}) async {
13-
final result = await request();
14-
// final fileResponse = isFile ? result : null;
15-
// print(result.body);
16-
// print(result.statusCode);
17-
final response = result.body.isEmpty ? null : json.decode(result.body);
18-
// if (result.statusCode == 401) throw UnAuhtorizeException();
19-
if (result.statusCode >= 200 && result.statusCode < 300) {
20-
// return converter(isFile ? fileResponse : response);
21-
return converter(response);
22-
}
23-
// final error = response == null ? null : ErrorModel.fromJson(response);
24-
// final message = error?.title;
25-
throw const ServerException(
26-
// message: message == null || message.isEmpty ? null : message,
27-
);
28-
}
8+
// Future<T> callApi<T>({
9+
// required ConvertToModel<T> converter,
10+
// required Request request,
11+
// bool isFile = false,
12+
// }) async {
13+
// final result = await request();
14+
// // final fileResponse = isFile ? result : null;
15+
// // print(result.body);
16+
// // print(result.statusCode);
17+
// final response = result.body.isEmpty ? null : json.decode(result.body);
18+
// // if (result.statusCode == 401) throw UnAuhtorizeException();
19+
// if (result.statusCode >= 200 && result.statusCode < 300) {
20+
// // return converter(isFile ? fileResponse : response);
21+
// return converter(response);
22+
// }
23+
// // final error = response == null ? null : ErrorModel.fromJson(response);
24+
// // final message = error?.title;
25+
// throw const ServerException(
26+
// // message: message == null || message.isEmpty ? null : message,
27+
// );
28+
// }
2929

30-
final isWebMobile = kIsWeb &&
31-
(defaultTargetPlatform == TargetPlatform.iOS ||
32-
defaultTargetPlatform == TargetPlatform.android);
30+
// final isWebMobile = kIsWeb &&
31+
// (defaultTargetPlatform == TargetPlatform.iOS ||
32+
// defaultTargetPlatform == TargetPlatform.android);

Diff for: lib/feature/api/manager/api_caller.dart

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
4+
import 'package:flutter/material.dart';
5+
import 'package:injectable/injectable.dart';
6+
7+
import '../../../core/error/exceptions.dart';
8+
import '../../../core/utils/typedefs.dart';
9+
10+
abstract class ApiCaller {
11+
Future<T> callApi<T>({
12+
required ConvertToModel<T> converter,
13+
required Request request,
14+
});
15+
}
16+
17+
@LazySingleton(as: ApiCaller)
18+
class ApiCallerImpl implements ApiCaller {
19+
@override
20+
Future<T> callApi<T>({
21+
required ConvertToModel<T> converter,
22+
required Request request,
23+
}) async {
24+
final result = await request();
25+
String logString = result.request?.url.toString() ?? 'null';
26+
logString = '$logString\nStatusCode: ${result.statusCode.toString()}';
27+
final body = utf8.decode(result.bodyBytes);
28+
if (result.statusCode >= 200 && result.statusCode < 300) {
29+
logString = '$logString\n${body.isEmpty ? 'empty body' : body}';
30+
debugPrint(
31+
'--------------------- Start ---------------------\n'
32+
'$logString\n'
33+
'--------------------- End ---------------------',
34+
);
35+
dynamic decodedBody;
36+
try {
37+
decodedBody = json.decode(body);
38+
} on FormatException {
39+
decodedBody = body;
40+
}
41+
final decodedResult = body.isEmpty ? null : decodedBody;
42+
final bodyResult = converter(decodedResult);
43+
return bodyResult;
44+
}
45+
logString = '$logString \n $body';
46+
debugPrint(
47+
'--------------------- Start ---------------------\n'
48+
'$logString\n'
49+
'--------------------- End ---------------------',
50+
);
51+
// if (result.statusCode == 401) throw const UnAuthorizeException();
52+
final errorMessage = handleError(body);
53+
throw ServerException(message: errorMessage);
54+
}
55+
56+
String? handleError(String body) {
57+
return null;
58+
// if (body.isEmpty) return null;
59+
// dynamic decodedBody;
60+
// try {
61+
// decodedBody = json.decode(body);
62+
// final item = ErrorModel.fromJson(decodedBody);
63+
// return item.message.isEmpty ? null : item.message;
64+
// } on Exception {
65+
// decodedBody = body;
66+
// }
67+
// return decodedBody?.toString();
68+
}
69+
}

Diff for: lib/feature/api/manager/my_client.dart

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:convert';
2+
import 'package:curl_generator/curl_generator.dart';
3+
import 'package:flutter/material.dart';
4+
import 'package:http/http.dart';
5+
import 'package:injectable/injectable.dart';
6+
7+
@lazySingleton
8+
class MyClient extends BaseClient {
9+
final Client delegate;
10+
11+
MyClient(this.delegate);
12+
13+
@override
14+
Future<StreamedResponse> send(BaseRequest request) {
15+
Map<String, dynamic> body = {};
16+
if (request is Request && request.body.isNotEmpty) {
17+
body = json.decode(request.body);
18+
}
19+
final mLog = Curl.curlOf(
20+
url: request.url.toString(),
21+
body: body,
22+
method: request.method,
23+
header: request.headers,
24+
);
25+
debugPrint(
26+
'--------------------- Start Curl ---------------------\n'
27+
'$mLog\n'
28+
'--------------------- End Curl ---------------------',
29+
);
30+
return delegate.send(request);
31+
}
32+
33+
@override
34+
void close() {
35+
delegate.close();
36+
super.close();
37+
}
38+
}

Diff for: lib/feature/home/data/data_source/home_data_source.dart

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import 'dart:io';
33
import 'package:file_picker/file_picker.dart';
44
import 'package:file_saver/file_saver.dart';
55
import 'package:flutter/foundation.dart';
6+
import 'package:injectable/injectable.dart';
67

7-
import '../../../../core/utils/top_level_functions.dart';
88
import '../../../../json_reader.dart';
9+
import '../../../api/manager/api_caller.dart';
910
import '../../domain/entity/education.dart';
1011
import '../../domain/entity/info.dart';
1112
import '../../domain/entity/job_experience.dart';
@@ -65,23 +66,28 @@ abstract class HomeDataSource {
6566
Future<void> requestForExport(Uint8List image);
6667
}
6768

69+
@LazySingleton(as: HomeDataSource)
6870
class HomeDataSourceImpl implements HomeDataSource {
6971
// final http.Client client;
7072
// final firebase_storage.FirebaseStorage storage;
7173
final FilePicker picker;
7274
final FileSaver fileSaver;
75+
final ApiCaller apiCaller;
76+
// final MyClient client;
7377
const HomeDataSourceImpl({
7478
// required this.client,
7579
// required this.storage,
7680
required this.picker,
7781
required this.fileSaver,
82+
required this.apiCaller,
83+
// required this.client,
7884
});
7985

8086
@override
8187
Future<List<Education>> getEducations(String language) async {
8288
// final educations = storage.ref('api/$language/education.json');
8389
// final url = await educations.getDownloadURL();
84-
return callApi<List<Education>>(
90+
return apiCaller.callApi<List<Education>>(
8591
converter: (json) =>
8692
(json as List).map((e) => EducationModel.fromJson(e)).toList(),
8793
request: () => readApi(
@@ -97,7 +103,7 @@ class HomeDataSourceImpl implements HomeDataSource {
97103
Future<Info> getInfo(String language) async {
98104
// final educations = storage.ref('api/$language/info.json');
99105
// final url = await educations.getDownloadURL();
100-
return callApi<Info>(
106+
return apiCaller.callApi<Info>(
101107
converter: (json) => InfoModel.fromJson(json),
102108
request: () => readApi(
103109
'info',
@@ -112,7 +118,7 @@ class HomeDataSourceImpl implements HomeDataSource {
112118
Future<List<JobExperience>> getJobExperiences(String language) async {
113119
// final educations = storage.ref('api/$language/job_experience.json');
114120
// final url = await educations.getDownloadURL();
115-
return callApi<List<JobExperience>>(
121+
return apiCaller.callApi<List<JobExperience>>(
116122
converter: (json) =>
117123
(json as List).map((e) => JobExperienceModel.fromJson(e)).toList(),
118124
request: () => readApi(
@@ -128,7 +134,7 @@ class HomeDataSourceImpl implements HomeDataSource {
128134
Future<Profession> getProfession(String language) async {
129135
// final educations = storage.ref('api/$language/profession.json');
130136
// final url = await educations.getDownloadURL();
131-
return callApi<Profession>(
137+
return apiCaller.callApi<Profession>(
132138
converter: (json) => ProfessionModel.fromJson(json),
133139
request: () => readApi(
134140
'profession',
@@ -143,7 +149,7 @@ class HomeDataSourceImpl implements HomeDataSource {
143149
Future<List<Project>> getProjects(String language) async {
144150
// final educations = storage.ref('api/$language/project.json');
145151
// final url = await educations.getDownloadURL();
146-
return callApi<List<Project>>(
152+
return apiCaller.callApi<List<Project>>(
147153
converter: (json) =>
148154
(json as List).map((e) => ProjectModel.fromJson(e)).toList(),
149155
request: () => readApi(
@@ -159,7 +165,7 @@ class HomeDataSourceImpl implements HomeDataSource {
159165
Future<List<Skill>> getSkills(String language) async {
160166
// final educations = storage.ref('api/$language/skill.json');
161167
// final url = await educations.getDownloadURL();
162-
return callApi<List<Skill>>(
168+
return apiCaller.callApi<List<Skill>>(
163169
converter: (json) =>
164170
(json as List).map((e) => SkillModel.fromJson(e)).toList(),
165171
request: () => readApi(
@@ -175,7 +181,7 @@ class HomeDataSourceImpl implements HomeDataSource {
175181
Future<List<Software>> getSoftwares(String language) async {
176182
// final educations = storage.ref('api/$language/software.json');
177183
// final url = await educations.getDownloadURL();
178-
return callApi<List<Software>>(
184+
return apiCaller.callApi<List<Software>>(
179185
converter: (json) =>
180186
(json as List).map((e) => SoftwareModel.fromJson(e)).toList(),
181187
request: () => readApi(

0 commit comments

Comments
 (0)