+58
crates/jacquard/src/client.rs
+58
crates/jacquard/src/client.rs
···
1048
Self::unauthenticated()
1049
}
1050
}
1051
+
1052
+
/// MemoryCredentialSession: credential session with in memory store and identity resolver
1053
+
pub type MemoryCredentialSession = CredentialSession<
1054
+
MemorySessionStore<SessionKey, AtpSession>,
1055
+
jacquard_identity::PublicResolver,
1056
+
>;
1057
+
1058
+
impl MemoryCredentialSession {
1059
+
/// Create an unauthenticated MemoryCredentialSession.
1060
+
///
1061
+
/// Uses an in memory store and a public resolver.
1062
+
/// Equivalent to a BasicClient that isn't wrapped in Agent
1063
+
fn unauthenticated() -> Self {
1064
+
use std::sync::Arc;
1065
+
let http = reqwest::Client::new();
1066
+
let resolver = jacquard_identity::PublicResolver::new(http, Default::default());
1067
+
let store = MemorySessionStore::default();
1068
+
CredentialSession::new(Arc::new(store), Arc::new(resolver))
1069
+
}
1070
+
1071
+
/// Create a MemoryCredentialSession and authenticate with the provided details
1072
+
///
1073
+
/// - `identifier`: handle (preferred), DID, or `https://` PDS base URL.
1074
+
/// - `session_id`: optional session label; defaults to "session".
1075
+
/// - Persists and activates the session, and updates the base endpoint to the user's PDS.
1076
+
///
1077
+
/// # Example
1078
+
/// ```no_run
1079
+
/// # use jacquard::client::BasicClient;
1080
+
/// # use jacquard::types::string::AtUri;
1081
+
/// # use jacquard_api::app_bsky::feed::post::Post;
1082
+
/// use crate::jacquard::client::{Agent, AgentSessionExt};
1083
+
/// # #[tokio::main]
1084
+
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
1085
+
/// let (session, _) = MemoryCredentialSession::authenticated(identifier, password, None);
1086
+
/// let agent = Agent::from(session);
1087
+
/// let output = agent.create_record::<Post>(post, None).await?;
1088
+
/// # Ok(())
1089
+
/// # }
1090
+
/// ```
1091
+
async fn authenticated(
1092
+
identifier: CowStr<'_>,
1093
+
password: CowStr<'_>,
1094
+
session_id: Option<CowStr<'_>>,
1095
+
) -> Result<(Self, AtpSession), ClientError> {
1096
+
let session = MemoryCredentialSession::unauthenticated();
1097
+
let auth = session
1098
+
.login(identifier, password, session_id, None, None)
1099
+
.await?;
1100
+
Ok((session, auth))
1101
+
}
1102
+
}
1103
+
1104
+
impl Default for MemoryCredentialSession {
1105
+
fn default() -> Self {
1106
+
MemoryCredentialSession::unauthenticated()
1107
+
}
1108
+
}