|
| 1 | +//! # PubNub set state module. |
| 2 | +//! |
| 3 | +//! The [`GetStateRequestBuilder`] lets you make and execute requests that will |
| 4 | +//! associate the provided `state` with `user_id` on the provided list of |
| 5 | +//! channels and channels in channel groups. |
| 6 | +
|
| 7 | +use derive_builder::Builder; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + core::{ |
| 11 | + utils::{ |
| 12 | + encoding::{ |
| 13 | + url_encode_extended, url_encoded_channel_groups, url_encoded_channels, |
| 14 | + UrlEncodeExtension, |
| 15 | + }, |
| 16 | + headers::{APPLICATION_JSON, CONTENT_TYPE}, |
| 17 | + }, |
| 18 | + Deserializer, PubNubError, Transport, TransportMethod, TransportRequest, |
| 19 | + }, |
| 20 | + dx::{ |
| 21 | + presence::{ |
| 22 | + builders, |
| 23 | + result::{GetStateResponseBody, GetStateResult}, |
| 24 | + }, |
| 25 | + pubnub_client::PubNubClientInstance, |
| 26 | + }, |
| 27 | + lib::{ |
| 28 | + alloc::{ |
| 29 | + string::{String, ToString}, |
| 30 | + vec, |
| 31 | + }, |
| 32 | + collections::HashMap, |
| 33 | + }, |
| 34 | +}; |
| 35 | + |
| 36 | +/// The [`GetStateRequestBuilder`] is used to build `user_id` associated state |
| 37 | +/// update request that is sent to the [`PubNub`] network. |
| 38 | +/// |
| 39 | +/// This struct is used by the [`set_state`] method of the [`PubNubClient`]. |
| 40 | +/// The [`set_state`] method is used to update state associated with `user_id` |
| 41 | +/// on the provided channels and groups. |
| 42 | +/// |
| 43 | +/// [`PubNub`]:https://www.pubnub.com/ |
| 44 | +#[derive(Builder)] |
| 45 | +#[builder( |
| 46 | + pattern = "owned", |
| 47 | + build_fn(vis = "pub(in crate::dx::presence)", validate = "Self::validate"), |
| 48 | + no_std |
| 49 | +)] |
| 50 | +pub struct GetStateRequest<T, D> { |
| 51 | + /// Current client which can provide transportation to perform the request. |
| 52 | + /// |
| 53 | + /// This field is used to get [`Transport`] to perform the request. |
| 54 | + #[builder(field(vis = "pub(in crate::dx::presence)"), setter(custom))] |
| 55 | + pub(in crate::dx::presence) pubnub_client: PubNubClientInstance<T, D>, |
| 56 | + |
| 57 | + /// Channels with which state will be associated. |
| 58 | + #[builder( |
| 59 | + field(vis = "pub(in crate::dx::presence)"), |
| 60 | + setter(strip_option, into), |
| 61 | + default = "vec![]" |
| 62 | + )] |
| 63 | + pub(in crate::dx::presence) channels: Vec<String>, |
| 64 | + |
| 65 | + /// Channel groups with which state will be associated. |
| 66 | + /// |
| 67 | + /// The specified state will be associated with channels that have been |
| 68 | + /// included in the specified target channel groups. |
| 69 | + #[builder( |
| 70 | + field(vis = "pub(in crate::dx::presence)"), |
| 71 | + setter(into, strip_option), |
| 72 | + default = "vec![]" |
| 73 | + )] |
| 74 | + pub(in crate::dx::presence) channel_groups: Vec<String>, |
| 75 | + |
| 76 | + #[builder(field(vis = "pub(in crate::dx::presence)"), setter(strip_option, into))] |
| 77 | + /// Identifier for which `state` should be associated for provided list of |
| 78 | + /// channels and groups. |
| 79 | + pub(in crate::dx::presence) user_id: String, |
| 80 | +} |
| 81 | + |
| 82 | +impl<T, D> GetStateRequestBuilder<T, D> { |
| 83 | + /// Validate user-provided data for request builder. |
| 84 | + /// |
| 85 | + /// Validator ensure that list of provided data is enough to build valid |
| 86 | + /// set state request instance. |
| 87 | + fn validate(&self) -> Result<(), String> { |
| 88 | + let groups_len = self.channel_groups.as_ref().map_or_else(|| 0, |v| v.len()); |
| 89 | + let channels_len = self.channels.as_ref().map_or_else(|| 0, |v| v.len()); |
| 90 | + |
| 91 | + builders::validate_configuration(&self.pubnub_client).and_then(|_| { |
| 92 | + if channels_len == groups_len && channels_len == 0 { |
| 93 | + Err("Either channels or channel groups should be provided".into()) |
| 94 | + } else if self.user_id.is_none() { |
| 95 | + Err("User id is missing".into()) |
| 96 | + } else { |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + /// Build [`GetStateRequest`] from builder. |
| 103 | + fn request(self) -> Result<GetStateRequest<T, D>, PubNubError> { |
| 104 | + self.build() |
| 105 | + .map_err(|err| PubNubError::general_api_error(err.to_string(), None, None)) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<T, D> GetStateRequest<T, D> { |
| 110 | + /// Create transport request from the request builder. |
| 111 | + pub(in crate::dx::presence) fn transport_request( |
| 112 | + &self, |
| 113 | + ) -> Result<TransportRequest, PubNubError> { |
| 114 | + let sub_key = &self.pubnub_client.config.subscribe_key; |
| 115 | + let mut query: HashMap<String, String> = HashMap::new(); |
| 116 | + |
| 117 | + // Serialize list of channel groups and add into query parameters list. |
| 118 | + url_encoded_channel_groups(&self.channel_groups) |
| 119 | + .and_then(|channel_groups| query.insert("channel-group".into(), channel_groups)); |
| 120 | + |
| 121 | + Ok(TransportRequest { |
| 122 | + path: format!( |
| 123 | + "/v2/presence/sub-key/{sub_key}/channel/{}/uuid/{}", |
| 124 | + url_encoded_channels(&self.channels), |
| 125 | + url_encode_extended(self.user_id.as_bytes(), UrlEncodeExtension::NonChannelPath) |
| 126 | + ), |
| 127 | + query_parameters: query, |
| 128 | + method: TransportMethod::Get, |
| 129 | + headers: [(CONTENT_TYPE.into(), APPLICATION_JSON.into())].into(), |
| 130 | + body: None, |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl<T, D> GetStateRequestBuilder<T, D> |
| 136 | +where |
| 137 | + T: Transport, |
| 138 | + D: Deserializer + 'static, |
| 139 | +{ |
| 140 | + /// Build and call asynchronous request. |
| 141 | + pub async fn execute(self) -> Result<GetStateResult, PubNubError> { |
| 142 | + let request = self.request()?; |
| 143 | + let transport_request = request.transport_request()?; |
| 144 | + let client = request.pubnub_client.clone(); |
| 145 | + let deserializer = client.deserializer.clone(); |
| 146 | + transport_request |
| 147 | + .send::<GetStateResponseBody, _, _, _>(&client.transport, deserializer) |
| 148 | + .await |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[allow(dead_code)] |
| 153 | +#[cfg(feature = "blocking")] |
| 154 | +impl<T, D> GetStateRequestBuilder<T, D> |
| 155 | +where |
| 156 | + T: crate::core::blocking::Transport, |
| 157 | + D: Deserializer + 'static, |
| 158 | +{ |
| 159 | + /// Build and call synchronous request. |
| 160 | + pub fn execute_blocking(self) -> Result<GetStateResult, PubNubError> { |
| 161 | + let request = self.request()?; |
| 162 | + let transport_request = request.transport_request()?; |
| 163 | + let client = request.pubnub_client.clone(); |
| 164 | + let deserializer = client.deserializer.clone(); |
| 165 | + transport_request |
| 166 | + .send_blocking::<GetStateResponseBody, _, _, _>(&client.transport, deserializer) |
| 167 | + } |
| 168 | +} |
0 commit comments