From b16f7137c0555f613c1b733a3872993118588c34 Mon Sep 17 00:00:00 2001 From: vissutagunawan <vglim3653@gmail.com> Date: Thu, 27 Mar 2025 10:09:57 +0700 Subject: [PATCH] Implement subscribe function in Notification service. --- src/service/notification.rs | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/service/notification.rs b/src/service/notification.rs index a3b3b11..aa0e757 100644 --- a/src/service/notification.rs +++ b/src/service/notification.rs @@ -11,4 +11,41 @@ use rocket::http::Status; pub struct NotificationService; - impl NotificationService {} \ No newline at end of file + impl NotificationService { + async fn subscribe_request(product_type: String) -> Result<SubscriberRequest> { + let product_type_upper = product_type.to_uppercase(); + let product_type_str = product_type_upper.as_str(); + let notification_reciever_url = format!("{}/recieve", APP_CONFIG.get_instance_root_url()); + let payload = SubscriberRequest { + name: APP_CONFIG.get_instance_name().to_string(), + url: notification_reciever_url, + }; + let request_url = format!( + "{}/notification/subscribe/{}", + APP_CONFIG.get_instance_root_url(), + product_type_str + ); + let request = REQWEST_CLIENT + .post(request_url.clone()) + .header("Content-Type", "application/json") + .header("Accept", "application/json") + .body(to_string(&payload).unwrap()) + .send() + .await; + log::warn_!("Sent subscribe request to {}", request_url); + return match request { + Ok(f) => match f.json::<SubscriberRequest>().await { + Ok(x) => Ok(x), + Err(y) => Err(compose_error_response(Status::NotAcceptable, y.to_string())), + }, + Err(e) => Err(compose_error_response(Status::NotFound, e.to_string())), + }; + } + + pub fn subscribe(product_type: &str) -> Result<SubscriberRequest> { + let product_type_clone = String::from(product_type); + return thread::spawn(move || Self::subscribe_request(product_type_clone)) + .join() + .unwrap(); + } + } \ No newline at end of file -- GitLab