|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use crate::client::NangoClient; |
| 4 | +use crate::common_derives; |
| 5 | + |
| 6 | +common_derives! { |
| 7 | + pub struct CreateConnectSessionRequest { |
| 8 | + pub end_user: EndUser, |
| 9 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 10 | + pub organization: Option<Organization>, |
| 11 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 12 | + pub allowed_integrations: Option<Vec<String>>, |
| 13 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 14 | + pub integrations_config_defaults: Option<HashMap<String, IntegrationConfigDefault>>, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +common_derives! { |
| 19 | + pub struct EndUser { |
| 20 | + pub id: String, |
| 21 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 22 | + pub email: Option<String>, |
| 23 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 24 | + pub display_name: Option<String>, |
| 25 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 26 | + pub tags: Option<HashMap<String, String>>, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +common_derives! { |
| 31 | + pub struct Organization { |
| 32 | + pub id: String, |
| 33 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 34 | + pub display_name: Option<String>, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +common_derives! { |
| 39 | + pub struct IntegrationConfigDefault { |
| 40 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 41 | + pub user_scopes: Option<String>, |
| 42 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 43 | + pub connection_config: Option<ConnectionConfigOverride>, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +common_derives! { |
| 48 | + pub struct ConnectionConfigOverride { |
| 49 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 50 | + pub oauth_scopes_override: Option<String>, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +common_derives! { |
| 55 | + pub struct ConnectSession { |
| 56 | + pub token: String, |
| 57 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 58 | + pub connect_link: Option<String>, |
| 59 | + pub expires_at: String, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +common_derives! { |
| 64 | + pub struct ReconnectSessionRequest { |
| 65 | + pub connection_id: String, |
| 66 | + pub integration_id: String, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(serde::Deserialize)] |
| 71 | +pub(crate) struct DataWrapper<T> { |
| 72 | + pub data: T, |
| 73 | +} |
| 74 | + |
| 75 | +impl NangoClient { |
| 76 | + pub async fn create_connect_session( |
| 77 | + &self, |
| 78 | + req: CreateConnectSessionRequest, |
| 79 | + ) -> Result<ConnectSession, crate::Error> { |
| 80 | + let mut url = self.api_base.clone(); |
| 81 | + url.set_path("/connect/sessions"); |
| 82 | + |
| 83 | + let response = self.client.post(url).json(&req).send().await?; |
| 84 | + let wrapper: DataWrapper<ConnectSession> = crate::client::parse_response(response).await?; |
| 85 | + Ok(wrapper.data) |
| 86 | + } |
| 87 | + |
| 88 | + pub async fn reconnect_session( |
| 89 | + &self, |
| 90 | + req: ReconnectSessionRequest, |
| 91 | + ) -> Result<ConnectSession, crate::Error> { |
| 92 | + let mut url = self.api_base.clone(); |
| 93 | + url.set_path("/connect/sessions/reconnect"); |
| 94 | + |
| 95 | + let response = self.client.post(url).json(&req).send().await?; |
| 96 | + let wrapper: DataWrapper<ConnectSession> = crate::client::parse_response(response).await?; |
| 97 | + Ok(wrapper.data) |
| 98 | + } |
| 99 | + |
| 100 | + pub async fn get_connect_session(&self) -> Result<ConnectSession, crate::Error> { |
| 101 | + let mut url = self.api_base.clone(); |
| 102 | + url.set_path("/connect/session"); |
| 103 | + |
| 104 | + let response = self.client.get(url).send().await?; |
| 105 | + let wrapper: DataWrapper<ConnectSession> = crate::client::parse_response(response).await?; |
| 106 | + Ok(wrapper.data) |
| 107 | + } |
| 108 | + |
| 109 | + pub async fn delete_connect_session(&self) -> Result<(), crate::Error> { |
| 110 | + let mut url = self.api_base.clone(); |
| 111 | + url.set_path("/connect/session"); |
| 112 | + |
| 113 | + let response = self.client.delete(url).send().await?; |
| 114 | + crate::client::check_response(response).await?; |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | +} |
0 commit comments