azure_functions/bindings/signalr_group_action.rs
1use crate::{
2 rpc::{typed_data::Data, TypedData},
3 signalr::GroupAction,
4 FromVec,
5};
6use serde::{Deserialize, Serialize};
7use serde_json::{to_string, to_value, Value};
8
9/// Represents the SignalR group action output binding.
10///
11/// The following binding attributes are supported:
12///
13/// | Name | Description |
14/// |--------------|------------------------------------------------------------------------------------------------------------------------------|
15/// | `name` | The name of the parameter being bound. |
16/// | `hub_name` | The name of the SignalR hub that will receive the group action. |
17/// | `connection` | The name of the app setting that contains the SignalR Service connection string. Defaults to `AzureSignalRConnectionString`. |
18///
19/// # Examples
20///
21/// This example implements an HTTP-triggered Azure Function that adds a user to a group:
22///
23/// ```rust
24/// use azure_functions::{
25/// bindings::{HttpRequest, SignalRGroupAction},
26/// func,
27/// signalr::GroupAction,
28/// };
29///
30/// #[func]
31/// #[binding(name = "req", auth_level = "anonymous", methods = "post")]
32/// #[binding(name = "$return", hub_name = "chat", connection = "myconnection")]
33/// pub fn add_to_group(req: HttpRequest) -> SignalRGroupAction {
34/// SignalRGroupAction {
35/// user_id: req.query_params().get("user").unwrap().to_owned(),
36/// group_name: req.query_params().get("group").unwrap().to_owned(),
37/// action: GroupAction::Add,
38/// }
39/// }
40/// ```
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct SignalRGroupAction {
44 /// The name of the group to operate on.
45 pub group_name: String,
46 /// The user id to operate on.
47 pub user_id: String,
48 /// The action to take.
49 pub action: GroupAction,
50}
51
52#[doc(hidden)]
53impl Into<TypedData> for SignalRGroupAction {
54 fn into(self) -> TypedData {
55 TypedData {
56 data: Some(Data::Json(
57 to_string(&self).expect("failed to convert SignalR group action to JSON string"),
58 )),
59 }
60 }
61}
62
63#[doc(hidden)]
64impl FromVec<SignalRGroupAction> for TypedData {
65 fn from_vec(vec: Vec<SignalRGroupAction>) -> Self {
66 TypedData {
67 data: Some(Data::Json(
68 Value::Array(vec.into_iter().map(|a| to_value(a).unwrap()).collect()).to_string(),
69 )),
70 }
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn it_serializes_to_json() {
80 let json = to_string(&SignalRGroupAction {
81 group_name: "foo".to_owned(),
82 user_id: "bar".to_owned(),
83 action: GroupAction::Add,
84 })
85 .unwrap();
86
87 assert_eq!(json, r#"{"groupName":"foo","userId":"bar","action":"add"}"#);
88 }
89
90 #[test]
91 fn it_converts_to_typed_data() {
92 let action = SignalRGroupAction {
93 group_name: "foo".to_owned(),
94 user_id: "bar".to_owned(),
95 action: GroupAction::Remove,
96 };
97
98 let data: TypedData = action.into();
99 assert_eq!(
100 data.data,
101 Some(Data::Json(
102 r#"{"groupName":"foo","userId":"bar","action":"remove"}"#.to_string()
103 ))
104 );
105 }
106}