Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement AtpAgent #36

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions atrium-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ serde_bytes = "0.11.9"

[dev-dependencies]
tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] }
serde_json = "1.0.96"
172 changes: 165 additions & 7 deletions atrium-api/src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! An ATP "Agent".
//! Manages session token lifecycles and provides all XRPC methods.
use crate::client::AtpServiceClient;
use async_trait::async_trait;
use atrium_xrpc::error::Error;
use atrium_xrpc::{HttpClient, XrpcClient};
use http::{Request, Response};
use std::error::Error;
use std::sync::{Arc, RwLock};

use crate::client::AtpServiceClient;

/// Type alias for the [com::atproto::server::create_session::Output](crate::com::atproto::server::create_session::Output)
pub type Session = crate::com::atproto::server::create_session::Output;

Expand All @@ -27,7 +26,7 @@ where
async fn send(
&self,
req: Request<Vec<u8>>,
) -> Result<Response<Vec<u8>>, Box<dyn Error + Send + Sync + 'static>> {
) -> Result<Response<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>> {
HttpClient::send(&self.xrpc, req).await
}
}
Expand Down Expand Up @@ -74,9 +73,168 @@ where
let api = AtpServiceClient::new(Arc::new(base));
Self { api, session }
}
pub fn set_session(&mut self, session: Session) {
if let Ok(mut lock) = self.session.write() {
*lock = Some(session);
pub fn get_session(&self) -> Option<Session> {
self.session.read().expect("read lock").clone()
}
pub async fn login(
&self,
identifier: &str,
password: &str,
) -> Result<Session, Error<crate::com::atproto::server::create_session::Error>> {
let result = self
.api
.com
.atproto
.server
.create_session(crate::com::atproto::server::create_session::Input {
identifier: identifier.into(),
password: password.into(),
})
.await?;
self.session
.write()
.expect("write lock")
.replace(result.clone());
Ok(result)
}
pub async fn resume_session(
&self,
session: Session,
) -> Result<(), Error<crate::com::atproto::server::get_session::Error>> {
self.session
.write()
.expect("write lock")
.replace(session.clone());
match self.api.com.atproto.server.get_session().await {
Ok(result) => {
assert_eq!(result.did, session.did);
self.session.write().expect("write lock").replace(Session {
email: result.email,
handle: result.handle,
..session
});
Ok(())
}
Err(err) => {
self.session.write().expect("write lock").take();
Err(err)
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

struct DummyClient {
session: Option<Session>,
}

#[async_trait]
impl HttpClient for DummyClient {
async fn send(
&self,
_req: Request<Vec<u8>>,
) -> Result<Response<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>> {
let builder =
Response::builder().header(http::header::CONTENT_TYPE, "application/json");
if let Some(session) = &self.session {
Ok(builder
.status(http::StatusCode::OK)
.body(serde_json::to_vec(&session)?)?)
} else {
Ok(builder
.status(http::StatusCode::UNAUTHORIZED)
.body(serde_json::to_vec(
&atrium_xrpc::error::ErrorResponseBody {
error: Some(String::from("AuthenticationRequired")),
message: Some(String::from("Invalid identifier or password")),
},
)?)?)
}
}
}

impl XrpcClient for DummyClient {
fn host(&self) -> &str {
"http://localhost:8080"
}
}

#[test]
fn new_agent() {
let agent = AtpAgent::new(atrium_xrpc::client::reqwest::ReqwestClient::new(
"http://localhost:8080".into(),
));
assert_eq!(agent.get_session(), None);
}

#[tokio::test]
async fn login() {
let session = Session {
access_jwt: "access".into(),
did: "did".into(),
email: None,
handle: "handle".into(),
refresh_jwt: "refresh".into(),
};
// success
{
let client = DummyClient {
session: Some(session.clone()),
};
let agent = AtpAgent::new(client);
agent.login("test", "pass").await.expect("failed to login");
assert_eq!(agent.get_session(), Some(session));
}
// failure with `createSession` error
{
let client = DummyClient { session: None };
let agent = AtpAgent::new(client);
agent
.login("test", "bad")
.await
.expect_err("should failed to login");
assert_eq!(agent.get_session(), None);
}
}

#[tokio::test]
async fn resume_session() {
let session = Session {
access_jwt: "access".into(),
did: "did".into(),
email: None,
handle: "handle".into(),
refresh_jwt: "refresh".into(),
};
// success
{
let client = DummyClient {
session: Some(session.clone()),
};
let agent = AtpAgent::new(client);
assert_eq!(agent.get_session(), None);
agent
.resume_session(Session {
email: Some(String::from("[email protected]")),
..session.clone()
})
.await
.expect("failed to resume session");
assert_eq!(agent.get_session(), Some(session.clone()));
}
// failure with `getSession` error
{
let client = DummyClient { session: None };
let agent = AtpAgent::new(client);
assert_eq!(agent.get_session(), None);
agent
.resume_session(session)
.await
.expect_err("should failed to resume session");
assert_eq!(agent.get_session(), None);
}
}
}