From 9297468f6fc086031d08a704283e79b8eeb69f1c Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 8 May 2025 16:00:46 -0400 Subject: [PATCH] finished api calls for moving an email, cleaned, and made clearer logs for errors in api calls --- lib/api_service.dart | 65 +++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/lib/api_service.dart b/lib/api_service.dart index 75f21a8..b9ffdf8 100644 --- a/lib/api_service.dart +++ b/lib/api_service.dart @@ -18,9 +18,11 @@ import 'dart:js' as js; class ApiService { static String ip = ""; static String port = ""; - static List threadAttachments = []; + static List threadAttachments = + []; //holds attachments of the thread static String currFolder = ""; - static List currThread = []; + static List currThread = []; //holds the email ids of the thread + static String currThreadID = ""; //picked an email it prints the threadID Future> fetchEmailsFromFolder( String folder, int pagenitaion) async { @@ -31,7 +33,6 @@ class ApiService { 'offset': pagenitaion.toString(), }); var response = await http.get(url); - // print(response); List allEmails = []; if (response.statusCode == 200) { @@ -143,13 +144,52 @@ class ApiService { return content; } - Future moveEmail(String fromFolder, String uID, String toFolder) async { + Future> threadsInSerializable( + String thread_id) async { + // grab all of the emails in thread anyways, for the future it'll come in handy + var url = Uri.http('$ip:$port', 'get_thread_messages', {'id': thread_id}); + try { + var response = await http.get(url); + if (response.statusCode == 200) { + List json = jsonDecode(response.body); + List serializableMessages = []; + for (var mail in json) { + serializableMessages.add(SerializableMessage.fromJson(mail)); + } + return serializableMessages; + } else { + print( + "failed get request with status code ${response.statusCode}, and body ${response.body}"); + } + } catch (e) { + print("caught in threadInSerializable method error: $e"); + } + + return []; + } + + Future moveEmail( + //only moves the first email of the thread //or perhaps should do the last + String fromFolder, + String thread_id, + String toFolder) async { var url = Uri.http('$ip:$port', 'move_email'); + + List mailsInSerializable = + await this.threadsInSerializable(thread_id); + + if (mailsInSerializable.isEmpty) { + return false; + } + + SerializableMessage firstMail = mailsInSerializable[0]; + Map requestBody = { 'from': fromFolder, - 'uid': uID, + 'uid': firstMail.uid.toString(), 'to': toFolder, }; + try { var response = await http.post( url, @@ -162,10 +202,10 @@ class ApiService { print('response body ${response.body}'); return true; } else { - print('error '); + print('error ${response.statusCode} ${response.body}'); } } catch (e) { - print(e); + print("failed trying to post move_email, with error: $e"); } return false; } @@ -253,13 +293,6 @@ class ApiService { } Future logIn(String json) async { - // var url = Uri.https('') - // try{ - // String response = await http.post( - // url - // ); - // } - return false; } @@ -298,7 +331,6 @@ class ApiService { var response = await http.get(url); if (response.statusCode == 200) { var result = response.body; - // print(result); Map attachmentData = json.decode(result); AttachmentResponse data = AttachmentResponse.fromJson(attachmentData); print("data $data"); @@ -449,7 +481,8 @@ class _EmailViewState extends State { @override Widget build(BuildContext context) { - // print(currentContent); + // print("thread id ${widget.id}"); + ApiService.currThreadID = widget.id; return Scaffold( appBar: AppBar( title: Text(widget.name),