diff --git a/src/service/notification.rs b/src/service/notification.rs index 843c426cb3c8da3f1e3753bd640f677786a38909..4c21a7d163ba3a48ec1f957c53efdf99b6e8c378 100644 --- a/src/service/notification.rs +++ b/src/service/notification.rs @@ -47,4 +47,39 @@ async fn subscribe_request(product_type: String) -> Result<SubscriberRequest> { }, Err(e) => Err(compose_error_response(Status::NotFound, e.to_string())), }; +} + +pub fn unsubscribe(product_type: &str) -> Result<SubscriberRequest> { + let product_type_clone = String::from(product_type); + return thread::spawn(move || Self::unsubscribe_request(product_type_clone)) + .join().unwrap(); +} + +#[tokio::main] +async fn unsubscribe_request(product_type: String) -> Result<SubscriberRequest> { + let product_type_upper: String = product_type.to_uppercase(); + let product_type_str: &str = product_type_upper.as_str(); + let notification_receiver_url: String = format!("{}/receive", APP_CONFIG.get_instance_root_url()); + + let request_url: String = format!("{}/notification/unsubscribe/{}?url={}", APP_CONFIG.get_publisher_root_url(), product_type_str, notification_receiver_url); + let request = REQWEST_CLIENT + .post(request_url.clone()) + .header("Content-Type", "application/json") + .header("Accept", "application/json") + .send().await; + log::warn!("Sent unsubscribe 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::NotFound, + String::from("Already unsubscribed to the topic.") + )), + }, + Err(e) => Err(compose_error_response( + Status::NotFound, + e.to_string() + )), + }; } \ No newline at end of file