A better Rust ATProto crate

add memory credential session type

authored by vielle.dev and committed by

Orual f9da6477 45693ccb

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