Skip to content

Commit

Permalink
Temporary fix for bsky-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Nov 8, 2024
1 parent 71f8cff commit 6734492
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 62 deletions.
32 changes: 16 additions & 16 deletions bsky-sdk/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
mod builder;
pub mod config;

pub use self::builder::BskyAgentBuilder;
pub use self::builder::BskyAtpAgentBuilder;
use self::config::Config;
use crate::error::Result;
use crate::moderation::util::interpret_label_value_definitions;
use crate::moderation::{ModerationPrefsLabeler, Moderator};
use crate::preference::{FeedViewPreferenceData, Preferences, ThreadViewPreferenceData};
use atrium_api::agent::store::MemorySessionStore;
use atrium_api::agent::{store::SessionStore, AtpAgent};
use atrium_api::agent::atp_agent::store::MemorySessionStore;
use atrium_api::agent::atp_agent::{store::AtpSessionStore, AtpAgent};
use atrium_api::app::bsky::actor::defs::PreferencesItem;
use atrium_api::types::{Object, Union};
use atrium_api::xrpc::XrpcClient;
Expand All @@ -21,8 +21,8 @@ use std::sync::Arc;

/// A Bluesky agent.
///
/// This agent is a wrapper around the [`AtpAgent`] that provides additional functionality for working with Bluesky.
/// For creating an instance of this agent, use the [`BskyAgentBuilder`].
/// This agent is a wrapper around the [`Agent`](atrium_api::agent::Agent) that provides additional functionality for working with Bluesky.
/// For creating an instance of this agent, use the [`BskyAtpAgentBuilder`].
///
/// # Example
///
Expand All @@ -40,7 +40,7 @@ use std::sync::Arc;
pub struct BskyAgent<T = ReqwestClient, S = MemorySessionStore>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
inner: Arc<AtpAgent<S, T>>,
}
Expand All @@ -49,24 +49,24 @@ where
pub struct BskyAgent<T, S = MemorySessionStore>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
inner: Arc<AtpAgent<S, T>>,
}

