From 23864f15662c3ecef845e75ca77e7243607e8526 Mon Sep 17 00:00:00 2001 From: Enzo Date: Fri, 10 Nov 2023 13:58:04 +0100 Subject: [PATCH] =?UTF-8?q?Fin=20des=20appel=20=C3=A0=20l'appi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Modele/Api/i_data_strategy.dart | 13 ++-- lib/Modele/Api/request_api.dart | 98 ++++++++++++++------------ lib/Modele/manager_file.dart | 20 +----- lib/View/page_test.dart | 104 +++++++++++++++++++++++++++- lib/main.dart | 4 +- pubspec.yaml | 1 + 6 files changed, 171 insertions(+), 69 deletions(-) diff --git a/lib/Modele/Api/i_data_strategy.dart b/lib/Modele/Api/i_data_strategy.dart index e7c7286..eda994a 100644 --- a/lib/Modele/Api/i_data_strategy.dart +++ b/lib/Modele/Api/i_data_strategy.dart @@ -4,25 +4,26 @@ import 'package:tuple/tuple.dart'; abstract class IDataStrategy { // Create user - Future postUser(String email, String hash, String username); + Future> postUser( + String email, String hash, String username); // Delete user - Future deleteUser(String token); + Future> deleteUser(String token); // Get Token validate - Future connexion(String email, String hash); + Future> connexion(String email, String hash); // Get all files for user - //Future getFiles(String token); + Future getFiles(String token); // Upload file on BDD Future> uploadFile(String token, File file); // Get one file by id - Future> getFile(String token, String fileUuid); + Future getFile(String token, String fileUuid); // Delete one file on BDD - //Future deleteFile(String token, String idFile); + Future> deleteFile(String token, String fileUuid); /* -> Modification attribut // Update email diff --git a/lib/Modele/Api/request_api.dart b/lib/Modele/Api/request_api.dart index b35bfb5..d25ac1f 100644 --- a/lib/Modele/Api/request_api.dart +++ b/lib/Modele/Api/request_api.dart @@ -11,31 +11,48 @@ class RequestApi extends IDataStrategy { "https://codefirst.iut.uca.fr/containers/SmartFit-smartfit_api"; @override - Future> getFile(String token, String fileUuid) async { - final response = await http.get(Uri.parse('$urlApi/user/files/$fileUuid'), - headers: {'Authorization': token}); + Future getFile(String token, String fileUuid) async { + final url = Uri.parse('$urlApi/user/files/$fileUuid'); + + var request = http.Request('GET', url); + request.headers.addAll({'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); if (response.statusCode == 200) { - /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ - throw UnimplementedError(); - } else { - throw UnimplementedError(); + 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"); + } + return const Tuple2(false, "Fail"); } @override - Future deleteFile(String token, String idFile) async { - final response = await http.delete(Uri.parse('$urlApi/$token/files')); + Future> deleteFile(String token, String fileUuid) async { + final response = await http.delete(Uri.parse('$urlApi/user/files'), + headers: {'Authorization': token}); + if (response.statusCode == 200) { - /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ - throw UnimplementedError(); - } else { - throw UnimplementedError(); + return const Tuple2(true, "Successful"); + } + if (response.statusCode == 401) { + return const Tuple2(false, "401 - UNAUTHORIZED"); + } + if (response.statusCode == 404) { + return const Tuple2(false, "404 - NOT FOUND"); } + return const Tuple2(false, "Fail"); } @override - Future deleteUser(String token) async { + Future> deleteUser(String token) async { final response = await http.delete(Uri.parse('$urlApi/user'), headers: {'Authorization': token}); if (response.statusCode == 200) { @@ -51,44 +68,45 @@ class RequestApi extends IDataStrategy { } @override - Future getFiles(String token) async { - final response = await http.get(Uri.parse('$urlApi/$token/files')); + Future getFiles(String token) async { + final response = await http.get(Uri.parse('$urlApi/user/files'), + headers: {'Authorization': token}); if (response.statusCode == 200) { - /*return Classe.fromJson(jsonDecode(response.body) as Map);*/ - throw UnimplementedError(); - } else { - throw UnimplementedError(); + return Tuple2(true, response.body); + } + if (response.statusCode == 401) { + return const Tuple2(false, "401 - UNAUTHORIZED"); } + return const Tuple2(false, "Fail"); } @override - Future connexion(String email, String hash) async { + Future> connexion(String email, String hash) async { final response = await http.get(Uri.parse('$urlApi/user/login/$email/$hash')); if (response.statusCode == 200) { - Map json = - jsonDecode(response.body) as Map; - return Tuple2(true, json['token']!); + Map json = jsonDecode(response.body); + return Tuple2(true, json['token'].toString()); } else if (response.statusCode == 401) { return const Tuple2(false, "UNAUTHORIZED"); } return const Tuple2(false, "Fail"); } - /* @override - Future postUser(String email, String hash, String username) async { - final response = - await http.post(Uri.parse('$urlApi/user'), headers: { - 'Content-Type': 'application/json; charset=UTF-8', - }, body: { - "email": "'$email'", - "hash": "'$hash'", - "username": "'$username'" - }); + Future> postUser( + String email, String hash, String username) async { + final response = await http.post(Uri.parse('$urlApi/user'), + body: { + "email": email, + "hash": hash, + "username": username + }); + if (response.statusCode == 200) { - return const Tuple2(true, "Successful"); + Map json = jsonDecode(response.body); + return Tuple2(true, json['token'].toString()); } if (response.statusCode == 400) { return const Tuple2(false, "400 BAD REQUEST - Json mal formaté"); @@ -98,7 +116,7 @@ class RequestApi extends IDataStrategy { false, "409 CONFLICT - Déja un compte avec cet email"); } return const Tuple2(false, "Fail"); - }*/ + } /* @override @@ -161,7 +179,7 @@ class RequestApi extends IDataStrategy { var request = http.MultipartRequest('POST', uri); final httpImage = http.MultipartFile.fromBytes( - 'file_FIT', await file.readAsBytes(), + 'file', await file.readAsBytes(), filename: file.path.split('/').last); request.files.add(httpImage); request.headers.addAll(headers); @@ -182,10 +200,4 @@ class RequestApi extends IDataStrategy { } return const Tuple2(false, "Fail "); } - - @override - Future postUser(String email, String hash, String username) { - // TODO: implement postUser - throw UnimplementedError(); - } } diff --git a/lib/Modele/manager_file.dart b/lib/Modele/manager_file.dart index dd3ea85..2de8703 100644 --- a/lib/Modele/manager_file.dart +++ b/lib/Modele/manager_file.dart @@ -1,21 +1,12 @@ import 'dart:convert'; -import 'dart:ffi'; import 'dart:io'; import 'package:csv/csv.dart'; import 'package:fit_tool/fit_tool.dart'; import 'package:fl_chart/fl_chart.dart'; import 'package:path_provider/path_provider.dart'; -import 'package:smartfit_app_mobile/Modele/Api/i_data_strategy.dart'; -import 'package:smartfit_app_mobile/Modele/Api/request_api.dart'; import 'package:smartfit_app_mobile/Modele/activity.dart'; class ManagerFile { - final IDataStrategy _dataStrategy = RequestApi(); - //List? _contentFile; - - //List? get contentFile => _contentFile; - // ----- // - // ----- Read csv File ------- // Future> readCSVFile(String path) async { if (File(path).exists() == false) return List.empty(); @@ -27,13 +18,6 @@ class ManagerFile { return fields; } - // ------ Import File and save it in BDD - Future importFileAndSaveInBDD(String path, String tokenUser) async { - if (File(path).existsSync() == false) return false; - _dataStrategy.uploadFile(tokenUser, File(path)); - return true; - } - // ----- Read a file FIT --- // Future> readFitFile(String path) async { if (File(path).existsSync() == false) return List.empty(); @@ -46,14 +30,14 @@ class ManagerFile { } // ------------- Get The path of application --- // - Future get _localPath async { + Future get localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } // --- A modifier si utilisé --- // Future writeFile(String nameFileWithExtension, File file) async { - final outFile = File("${await _localPath}\\Files\\$nameFileWithExtension"); + final outFile = File("${await localPath}\\Files\\$nameFileWithExtension"); if (outFile.existsSync() == false) { outFile.createSync(recursive: true); } diff --git a/lib/View/page_test.dart b/lib/View/page_test.dart index c42bae4..890ba3a 100644 --- a/lib/View/page_test.dart +++ b/lib/View/page_test.dart @@ -1,12 +1,18 @@ +import 'dart:convert'; + +import 'package:crypto/crypto.dart'; import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; import 'package:file_picker/file_picker.dart'; import 'dart:io'; import 'package:provider/provider.dart'; +import 'package:smartfit_app_mobile/Modele/Api/i_data_strategy.dart'; +import 'package:smartfit_app_mobile/Modele/Api/request_api.dart'; import 'package:smartfit_app_mobile/Modele/activity.dart'; import 'package:smartfit_app_mobile/Modele/manager_file.dart'; import 'package:smartfit_app_mobile/Modele/user.dart'; +import 'package:tuple/tuple.dart'; // ----------- File --------------- // @@ -56,6 +62,7 @@ class TestPage extends StatefulWidget { class _TestPage extends State { // Lire un fichier avec picker FilePickerResult? result; + IDataStrategy strategy = RequestApi(); //late File x = File(file.path); Future readFile() async { @@ -74,6 +81,90 @@ class _TestPage extends State { } } + Future createUser() async { + String mds = "1234"; + var byte = utf8.encode(mds); + var digest = sha256.convert(byte); + print(digest.toString()); + print("Appel"); + Tuple2 res = + await strategy.postUser("toto@gmail.com", digest.toString(), "toto"); + print(res.item1); + print(res.item2); + } + + Future login() async { + String mds = "1234"; + var byte = utf8.encode(mds); + var digest = sha256.convert(byte); + print(digest.toString()); + print("Appel"); + Tuple2 res = + await strategy.connexion("toto@gmail.com", digest.toString()); + print(res.item1); + print(res.item2); + } + + Future deleteUser() async { + String token = + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiYjA3OThmMjAtN2ZiMy0xMWVlLWJhZmQtMDI0MjBhNWEwMDFmIiwiZXhwIjoxNzA0ODgyNDI3fQ.2_bnvEC7_pwchielF3Kpu9fFtXDv_KabdOU8T07UnWI"; + print("Appel"); + Tuple2 res = await strategy.deleteUser(token); + print(res.item1); + print(res.item2); + } + + Future getFiles() async { + String token = + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiOGUyYWVmMTItN2ZiNC0xMWVlLWJhZmQtMDI0MjBhNWEwMDFmIiwiZXhwIjoxNzA0ODgyNzk3fQ.b_zsOHj2C-Y28CrcozbSjEz8BUWL8kgjjx5CDhES8PI"; + print("Appel"); + Tuple2 res = await strategy.getFiles(token); + print(res.item1); + print(res.item2); + } + + Future modifAttribut() async { + String nameAtt = "username"; + String newValue = "toto2"; + String token = + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiOGUyYWVmMTItN2ZiNC0xMWVlLWJhZmQtMDI0MjBhNWEwMDFmIiwiZXhwIjoxNzA0ODgzMDM4fQ.umN7LmUDbKUOeIToLcsOUIioQ7u4wsReHggRDB68VPQ"; + print("Appel"); + Tuple2 res = await strategy.modifAttribut(token, nameAtt, newValue); + print(res.item1); + print(res.item2); + } + + Future uploadFile() async { + PlatformFile t = result!.files.single; + String token = + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiOGUyYWVmMTItN2ZiNC0xMWVlLWJhZmQtMDI0MjBhNWEwMDFmIiwiZXhwIjoxNzA0ODgzNjM5fQ.0TmfJ9eYnszw4_RkNwPkMzkJxvsIFs5BI9uhQ7qYb0g"; + String? lol = t.path!; + print("Appel"); + Tuple2 res = await strategy.uploadFile(token, File(lol)); + print(res.item1); + print(res.item2); + } + + Future getOneFile() async { + String ui = "fc6e234c-7fc6-11ee-bafd-02420a5a001f"; + String token = + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiOGUyYWVmMTItN2ZiNC0xMWVlLWJhZmQtMDI0MjBhNWEwMDFmIiwiZXhwIjoxNzA0ODgzOTE3fQ.TUdrGEo7A0auQlUfO5RQm874QWuGXFBSKbJ8qTGPF2M"; + print("Appel"); + Tuple2 res = await strategy.getFile(token, ui); + print(res.item1); + print(res.item2); + + ManagerFile x = ManagerFile(); + File file = File("${await x.localPath}/Walking_2023-11-08T10_57_28.fit"); + await file.create(); + await file.writeAsBytes(res.item2); + print(await x.localPath); + print("Save"); + + print(await x + .readFitFile("${await x.localPath}/Walking_2023-11-08T10_57_28.fit")); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -101,7 +192,18 @@ class _TestPage extends State { } }, child: const Text("File - ")), - ElevatedButton(onPressed: readFile, child: const Text("Read Data")) + ElevatedButton(onPressed: login, child: const Text("Connexion")), + ElevatedButton( + onPressed: createUser, child: const Text("Create User")), + ElevatedButton( + onPressed: deleteUser, child: const Text("Delete User")), + ElevatedButton(onPressed: getFiles, child: const Text("getFiles")), + ElevatedButton( + onPressed: modifAttribut, child: const Text("modif attribut")), + ElevatedButton( + onPressed: uploadFile, child: const Text("Upload File")), + ElevatedButton( + onPressed: getOneFile, child: const Text("Get One File")) ], ), ); diff --git a/lib/main.dart b/lib/main.dart index 408ec98..1643aec 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:smartfit_app_mobile/Modele/user.dart'; import 'package:smartfit_app_mobile/View/on_boarding/started_view.dart'; +import 'package:smartfit_app_mobile/View/page_test.dart'; import 'package:smartfit_app_mobile/common/colo_extension.dart'; void main() { @@ -36,7 +37,8 @@ class MyApp extends StatelessWidget { // tested with just a hot reload. primaryColor: TColor.primaryColor1, fontFamily: "Poppins"), - home: const StartedView(), + //home: const StartedView(), + home: const TestPage(), ); } } diff --git a/pubspec.yaml b/pubspec.yaml index affc2bd..cfd00f8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -53,6 +53,7 @@ dependencies: http: ^1.1.0 provider: ^6.0.5 tuple: ^2.0.2 + crypto: ^3.0.3 dev_dependencies: flutter_test: