diff --git a/atrium-api/src/agent.rs b/atrium-api/src/agent.rs index 83ed0eb2..8f261c27 100644 --- a/atrium-api/src/agent.rs +++ b/atrium-api/src/agent.rs @@ -78,6 +78,9 @@ impl Agent where M: CloneWithProxy + SessionManager + Send + Sync, { + /// Configures the atproto-proxy header to be applied on requests. + /// + /// Returns a new client service with the proxy header configured. pub fn api_with_proxy( &self, did: Did, @@ -91,12 +94,15 @@ impl Configure for Agent where M: Configure + SessionManager + Send + Sync, { + /// Set the current endpoint. fn configure_endpoint(&self, endpoint: String) { self.session_manager.configure_endpoint(endpoint); } + /// Configures the moderation services to be applied on requests. fn configure_labelers_header(&self, labeler_dids: Option>) { self.session_manager.configure_labelers_header(labeler_dids); } + /// Configures the atproto-proxy header to be applied on requests. fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { self.session_manager.configure_proxy_header(did, service_type); } diff --git a/atrium-api/src/agent/atp_agent.rs b/atrium-api/src/agent/atp_agent.rs index c6a100cb..23f03619 100644 --- a/atrium-api/src/agent/atp_agent.rs +++ b/atrium-api/src/agent/atp_agent.rs @@ -4,10 +4,9 @@ mod inner; pub mod store; use self::store::AtpSessionStore; -use super::inner::Wrapper; -use super::{Agent, InnerStore, SessionManager}; +use super::{inner::Wrapper, Agent, CloneWithProxy, Configure, InnerStore, SessionManager}; use crate::{ - client::{com::atproto::Service as AtprotoService, Service}, + client::com::atproto::Service, did_doc::DidDocument, types::{string::Did, TryFromUnknown}, }; @@ -22,12 +21,12 @@ pub type AtpSession = crate::com::atproto::server::create_session::Output; pub struct CredentialSession where S: AtpSessionStore + Send + Sync, - S::Error: std::error::Error + Send + Sync + 'static, T: XrpcClient + Send + Sync, + S::Error: std::error::Error + Send + Sync + 'static, { store: Arc>, inner: Arc>, - atproto_service: AtprotoService>, + atproto_service: Service>, } impl CredentialSession @@ -39,7 +38,7 @@ where pub fn new(xrpc: T, store: S) -> Self { let store = Arc::new(InnerStore::new(store, xrpc.base_uri())); let inner = Arc::new(inner::Client::new(Arc::clone(&store), xrpc)); - let atproto_service = AtprotoService::new(Arc::clone(&inner)); + let atproto_service = Service::new(Arc::clone(&inner)); Self { store, inner, atproto_service } } /// Start a new session with this agent. @@ -103,28 +102,6 @@ where } } } - /// Set the current endpoint. - pub fn configure_endpoint(&self, endpoint: String) { - self.inner.configure_endpoint(endpoint); - } - /// Configures the moderation services to be applied on requests. - pub fn configure_labelers_header(&self, labeler_dids: Option>) { - self.inner.configure_labelers_header(labeler_dids); - } - /// Configures the atproto-proxy header to be applied on requests. - pub fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { - self.inner.configure_proxy_header(did, service_type); - } - /// Configures the atproto-proxy header to be applied on requests. - /// - /// Returns a new client service with the proxy header configured. - pub fn api_with_proxy( - &self, - did: Did, - service_type: impl AsRef, - ) -> Service> { - Service::new(Arc::new(self.inner.clone_with_proxy(did, service_type))) - } /// Get the current session. pub async fn get_session(&self) -> Option { self.store.get().await.ok().and_then(convert::identity) @@ -191,6 +168,36 @@ where } } +impl Configure for CredentialSession +where + S: AtpSessionStore + Send + Sync, + T: XrpcClient + Send + Sync, + S::Error: std::error::Error + Send + Sync + 'static, +{ + fn configure_endpoint(&self, endpoint: String) { + self.inner.configure_endpoint(endpoint); + } + fn configure_labelers_header(&self, labeler_dids: Option>) { + self.inner.configure_labelers_header(labeler_dids); + } + fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { + self.inner.configure_proxy_header(did, service_type); + } +} + +impl CloneWithProxy for CredentialSession +where + S: AtpSessionStore + Send + Sync, + S::Error: std::error::Error + Send + Sync + 'static, + T: XrpcClient + Send + Sync, +{ + fn clone_with_proxy(&self, did: Did, service_type: impl AsRef) -> Self { + let inner = Arc::new(self.inner.clone_with_proxy(did, service_type)); + let atproto_service = Service::new(Arc::clone(&inner)); + Self { store: Arc::clone(&self.store), inner, atproto_service } + } +} + /// An ATP "Agent". /// Manages session token lifecycles and provides convenience methods. /// @@ -244,28 +251,6 @@ where ) -> Result<(), Error> { self.session_manager.resume_session(session).await } - // /// Set the current endpoint. - pub fn configure_endpoint(&self, endpoint: String) { - self.session_manager.configure_endpoint(endpoint); - } - /// Configures the moderation services to be applied on requests. - pub fn configure_labelers_header(&self, labeler_dids: Option>) { - self.session_manager.configure_labelers_header(labeler_dids); - } - /// Configures the atproto-proxy header to be applied on requests. - pub fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { - self.session_manager.configure_proxy_header(did, service_type); - } - /// Configures the atproto-proxy header to be applied on requests. - /// - /// Returns a new client service with the proxy header configured. - pub fn api_with_proxy( - &self, - did: Did, - service_type: impl AsRef, - ) -> Service> { - self.session_manager.api_with_proxy(did, service_type) - } /// Get the current session. pub async fn get_session(&self) -> Option { self.session_manager.get_session().await diff --git a/atrium-api/src/agent/atp_agent/inner.rs b/atrium-api/src/agent/atp_agent/inner.rs index a4d86013..c53d823d 100644 --- a/atrium-api/src/agent/atp_agent/inner.rs +++ b/atrium-api/src/agent/atp_agent/inner.rs @@ -1,6 +1,6 @@ use super::{AtpSession, AtpSessionStore}; use crate::{ - agent::{Configure, InnerStore, WrapperClient}, + agent::{CloneWithProxy, Configure, InnerStore, WrapperClient}, did_doc::DidDocument, types::{string::Did, TryFromUnknown}, }; @@ -35,20 +35,6 @@ where notify: Arc::new(Notify::new()), } } - pub fn configure_endpoint(&self, endpoint: String) { - *self.store.endpoint.write().expect("failed to write endpoint") = endpoint; - } - pub fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { - self.inner.configure_proxy_header(did, service_type); - } - pub fn clone_with_proxy(&self, did: Did, service_type: impl AsRef) -> Self { - let cloned = self.clone(); - cloned.inner.configure_proxy_header(did, service_type); - cloned - } - pub fn configure_labelers_header(&self, labeler_dids: Option>) { - self.inner.configure_labelers_header(labeler_dids); - } pub async fn get_labelers_header(&self) -> Option> { self.inner.atproto_accept_labelers_header().await } @@ -131,11 +117,27 @@ where } } -impl Clone for Client -where - S: AtpSessionStore + Send + Sync, - T: XrpcClient + Send + Sync, -{ +impl Configure for Client { + fn configure_endpoint(&self, endpoint: String) { + *self.store.endpoint.write().expect("failed to write endpoint") = endpoint; + } + fn configure_labelers_header(&self, labeler_dids: Option>) { + self.inner.configure_labelers_header(labeler_dids); + } + fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { + self.inner.configure_proxy_header(did, service_type); + } +} + +impl CloneWithProxy for Client { + fn clone_with_proxy(&self, did: Did, service_type: impl AsRef) -> Self { + let cloned = self.clone(); + cloned.inner.configure_proxy_header(did, service_type); + cloned + } +} + +impl Clone for Client { fn clone(&self) -> Self { Self { store: self.store.clone(), diff --git a/atrium-api/src/agent/inner.rs b/atrium-api/src/agent/inner.rs index 2eda77e2..e2cc89a0 100644 --- a/atrium-api/src/agent/inner.rs +++ b/atrium-api/src/agent/inner.rs @@ -1,4 +1,4 @@ -use super::{CloneWithProxy, SessionManager}; +use super::{CloneWithProxy, Configure, SessionManager}; use crate::types::string::Did; use atrium_xrpc::{Error, HttpClient, OutputDataOrBytes, XrpcClient, XrpcRequest}; use http::{Request, Response}; @@ -60,6 +60,21 @@ where } } +impl Configure for Wrapper +where + M: Configure, +{ + fn configure_endpoint(&self, endpoint: String) { + self.inner.configure_endpoint(endpoint); + } + fn configure_labelers_header(&self, labeler_dids: Option>) { + self.inner.configure_labelers_header(labeler_dids); + } + fn configure_proxy_header(&self, did: Did, service_type: impl AsRef) { + self.inner.configure_proxy_header(did, service_type); + } +} + impl CloneWithProxy for Wrapper where M: CloneWithProxy,