|
|
|
@ -19,85 +19,105 @@ class RequestApi implements IDataStrategy {
|
|
|
|
|
var request = http.Request('GET', url);
|
|
|
|
|
request.headers.addAll(<String, String>{'Authorization': token});
|
|
|
|
|
|
|
|
|
|
var streamedResponse = await request.send();
|
|
|
|
|
final response = await http.Response.fromStream(streamedResponse);
|
|
|
|
|
// !! Crée un fichier comme ca avec les bytes !!
|
|
|
|
|
//File("//").writeAsBytes(response.bodyBytes);
|
|
|
|
|
try {
|
|
|
|
|
var streamedResponse = await request.send();
|
|
|
|
|
final response = await http.Response.fromStream(streamedResponse);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return Tuple2(true, response.bodyBytes);
|
|
|
|
|
}
|
|
|
|
|
if ((response.statusCode == 401)) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if ((response.statusCode == 404)) {
|
|
|
|
|
return const Tuple2(false, "404 - NOT FOUND");
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return Tuple2(true, response.bodyBytes);
|
|
|
|
|
}
|
|
|
|
|
if ((response.statusCode == 401)) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if ((response.statusCode == 404)) {
|
|
|
|
|
return const Tuple2(false, "404 - NOT FOUND");
|
|
|
|
|
}
|
|
|
|
|
// When Network Off
|
|
|
|
|
} on SocketException {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Tuple2<bool, String>> deleteFile(String token, String fileUuid) async {
|
|
|
|
|
final response = await http.delete(
|
|
|
|
|
Uri.parse('$urlApi/user/files/$fileUuid'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
Future<bool> deleteFile(String token, String fileUuid) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await http.delete(
|
|
|
|
|
Uri.parse('$urlApi/user/files/$fileUuid'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2(true, "Successful");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 404) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 404) {
|
|
|
|
|
return const Tuple2(false, "404 - NOT FOUND");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Tuple2<bool, String>> deleteUser(String token) async {
|
|
|
|
|
final response = await http.delete(Uri.parse('$urlApi/user'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2<bool, String>(true, "Successful");
|
|
|
|
|
} else if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2<bool, String>(
|
|
|
|
|
false, "401 UNAUTHORIZED - Mauvais ou pas de token");
|
|
|
|
|
} else if (response.statusCode == 404) {
|
|
|
|
|
return const Tuple2<bool, String>(
|
|
|
|
|
false, "404 NOT FOUND - Pas de compte lié");
|
|
|
|
|
try {
|
|
|
|
|
final response = await http.delete(Uri.parse('$urlApi/user'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2<bool, String>(true, "Successful");
|
|
|
|
|
} else if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2<bool, String>(
|
|
|
|
|
false, "401 UNAUTHORIZED - Mauvais ou pas de token");
|
|
|
|
|
} else if (response.statusCode == 404) {
|
|
|
|
|
return const Tuple2<bool, String>(
|
|
|
|
|
false, "404 NOT FOUND - Pas de compte lié");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2<bool, String>(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2<bool, String>(false, "Fail");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Tuple2> getFiles(String token) async {
|
|
|
|
|
final response = await http.get(Uri.parse('$urlApi/user/files'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
try {
|
|
|
|
|
final response = await http.get(Uri.parse('$urlApi/user/files'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return Tuple2(true,
|
|
|
|
|
(json.decode(response.body) as List).cast<Map<String, dynamic>>());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return Tuple2(true,
|
|
|
|
|
(json.decode(response.body) as List).cast<Map<String, dynamic>>());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Tuple2<bool, String>> connexion(String email, String hash) async {
|
|
|
|
|
final response =
|
|
|
|
|
await http.get(Uri.parse('$urlApi/user/login/$email/$hash'));
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return Tuple2<bool, String>(true, json['token'].toString());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2<bool, String>(false, "UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 404) {
|
|
|
|
|
return const Tuple2<bool, String>(false, "Not found the email");
|
|
|
|
|
try {
|
|
|
|
|
final response =
|
|
|
|
|
await http.get(Uri.parse('$urlApi/user/login/$email/$hash'));
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return Tuple2<bool, String>(true, json['token'].toString());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2<bool, String>(false, "UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 404) {
|
|
|
|
|
return const Tuple2<bool, String>(false, "Not found the email");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
}
|
|
|
|
@ -107,19 +127,23 @@ class RequestApi implements IDataStrategy {
|
|
|
|
|
String email, String hash, String username) async {
|
|
|
|
|
var body = {"email": email, "hash": hash, "username": username};
|
|
|
|
|
var header = {"Content-Type": "application/json"};
|
|
|
|
|
final response = await http.post(Uri.parse('$urlApi/user'),
|
|
|
|
|
headers: header, body: jsonEncode(body));
|
|
|
|
|
try {
|
|
|
|
|
final response = await http.post(Uri.parse('$urlApi/user'),
|
|
|
|
|
headers: header, body: jsonEncode(body));
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return Tuple2(true, json['token'].toString());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 BAD REQUEST - Json mal formaté");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 409) {
|
|
|
|
|
return const Tuple2(
|
|
|
|
|
false, "409 CONFLICT - Déja un compte avec cet email");
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return Tuple2(true, json['token'].toString());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 BAD REQUEST - Json mal formaté");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 409) {
|
|
|
|
|
return const Tuple2(
|
|
|
|
|
false, "409 CONFLICT - Déja un compte avec cet email");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
}
|
|
|
|
@ -127,25 +151,28 @@ class RequestApi implements IDataStrategy {
|
|
|
|
|
@override
|
|
|
|
|
Future<Tuple2<bool, String>> modifAttribut(
|
|
|
|
|
String token, String nameAttribut, String newValue) async {
|
|
|
|
|
final response = await http.put(Uri.parse('$urlApi/user/$nameAttribut'),
|
|
|
|
|
headers: <String, String>{
|
|
|
|
|
'Authorization': token,
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: jsonEncode(<String, String>{nameAttribut: newValue}));
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
//Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return const Tuple2(true, "200 - OK");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "400 - UNAUTHORIZED");
|
|
|
|
|
} else {
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
try {
|
|
|
|
|
final response = await http.put(Uri.parse('$urlApi/user/$nameAttribut'),
|
|
|
|
|
headers: <String, String>{
|
|
|
|
|
'Authorization': token,
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
},
|
|
|
|
|
body: jsonEncode(<String, String>{nameAttribut: newValue}));
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
//Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return const Tuple2(true, "200 - OK");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "400 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- Priviligié uploadFileByte -- //
|
|
|
|
@ -169,19 +196,23 @@ class RequestApi implements IDataStrategy {
|
|
|
|
|
request.fields["SmartFit_Category"] = categoryActivity;
|
|
|
|
|
request.fields["SmartFit_Date"] = dateActivity;
|
|
|
|
|
|
|
|
|
|
final response = await request.send();
|
|
|
|
|
try {
|
|
|
|
|
final response = await request.send();
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2(true, "Successful");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 409) {
|
|
|
|
|
return const Tuple2(false, "409 - CONFLICT");
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2(true, "Successful");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 409) {
|
|
|
|
|
return const Tuple2(false, "409 - CONFLICT");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail ");
|
|
|
|
|
}
|
|
|
|
@ -209,38 +240,45 @@ class RequestApi implements IDataStrategy {
|
|
|
|
|
request.fields["SmartFit_Date"] = date.toString();
|
|
|
|
|
request.fields["info"] = activityInfo.toJson();
|
|
|
|
|
|
|
|
|
|
final response = await request.send();
|
|
|
|
|
try {
|
|
|
|
|
final response = await request.send();
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2(true, "Successful");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 409) {
|
|
|
|
|
return const Tuple2(false, "409 - CONFLICT");
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Tuple2(true, "Successful");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 409) {
|
|
|
|
|
return const Tuple2(false, "409 - CONFLICT");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Tuple2> getInfoUser(String token) async {
|
|
|
|
|
final response = await http.get(Uri.parse('$urlApi/user/info'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return Tuple2(true, json);
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
try {
|
|
|
|
|
final response = await http.get(Uri.parse('$urlApi/user/info'),
|
|
|
|
|
headers: <String, String>{'Authorization': token});
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
Map<String, dynamic> json = jsonDecode(response.body);
|
|
|
|
|
return Tuple2(true, json);
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
|
return const Tuple2(false, "400 - BAD REQUEST");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
|
return const Tuple2(false, "No connection");
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode == 401) {
|
|
|
|
|
return const Tuple2(false, "401 - UNAUTHORIZED");
|
|
|
|
|
}
|
|
|
|
|
return const Tuple2(false, "Fail ");
|
|
|
|
|
return const Tuple2(false, "Fail");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|