Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Authentication.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
4
5use CommonLibrary::{
6	Environment::Requires::Requires,
7	IPC::{DTO::ProxyTarget::ProxyTarget, IPCProvider::IPCProvider as IPCProviderTrait},
8};
9use serde_json::{Value, json};
10use tauri::Runtime;
11
12use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect, dev_log};
13
14pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
15	match MethodName {
16		"Authentication.GetSession" => {
17			let effect =
18				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
19					Box::pin(async move {
20						let provider_id = Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string();
21						let scopes = Parameters.get(1).cloned().unwrap_or(json!([]));
22						let options = Parameters.get(2).cloned().unwrap_or(json!({}));
23						let IPCProvider:Arc<dyn IPCProviderTrait> = run_time.Environment.Require();
24						let Method = format!("{}$getSession", ProxyTarget::ExtHostAuthentication.GetTargetPrefix());
25						match IPCProvider
26							.SendRequestToSideCar(
27								"cocoon-main".to_string(),
28								Method,
29								json!([provider_id, scopes, options]),
30								5000,
31							)
32							.await
33						{
34							Ok(value) => Ok(value),
35							Err(error) => {
36								dev_log!(
37									"ipc",
38									"warn: [Authentication.GetSession] extension did not answer ({:?}); returning null",
39									error
40								);
41								Ok(json!(null))
42							},
43						}
44					})
45				};
46			Some(Ok(Box::new(effect)))
47		},
48
49		"Authentication.GetAccounts" => {
50			let effect =
51				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
52					Box::pin(async move {
53						let provider_id = Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string();
54						let IPCProvider:Arc<dyn IPCProviderTrait> = run_time.Environment.Require();
55						let Method = format!("{}$getAccounts", ProxyTarget::ExtHostAuthentication.GetTargetPrefix());
56						match IPCProvider
57							.SendRequestToSideCar("cocoon-main".to_string(), Method, json!([provider_id]), 5000)
58							.await
59						{
60							Ok(value) => Ok(value),
61							Err(error) => {
62								dev_log!(
63									"ipc",
64									"warn: [Authentication.GetAccounts] extension did not answer ({:?}); returning []",
65									error
66								);
67								Ok(json!([]))
68							},
69						}
70					})
71				};
72			Some(Ok(Box::new(effect)))
73		},
74
75		_ => None,
76	}
77}