-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthentication.dart
More file actions
197 lines (178 loc) · 5.46 KB
/
authentication.dart
File metadata and controls
197 lines (178 loc) · 5.46 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:piwigo_ng/domain/models/info_model.dart';
import 'package:piwigo_ng/domain/models/status_model.dart';
import 'package:piwigo_ng/data/services/api/api_error.dart';
import 'package:piwigo_ng/data/services/api/upload.dart';
import 'package:piwigo_ng/data/services/local/preferences_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'api_client.dart';
Future<ApiResponse<String>> pingAPI() async {
Map<String, String> queries = {
'format': 'json',
'method': 'pwg.getVersion',
};
try {
Response response = await ApiClient.get(queryParameters: queries);
var data = json.decode(response.data);
if (data['stat'] == 'ok') {
return ApiResponse<String>(data: data['result']);
}
} on DioException catch (e) {
debugPrint(e.message);
} catch (e) {
debugPrint('Error $e');
}
return ApiResponse(error: ApiErrors.error);
}
Future<ApiResponse<bool>> loginUser(
String url, {
String username = '',
String password = '',
}) async {
if (url.isEmpty) {
return ApiResponse<bool>(
data: false,
error: ApiErrors.wrongServerUrl,
);
}
ApiClient.cookieJar.deleteAll();
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(Preferences.serverUrlKey, url);
if (username.isEmpty && password.isEmpty) {
ApiResponse<StatusModel> status = await sessionStatus();
if (!status.hasError && status.hasData) {
Preferences.saveId(status.data!, username: username, password: password);
return ApiResponse<bool>(
data: true,
);
}
askMediaPermission();
return ApiResponse<bool>(
data: false,
// error: ApiErrors.wrongServerUrl,
);
}
Map<String, String> queries = {
'format': 'json',
'method': 'pwg.session.login',
};
Map<String, String> fields = {
'username': username,
'password': password,
};
try {
Response response = await ApiClient.post(
data: FormData.fromMap(fields),
options: Options(contentType: Headers.formUrlEncodedContentType),
queryParameters: queries,
);
if (response.statusCode == 200) {
var data = json.decode(response.data);
if (data['stat'] == 'fail') {
return ApiResponse<bool>(
data: false,
error: ApiErrors.wrongLoginId,
);
}
ApiResponse<StatusModel> status = await sessionStatus();
if (status.hasData) {
Preferences.saveId(status.data!, username: username, password: password);
}
askMediaPermission();
return ApiResponse<bool>(
data: true,
);
}
} on DioException catch (e) {
debugPrint(e.message);
} catch (e) {
debugPrint('Error $e');
}
return ApiResponse<bool>(
data: false,
// error: ApiErrors.wrongServerUrl,
);
}
Future<ApiResponse<StatusModel>> sessionStatus() async {
Map<String, String> queries = {'format': 'json', 'method': 'pwg.session.getStatus'};
try {
Response response = await ApiClient.get(queryParameters: queries);
var data = json.decode(response.data);
if (data['stat'] == 'ok') {
if (await methodExist('community.session.getStatus')) {
String? community = await communityStatus();
data['result']['real_user_status'] = community;
}
return ApiResponse<StatusModel>(
data: StatusModel.fromJson(data['result']),
);
}
} on DioException catch (e) {
debugPrint(e.message);
} catch (e) {
debugPrint('Session Status Error: $e');
if (e is Error) {
debugPrint('${e.stackTrace}');
}
}
return ApiResponse(
error: ApiErrors.getStatusError,
);
}
Future<String?> communityStatus() async {
Map<String, String> queries = {'format': 'json', 'method': 'community.session.getStatus'};
try {
Response response = await ApiClient.get(queryParameters: queries);
var data = json.decode(response.data);
if (data['stat'] == 'ok') {
return data['result']['real_user_status'];
}
} on DioException catch (e) {
debugPrint(e.message);
} catch (e) {
debugPrint('Error $e');
}
return null;
}
Future<ApiResponse<InfoModel>> getInfo() async {
Map<String, String> queries = {'format': 'json', 'method': 'pwg.getInfos'};
try {
Response response = await ApiClient.get(queryParameters: queries);
var data = json.decode(response.data);
if (data['stat'] == 'ok') {
return ApiResponse<InfoModel>(
data: InfoModel.fromJson(data['result']),
);
}
} on DioException catch (e) {
debugPrint(e.message);
} catch (e) {
debugPrint('Error $e');
}
return ApiResponse(
error: ApiErrors.getInfoError,
);
}
Future<ApiResponse<List<String>>> getMethods() async {
Map<String, String> queries = {'format': 'json', 'method': 'reflection.getMethodList'};
try {
Response response = await ApiClient.get(queryParameters: queries);
Map<String, dynamic> data = json.decode(response.data);
final List<String> methods = data['result']['methods'].map<String>((e) => e.toString()).toList();
return ApiResponse<List<String>>(data: methods);
} on DioException catch (e) {
debugPrint(e.message);
} catch (e) {
debugPrint('Error $e');
}
return ApiResponse<List<String>>(error: ApiErrors.getMethodsError);
}
Future<bool> methodExist(String method) async {
var result = await getMethods();
if (result.hasData) {
return result.data!.contains(method);
}
return false;
}