#[cfg_attr(docsrs, doc(cfg(feature = "default-client")))]
#[cfg(feature = "default-client")]
impl BskyAgent {
/// Create a new [`BskyAgentBuilder`] with the default client and session store.
pub fn builder() -> BskyAgentBuilder<ReqwestClient, MemorySessionStore> {
BskyAgentBuilder::default()
/// Create a new [`BskyAtpAgentBuilder`] with the default client and session store.
pub fn builder() -> BskyAtpAgentBuilder<ReqwestClient, MemorySessionStore> {
BskyAtpAgentBuilder::default()
}
}

impl<T, S> BskyAgent<T, S>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
/// Get the agent's current state as a [`Config`].
pub async fn to_config(&self) -> Config {
Expand Down Expand Up @@ -248,7 +248,7 @@ where
impl<T, S> Deref for BskyAgent<T, S>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
type Target = AtpAgent<S, T>;

Expand All @@ -260,16 +260,16 @@ where
#[cfg(test)]
mod tests {
use super::*;
use atrium_api::agent::Session;
use atrium_api::agent::atp_agent::AtpSession;

#[derive(Clone)]
struct NoopStore;

impl SessionStore for NoopStore {
async fn get_session(&self) -> Option<Session> {
impl AtpSessionStore for NoopStore {
async fn get_session(&self) -> Option<AtpSession> {
unimplemented!()
}
async fn set_session(&self, _: Session) {
async fn set_session(&self, _: AtpSession) {
unimplemented!()
}
async fn clear_session(&self) {
Expand Down
53 changes: 28 additions & 25 deletions bsky-sdk/src/agent/builder.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
use super::config::Config;
use super::BskyAgent;
use crate::error::Result;
use atrium_api::agent::store::MemorySessionStore;
use atrium_api::agent::{store::SessionStore, AtpAgent};
use atrium_api::agent::atp_agent::{
store::{AtpSessionStore, MemorySessionStore},
AtpAgent,
};
use atrium_api::xrpc::XrpcClient;
#[cfg(feature = "default-client")]
use atrium_xrpc_client::reqwest::ReqwestClient;
use std::sync::Arc;

/// A builder for creating a [`BskyAgent`].
pub struct BskyAgentBuilder<T, S = MemorySessionStore>
/// A builder for creating a [`BskyAtpAgent`].
pub struct BskyAtpAgentBuilder<T, S = MemorySessionStore>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
config: Config,
store: S,
client: T,
}

impl<T> BskyAgentBuilder<T>
impl<T> BskyAtpAgentBuilder<T>
where
T: XrpcClient + Send + Sync,
{
Expand All @@ -29,10 +31,10 @@ where
}
}

impl<T, S> BskyAgentBuilder<T, S>
impl<T, S> BskyAtpAgentBuilder<T, S>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
/// Set the configuration for the agent.
pub fn config(mut self, config: Config) -> Self {
Expand All @@ -42,20 +44,20 @@ where
/// Set the session store for the agent.
///
/// Returns a new builder with the session store set.
pub fn store<S0>(self, store: S0) -> BskyAgentBuilder<T, S0>
pub fn store<S0>(self, store: S0) -> BskyAtpAgentBuilder<T, S0>
where
S0: SessionStore + Send + Sync,
S0: AtpSessionStore + Send + Sync,
{
BskyAgentBuilder { config: self.config, store, client: self.client }
BskyAtpAgentBuilder { config: self.config, store, client: self.client }
}
/// Set the XRPC client for the agent.
///
/// Returns a new builder with the XRPC client set.
pub fn client<T0>(self, client: T0) -> BskyAgentBuilder<T0, S>
pub fn client<T0>(self, client: T0) -> BskyAtpAgentBuilder<T0, S>
where
T0: XrpcClient + Send + Sync,
{
BskyAgentBuilder { config: self.config, store: self.store, client }
BskyAtpAgentBuilder { config: self.config, store: self.store, client }
}
pub async fn build(self) -> Result<BskyAgent<T, S>> {
let agent = AtpAgent::new(self.client, self.store);
Expand Down Expand Up @@ -91,7 +93,7 @@ where

#[cfg_attr(docsrs, doc(cfg(feature = "default-client")))]
#[cfg(feature = "default-client")]
impl Default for BskyAgentBuilder<ReqwestClient, MemorySessionStore> {
impl Default for BskyAtpAgentBuilder<ReqwestClient, MemorySessionStore> {
/// Create a new builder with the default client and session store.
///
/// Default client is [`ReqwestClient`] and default session store is [`MemorySessionStore`].
Expand All @@ -103,10 +105,10 @@ impl Default for BskyAgentBuilder<ReqwestClient, MemorySessionStore> {
#[cfg(test)]
mod tests {
use super::*;
use atrium_api::agent::Session;
use atrium_api::agent::atp_agent::AtpSession;
use atrium_api::com::atproto::server::create_session::OutputData;

fn session() -> Session {
fn session() -> AtpSession {
OutputData {
access_jwt: String::new(),
active: None,
Expand All @@ -124,11 +126,11 @@ mod tests {

struct MockSessionStore;

impl SessionStore for MockSessionStore {
async fn get_session(&self) -> Option<Session> {
impl AtpSessionStore for MockSessionStore {
async fn get_session(&self) -> Option<AtpSession> {
Some(session())
}
async fn set_session(&self, _: Session) {}
async fn set_session(&self, _: AtpSession) {}
async fn clear_session(&self) {}
}

Expand All @@ -137,13 +139,13 @@ mod tests {
async fn default() -> Result<()> {
// default build
{
let agent = BskyAgentBuilder::default().build().await?;
let agent = BskyAtpAgentBuilder::default().build().await?;
assert_eq!(agent.get_endpoint().await, "https://bsky.social");
assert_eq!(agent.get_session().await, None);
}
// with store
{
let agent = BskyAgentBuilder::default().store(MockSessionStore).build().await?;
let agent = BskyAtpAgentBuilder::default().store(MockSessionStore).build().await?;
assert_eq!(agent.get_endpoint().await, "https://bsky.social");
assert_eq!(
agent.get_session().await.map(|session| session.data.handle),
Expand All @@ -152,7 +154,7 @@ mod tests {
}
// with config
{
let agent = BskyAgentBuilder::default()
let agent = BskyAtpAgentBuilder::default()
.config(Config {
endpoint: "https://example.com".to_string(),
..Default::default()
Expand All @@ -172,12 +174,13 @@ mod tests {

// default build
{
let agent = BskyAgentBuilder::new(MockClient).build().await?;
let agent = BskyAtpAgentBuilder::new(MockClient).build().await?;
assert_eq!(agent.get_endpoint().await, "https://bsky.social");
}
// with store
{
let agent = BskyAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
let agent =
BskyAtpAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
assert_eq!(agent.get_endpoint().await, "https://bsky.social");
assert_eq!(
agent.get_session().await.map(|session| session.data.handle),
Expand All @@ -186,7 +189,7 @@ mod tests {
}
// with config
{
let agent = BskyAgentBuilder::new(MockClient)
let agent = BskyAtpAgentBuilder::new(MockClient)
.config(Config {
endpoint: "https://example.com".to_string(),
..Default::default()
Expand Down
9 changes: 4 additions & 5 deletions bsky-sdk/src/agent/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//! Configuration for the [`BskyAgent`](super::BskyAgent).
mod file;

use std::future::Future;

pub use self::file::FileStore;
use crate::error::{Error, Result};
use atrium_api::agent::Session;
pub use file::FileStore;
use atrium_api::agent::atp_agent::AtpSession;
use serde::{Deserialize, Serialize};
use std::future::Future;

/// Configuration data struct for the [`BskyAgent`](super::BskyAgent).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// The base URL for the XRPC endpoint.
pub endpoint: String,
/// The session data.
pub session: Option<Session>,
pub session: Option<AtpSession>,
/// The labelers header values.
pub labelers_header: Option<Vec<String>>,
/// The proxy header for service proxying.
Expand Down
24 changes: 12 additions & 12 deletions bsky-sdk/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::future::Future;

use crate::error::{Error, Result};
use crate::BskyAgent;
use atrium_api::agent::store::SessionStore;
use atrium_api::agent::atp_agent::store::AtpSessionStore;
use atrium_api::com::atproto::repo::{
create_record, delete_record, get_record, list_records, put_record,
};
Expand All @@ -16,7 +16,7 @@ use atrium_api::xrpc::XrpcClient;
pub trait Record<T, S>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
fn list(
agent: &BskyAgent<T, S>,
Expand Down Expand Up @@ -45,7 +45,7 @@ macro_rules! record_impl {
impl<T, S> Record<T, S> for $record
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
async fn list(
agent: &BskyAgent<T, S>,
Expand Down Expand Up @@ -162,7 +162,7 @@ macro_rules! record_impl {
impl<T, S> Record<T, S> for $record_data
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
async fn list(
agent: &BskyAgent<T, S>,
Expand Down Expand Up @@ -273,9 +273,9 @@ record_impl!(
#[cfg(test)]
mod tests {
use super::*;
use crate::agent::BskyAgentBuilder;
use crate::agent::BskyAtpAgentBuilder;
use crate::tests::FAKE_CID;
use atrium_api::agent::Session;
use atrium_api::agent::atp_agent::AtpSession;
use atrium_api::com::atproto::server::create_session::OutputData;
use atrium_api::types::string::Datetime;
use atrium_api::xrpc::http::{Request, Response};
Expand Down Expand Up @@ -321,8 +321,8 @@ mod tests {

struct MockSessionStore;

impl SessionStore for MockSessionStore {
async fn get_session(&self) -> Option<Session> {
impl AtpSessionStore for MockSessionStore {
async fn get_session(&self) -> Option<AtpSession> {
Some(
OutputData {
access_jwt: String::from("access"),
Expand All @@ -339,13 +339,13 @@ mod tests {
.into(),
)
}
async fn set_session(&self, _: Session) {}
async fn set_session(&self, _: AtpSession) {}
async fn clear_session(&self) {}
}

#[tokio::test]
async fn actor_profile() -> Result<()> {
let agent = BskyAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
let agent = BskyAtpAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
// create
let output = atrium_api::app::bsky::actor::profile::RecordData {
avatar: None,
Expand Down Expand Up @@ -377,7 +377,7 @@ mod tests {

#[tokio::test]
async fn feed_post() -> Result<()> {
let agent = BskyAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
let agent = BskyAtpAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
// create
let output = atrium_api::app::bsky::feed::post::RecordData {
created_at: Datetime::now(),
Expand Down Expand Up @@ -409,7 +409,7 @@ mod tests {

#[tokio::test]
async fn graph_follow() -> Result<()> {
let agent = BskyAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
let agent = BskyAtpAgentBuilder::new(MockClient).store(MockSessionStore).build().await?;
// create
let output = atrium_api::app::bsky::graph::follow::RecordData {
created_at: Datetime::now(),
Expand Down
4 changes: 2 additions & 2 deletions bsky-sdk/src/record/agent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Record;
use crate::error::{Error, Result};
use crate::BskyAgent;
use atrium_api::agent::store::SessionStore;
use atrium_api::agent::atp_agent::store::AtpSessionStore;
use atrium_api::com::atproto::repo::{create_record, delete_record};
use atrium_api::record::KnownRecord;
use atrium_api::types::string::RecordKey;
Expand All @@ -10,7 +10,7 @@ use atrium_api::xrpc::XrpcClient;
impl<T, S> BskyAgent<T, S>
where
T: XrpcClient + Send + Sync,
S: SessionStore + Send + Sync,
S: AtpSessionStore + Send + Sync,
{
/// Create a record with various types of data.
/// For example, the Record families defined in [`KnownRecord`](atrium_api::record::KnownRecord) are supported.
Expand Down
Loading

0 comments on commit 6734492

Please sign in to comment.