diff --git a/Cargo.lock b/Cargo.lock index 340bd88..c69a9b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -715,6 +715,19 @@ dependencies = [ "syn", ] +[[package]] +name = "dev-communicators" +version = "0.1.0" +source = "git+https://git.franklinblanco.dev/franklinblanco/dev-communicators.git#16f9b31a7099caf86abe4c0ce1859b2ed714d9e6" +dependencies = [ + "actix-web-utils", + "dev-dtos", + "err", + "reqwest", + "serde", + "tokio", +] + [[package]] name = "dev-dtos" version = "0.1.0" @@ -1370,6 +1383,7 @@ dependencies = [ "chat-types", "chrono", "chrono-tz", + "dev-communicators", "dev-dtos", "err", "futures", diff --git a/Cargo.toml b/Cargo.toml index 1a37bed..d069fe8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,10 @@ futures-util = "0.3.26" # internal deps err = { git = "https://git.franklinblanco.dev/franklinblanco/err.git" } league-types = { git = "https://git.franklinblanco.dev/franklinblanco/league-types.git" } + dev-dtos = { git = "https://git.franklinblanco.dev/franklinblanco/user-svc-dtos-rust.git" } +dev-communicators = { git = "https://git.franklinblanco.dev/franklinblanco/dev-communicators.git" } + chat-types = { git = "https://git.franklinblanco.dev/franklinblanco/chat-types.git" } chat-communicators = { git = "https://git.franklinblanco.dev/franklinblanco/chat-communicators.git" } diff --git a/src/callbacks/chat.rs b/src/callbacks/chat.rs index ed2dee9..ea86edc 100644 --- a/src/callbacks/chat.rs +++ b/src/callbacks/chat.rs @@ -1,11 +1,11 @@ -use std::sync::{Arc}; +use std::{sync::{Arc}, thread, time::Duration}; use chat_types::{dto::server_in::ServerMessageIn, domain::chat_message::{ChatMessageSender, ChatMessageContent}}; use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto; use tokio::{runtime::Runtime, sync::RwLock}; -use crate::{utils::storage, ForeignError, client::chat::init_client_connection}; +use crate::{utils::storage, ForeignError, client::chat::init_client_connection, RustError}; pub enum ClientError { One, Two, Three @@ -38,7 +38,14 @@ impl<'a> WebsocketCaller { pub fn init_ws_connection(&'a self, websocket_ffi: Box) { let ws_ffi_rwlock = Arc::new(websocket_ffi); - let user: UserForAuthenticationDto = storage::read("user".into()).unwrap(); //TODO: Remove unwrap + let mut user_result: Result = storage::read("user".into()); + + while let Err(error) = user_result { + println!("Couldn't read stored user. Error: {error}"); + thread::sleep(Duration::from_millis(500)); + user_result = storage::read("user".into()); + } + let user: UserForAuthenticationDto = user_result.unwrap(); let rt = Runtime::new().unwrap(); let _ = rt.block_on( diff --git a/src/client/league/mod.rs b/src/client/league/mod.rs index e670350..8fc07d5 100644 --- a/src/client/league/mod.rs +++ b/src/client/league/mod.rs @@ -5,13 +5,14 @@ use reqwest::Method; use crate::{client::base::perform_request_without_client_sync as perform_request, RustError}; -pub const BASE_URL: &str = "http://backend.blancoinfante.com/"; +const BASE_URL: &str = "http://backend.blancoinfante.com/"; // ############# // SPORT ROUTES // ############# pub fn get_all_sports() -> Result, RustError> { perform_request::<(), Vec>(BASE_URL.to_string(), Method::GET, "league/sport".into(), None, 200, vec![]) + //perform_request::<(), Vec>("https://google.com/".into(), Method::GET, "".into(), None, 200, vec![]) } // ############# diff --git a/src/client/mod.rs b/src/client/mod.rs index 77b8d75..f94decc 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1,3 +1,4 @@ pub mod league; pub mod base; -pub mod chat; \ No newline at end of file +pub mod chat; +pub mod user; \ No newline at end of file diff --git a/src/client/user/mod.rs b/src/client/user/mod.rs new file mode 100644 index 0000000..c406c06 --- /dev/null +++ b/src/client/user/mod.rs @@ -0,0 +1,20 @@ + + +use dev_communicators::middleware::user_svc::user_service::authenticate_user_with_token; +use dev_dtos::{dtos::user::user_dtos::{UserForAuthenticationDto}}; +use reqwest::{Client}; +use tokio::runtime::Runtime; + +use crate::{RustError}; +//const BASE_URL: &str = "http://backend.blancoinfante.com/"; + +pub fn authenticate_user(user: UserForAuthenticationDto) -> Result<(), RustError> { + let rt = Runtime::new().unwrap(); + let client = Client::new(); + match rt.block_on( + authenticate_user_with_token(&client, &user) + ) { + Ok(_) => Ok(()), + Err(error) => Err(error.into()), + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2fe076d..02993d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,22 +5,21 @@ pub mod client; pub mod utils; pub mod callbacks; +use utils::storage; + pub use chat_types::client_types::chat_room::ChatRoom; pub use chat_types::dto::chat::ChatRoomParticipants; -pub use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto; -use utils::storage; +pub use dev_dtos::dtos::user::user_dtos::{UserForAuthenticationDto}; pub use utils::storage::*; pub use callbacks::chat::*; pub use league_types::domain::sport::Sport; pub use types::error::*; pub use client::chat::http::*; +pub use client::league::*; +pub use client::user::*; pub use chat_types::client_types::chat_message::*; -pub fn get_all_sports() -> Result, RustError> { - client::league::get_all_sports() -} - pub fn get_me() -> Result { storage::read("user".into()) } diff --git a/src/network.udl b/src/network.udl index d8567f9..320a3a0 100644 --- a/src/network.udl +++ b/src/network.udl @@ -93,4 +93,6 @@ namespace network { void init_storage(string path); [Throws=RustError] ChatRoom create_new_chat_room(UserForAuthenticationDto user, ChatRoomParticipants participants, string title); + [Throws=RustError] + void authenticate_user(UserForAuthenticationDto user); }; \ No newline at end of file diff --git a/src/utils/storage.rs b/src/utils/storage.rs index f04db1d..c5d70c8 100644 --- a/src/utils/storage.rs +++ b/src/utils/storage.rs @@ -12,9 +12,10 @@ pub fn init_storage(path: String) { let mut write = STORAGE_PATH.write().expect("FATAL ERROR. FAILED TO SECURE A RWLOCK CORRECTLY. STORAGE WON'T WORK NOW."); *write = path; drop(write); + // TESTING PURPOSES - store(UserForAuthenticationDto{ app: "".into(), id: "3".into(), token: "/2uuNJG3Z2bT9VVd64xBeACPxg64GicloiXtG9uO87as5q5g46TtNu0sAVTACyR8R8uMVXoTBlBP4Q3JhcGB2Q==".to_string() }, "user".into()).unwrap(); - println!("Wrote shit"); + //TODO: Remove this line in prod + //store(UserForAuthenticationDto{ app: "".into(), id: "3".into(), token: "/2uuNJG3Z2bT9VVd64xBeACPxg64GicloiXtG9uO87as5q5g46TtNu0sAVTACyR8R8uMVXoTBlBP4Q3JhcGB2Q==".to_string() }, "user".into()).unwrap(); } // These functions won't be exposed to the clients, just used by the functions they call internally @@ -38,4 +39,10 @@ pub fn read(path: String) -> Result { Ok(object) => Ok(object), Err(e) => return Err(RustError::SerdeError { error: MessageResource { key: "STORAGE.OBJECT_DESERIALIZATION".into(), message: Some(e.to_string()) }, serde_error_str: None }), } +} +pub fn delete(path: String) -> Result<(), RustError> { + match fs::remove_file(format!("{}/{}", STORAGE_PATH.read().unwrap(), path)) { + Ok(_) => Ok(()), + Err(e) => return Err(RustError::IO { error: MessageResource { key:"STORAGE.OBJECT_DELETING".into(), message: Some(e.to_string())} }), + } } \ No newline at end of file