|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + service_bus::{ |
| 5 | + peek_lock_message, peek_lock_message2, receive_and_delete_message, send_message, |
| 6 | + PeekLockResponse, |
| 7 | + }, |
| 8 | + utils::body_bytes_to_utf8, |
| 9 | +}; |
| 10 | +use ring::hmac::Key; |
| 11 | +use std::time::Duration; |
| 12 | + |
| 13 | +use azure_core::{error::Error, HttpClient}; |
| 14 | + |
| 15 | +/// Client object that allows interaction with the ServiceBus API |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct TopicClient { |
| 18 | + http_client: Arc<dyn HttpClient>, |
| 19 | + namespace: String, |
| 20 | + topic: String, |
| 21 | + policy_name: String, |
| 22 | + signing_key: Key, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Clone)] |
| 26 | +pub struct TopicSender { |
| 27 | + topic_client: TopicClient, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Clone)] |
| 31 | +pub struct SubscriptionReceiver { |
| 32 | + topic_client: TopicClient, |
| 33 | + subscription: String, |
| 34 | +} |
| 35 | + |
| 36 | +impl TopicClient { |
| 37 | + /// Creates a new topic client instance |
| 38 | + pub fn new<N, T, P, K>( |
| 39 | + http_client: Arc<dyn HttpClient>, |
| 40 | + namespace: N, |
| 41 | + topic: T, |
| 42 | + policy_name: P, |
| 43 | + policy_key: K, |
| 44 | + ) -> Result<TopicClient, Error> |
| 45 | + where |
| 46 | + N: Into<String>, |
| 47 | + T: Into<String>, |
| 48 | + P: Into<String>, |
| 49 | + K: AsRef<str>, |
| 50 | + { |
| 51 | + let signing_key = Key::new(ring::hmac::HMAC_SHA256, policy_key.as_ref().as_bytes()); |
| 52 | + |
| 53 | + Ok(Self { |
| 54 | + http_client, |
| 55 | + namespace: namespace.into(), |
| 56 | + topic: topic.into(), |
| 57 | + policy_name: policy_name.into(), |
| 58 | + signing_key, |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + pub fn topic_sender(&self) -> TopicSender { |
| 63 | + TopicSender::new(self.clone()) |
| 64 | + } |
| 65 | + |
| 66 | + pub fn subscription_receiver(&self, subscription: &str) -> SubscriptionReceiver { |
| 67 | + SubscriptionReceiver::new(self.clone(), subscription) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl TopicSender { |
| 72 | + pub fn new(topic_client: TopicClient) -> TopicSender { |
| 73 | + Self { topic_client } |
| 74 | + } |
| 75 | + /// Sends a message to the topic |
| 76 | + pub async fn send_message(&self, msg: &str) -> Result<(), Error> { |
| 77 | + send_message( |
| 78 | + &self.topic_client.http_client, |
| 79 | + &self.topic_client.namespace, |
| 80 | + &self.topic_client.topic, |
| 81 | + &self.topic_client.policy_name, |
| 82 | + &self.topic_client.signing_key, |
| 83 | + msg, |
| 84 | + ) |
| 85 | + .await |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl SubscriptionReceiver { |
| 90 | + pub fn new<S>(topic_client: TopicClient, subscription: S) -> SubscriptionReceiver |
| 91 | + where |
| 92 | + S: Into<String>, |
| 93 | + { |
| 94 | + Self { |
| 95 | + topic_client, |
| 96 | + subscription: subscription.into(), |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// Receive and delete a message |
| 101 | + pub async fn receive_and_delete_message(&self) -> Result<String, Error> { |
| 102 | + body_bytes_to_utf8( |
| 103 | + receive_and_delete_message( |
| 104 | + &self.topic_client.http_client, |
| 105 | + &self.topic_client.namespace, |
| 106 | + &self.topic_client.topic, |
| 107 | + &self.topic_client.policy_name, |
| 108 | + &self.topic_client.signing_key, |
| 109 | + Some(&self.subscription), |
| 110 | + ) |
| 111 | + .await? |
| 112 | + .body(), |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + /// Non-destructively read a message |
| 117 | + /// |
| 118 | + /// Note: This function does not return the delete location |
| 119 | + /// of the message, so, after reading, you will lose |
| 120 | + /// "track" of it until the lock expiry runs out and |
| 121 | + /// the message can be consumed by others. If you want to keep |
| 122 | + /// track of this message (i.e., have the possibility of deletion), |
| 123 | + /// use `peek_lock_message2`. |
| 124 | + pub async fn peek_lock_message(&self, lock_expiry: Option<Duration>) -> Result<String, Error> { |
| 125 | + body_bytes_to_utf8( |
| 126 | + peek_lock_message( |
| 127 | + &self.topic_client.http_client, |
| 128 | + &self.topic_client.namespace, |
| 129 | + &self.topic_client.topic, |
| 130 | + &self.topic_client.policy_name, |
| 131 | + &self.topic_client.signing_key, |
| 132 | + lock_expiry, |
| 133 | + Some(&self.subscription), |
| 134 | + ) |
| 135 | + .await? |
| 136 | + .body(), |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + /// Non-destructively read a message but track it |
| 141 | + /// |
| 142 | + /// Note: This function returns a `PeekLockResponse` |
| 143 | + /// that contains a helper `delete_message` function. |
| 144 | + pub async fn peek_lock_message2( |
| 145 | + &self, |
| 146 | + timeout: Option<Duration>, |
| 147 | + ) -> Result<PeekLockResponse, Error> { |
| 148 | + peek_lock_message2( |
| 149 | + &self.topic_client.http_client, |
| 150 | + &self.topic_client.namespace, |
| 151 | + &self.topic_client.topic, |
| 152 | + &self.topic_client.policy_name, |
| 153 | + &self.topic_client.signing_key, |
| 154 | + timeout, |
| 155 | + Some(&self.subscription), |
| 156 | + ) |
| 157 | + .await |
| 158 | + } |
| 159 | +} |
0 commit comments