Skip to content

Commit

Permalink
Update AtpAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Jan 4, 2025
1 parent d987bb8 commit 8810bc7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 71 deletions.
6 changes: 6 additions & 0 deletions atrium-api/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ impl<M> Agent<M>
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,
Expand All @@ -91,12 +94,15 @@ impl<M> Configure for Agent<M>
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<Vec<(Did, bool)>>) {
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<str>) {
self.session_manager.configure_proxy_header(did, service_type);
}
Expand Down
85 changes: 35 additions & 50 deletions atrium-api/src/agent/atp_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -22,12 +21,12 @@ pub type AtpSession = crate::com::atproto::server::create_session::Output;
pub struct CredentialSession<S, T>
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<InnerStore<S, AtpSession>>,
inner: Arc<inner::Client<S, T>>,
atproto_service: AtprotoService<inner::Client<S, T>>,
atproto_service: Service<inner::Client<S, T>>,
}

impl<S, T> CredentialSession<S, T>
Expand All @@ -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.
Expand Down Expand Up @@ -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<Vec<(Did, bool)>>) {
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<str>) {
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<str>,
) -> Service<inner::Client<S, T>> {
Service::new(Arc::new(self.inner.clone_with_proxy(did, service_type)))
}
/// Get the current session.
pub async fn get_session(&self) -> Option<AtpSession> {
self.store.get().await.ok().and_then(convert::identity)
Expand Down Expand Up @@ -191,6 +168,36 @@ where
}
}

impl<S, T> Configure for CredentialSession<S, T>
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<Vec<(Did, bool)>>) {
self.inner.configure_labelers_header(labeler_dids);
}
fn configure_proxy_header(&self, did: Did, service_type: impl AsRef<str>) {
self.inner.configure_proxy_header(did, service_type);
}
}

impl<S, T> CloneWithProxy for CredentialSession<S, T>
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<str>) -> 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.
///
Expand Down Expand Up @@ -244,28 +251,6 @@ where
) -> Result<(), Error<crate::com::atproto::server::get_session::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<Vec<(Did, bool)>>) {
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<str>) {
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<str>,
) -> Service<inner::Client<S, T>> {
self.session_manager.api_with_proxy(did, service_type)
}
/// Get the current session.
pub async fn get_session(&self) -> Option<AtpSession> {
self.session_manager.get_session().await
Expand Down
42 changes: 22 additions & 20 deletions atrium-api/src/agent/atp_agent/inner.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -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<str>) {
self.inner.configure_proxy_header(did, service_type);
}
pub fn clone_with_proxy(&self, did: Did, service_type: impl AsRef<str>) -> Self {
let cloned = self.clone();
cloned.inner.configure_proxy_header(did, service_type);
cloned
}
pub fn configure_labelers_header(&self, labeler_dids: Option<Vec<(Did, bool)>>) {
self.inner.configure_labelers_header(labeler_dids);
}
pub async fn get_labelers_header(&self) -> Option<Vec<String>> {
self.inner.atproto_accept_labelers_header().await
}
Expand Down Expand Up @@ -131,11 +117,27 @@ where
}
}

impl<S, T> Clone for Client<S, T>
where
S: AtpSessionStore + Send + Sync,
T: XrpcClient + Send + Sync,
{
impl<S, T> Configure for Client<S, T> {
fn configure_endpoint(&self, endpoint: String) {
*self.store.endpoint.write().expect("failed to write endpoint") = endpoint;
}
fn configure_labelers_header(&self, labeler_dids: Option<Vec<(Did, bool)>>) {
self.inner.configure_labelers_header(labeler_dids);
}
fn configure_proxy_header(&self, did: Did, service_type: impl AsRef<str>) {
self.inner.configure_proxy_header(did, service_type);
}
}

impl<S, T> CloneWithProxy for Client<S, T> {
fn clone_with_proxy(&self, did: Did, service_type: impl AsRef<str>) -> Self {
let cloned = self.clone();
cloned.inner.configure_proxy_header(did, service_type);
cloned
}
}

impl<S, T> Clone for Client<S, T> {
fn clone(&self) -> Self {
Self {
store: self.store.clone(),
Expand Down
17 changes: 16 additions & 1 deletion atrium-api/src/agent/inner.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -60,6 +60,21 @@ where
}
}

impl<M> Configure for Wrapper<M>
where
M: Configure,
{
fn configure_endpoint(&self, endpoint: String) {
self.inner.configure_endpoint(endpoint);
}
fn configure_labelers_header(&self, labeler_dids: Option<Vec<(Did, bool)>>) {
self.inner.configure_labelers_header(labeler_dids);
}
fn configure_proxy_header(&self, did: Did, service_type: impl AsRef<str>) {
self.inner.configure_proxy_header(did, service_type);
}
}

impl<M> CloneWithProxy for Wrapper<M>
where
M: CloneWithProxy,
Expand Down

0 comments on commit 8810bc7

Please sign in to comment.