Skip to content

Commit 078241f

Browse files
committed
Refactoring code
1 parent 5b04cab commit 078241f

File tree

14 files changed

+83
-129
lines changed

14 files changed

+83
-129
lines changed

atrium-api/src/agent.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ mod tests {
168168
use crate::com::atproto::server::create_session::OutputData;
169169
use crate::did_doc::{DidDocument, Service, VerificationMethod};
170170
use crate::types::TryIntoUnknown;
171-
use async_trait::async_trait;
172171
use atrium_xrpc::HttpClient;
173172
use http::{HeaderMap, HeaderName, HeaderValue, Request, Response};
174173
use std::collections::HashMap;
@@ -189,8 +188,6 @@ mod tests {
189188
headers: Arc<RwLock<Vec<HeaderMap<HeaderValue>>>>,
190189
}
191190

192-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
193-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
194191
impl HttpClient for MockClient {
195192
async fn send_http(
196193
&self,

atrium-api/src/agent/inner.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::{Session, SessionStore};
22
use crate::did_doc::DidDocument;
33
use crate::types::string::Did;
44
use crate::types::TryFromUnknown;
5-
use async_trait::async_trait;
65
use atrium_xrpc::error::{Error, Result, XrpcErrorKind};
76
use atrium_xrpc::{HttpClient, OutputDataOrBytes, XrpcClient, XrpcRequest};
87
use http::{Method, Request, Response, Uri};
@@ -51,8 +50,6 @@ impl<S, T> Clone for WrapperClient<S, T> {
5150
}
5251
}
5352

54-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
55-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
5653
impl<S, T> HttpClient for WrapperClient<S, T>
5754
where
5855
S: Send + Sync,
@@ -67,8 +64,6 @@ where
6764
}
6865
}
6966

70-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
71-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
7267
impl<S, T> XrpcClient for WrapperClient<S, T>
7368
where
7469
S: SessionStore + Send + Sync,
@@ -231,8 +226,6 @@ where
231226
}
232227
}
233228

234-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
235-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
236229
impl<S, T> HttpClient for Client<S, T>
237230
where
238231
S: Send + Sync,
@@ -247,8 +240,6 @@ where
247240
}
248241
}
249242

250-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
251-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
252243
impl<S, T> XrpcClient for Client<S, T>
253244
where
254245
S: SessionStore + Send + Sync,
@@ -321,8 +312,6 @@ impl<S> Store<S> {
321312
}
322313
}
323314

324-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
325-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
326315
impl<S> SessionStore for Store<S>
327316
where
328317
S: SessionStore + Send + Sync,

atrium-api/src/agent/store.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
mod memory;
22

3+
use std::future::Future;
4+
35
pub use self::memory::MemorySessionStore;
46
pub(crate) use super::Session;
5-
use async_trait::async_trait;
67

7-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
8-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
98
pub trait SessionStore {
109
#[must_use]
11-
async fn get_session(&self) -> Option<Session>;
10+
fn get_session(&self) -> impl Future<Output = Option<Session>> + Send;
1211
#[must_use]
13-
async fn set_session(&self, session: Session);
12+
fn set_session(&self, session: Session) -> impl Future<Output = ()> + Send;
1413
#[must_use]
15-
async fn clear_session(&self);
14+
fn clear_session(&self) -> impl Future<Output = ()> + Send;
1615
}

atrium-api/src/agent/store/memory.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::{Session, SessionStore};
2-
use async_trait::async_trait;
32
use std::sync::Arc;
43
use tokio::sync::RwLock;
54

@@ -8,8 +7,6 @@ pub struct MemorySessionStore {
87
session: Arc<RwLock<Option<Session>>>,
98
}
109

11-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
12-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
1310
impl SessionStore for MemorySessionStore {
1411
async fn get_session(&self) -> Option<Session> {
1512
self.session.read().await.clone()

atrium-xrpc-client/src/isahc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![doc = "XrpcClient implementation for [isahc]"]
2-
use async_trait::async_trait;
32
use atrium_xrpc::http::{Request, Response};
43
use atrium_xrpc::{HttpClient, XrpcClient};
54
use isahc::{AsyncReadResponseExt, HttpClient as Client};
@@ -52,7 +51,6 @@ impl IsahcClientBuilder {
5251
}
5352
}
5453

55-
#[async_trait]
5654
impl HttpClient for IsahcClient {
5755
async fn send_http(
5856
&self,

atrium-xrpc-client/src/reqwest.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![doc = "XrpcClient implementation for [reqwest]"]
2-
use async_trait::async_trait;
32
use atrium_xrpc::http::{Request, Response};
43
use atrium_xrpc::{HttpClient, XrpcClient};
54
use reqwest::Client;
@@ -48,8 +47,6 @@ impl ReqwestClientBuilder {
4847
}
4948
}
5049

51-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
52-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
5350
impl HttpClient for ReqwestClient {
5451
async fn send_http(
5552
&self,

atrium-xrpc/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod tests {
1313
use super::*;
1414
use crate::error::{XrpcError, XrpcErrorKind};
1515
use crate::{HttpClient, XrpcClient};
16-
use async_trait::async_trait;
1716
use http::{Request, Response};
1817
#[cfg(target_arch = "wasm32")]
1918
use wasm_bindgen_test::*;
@@ -24,8 +23,6 @@ mod tests {
2423
body: Vec<u8>,
2524
}
2625

27-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
28-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
2926
impl HttpClient for DummyClient {
3027
async fn send_http(
3128
&self,

atrium-xrpc/src/traits.rs

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ use crate::error::Error;
22
use crate::error::{XrpcError, XrpcErrorKind};
33
use crate::types::{Header, NSID_REFRESH_SESSION};
44
use crate::{InputDataOrBytes, OutputDataOrBytes, XrpcRequest};
5-
use async_trait::async_trait;
65
use http::{Method, Request, Response};
76
use serde::{de::DeserializeOwned, Serialize};
87
use std::fmt::Debug;
8+
use std::future::Future;
99

1010
/// An abstract HTTP client.
11-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
12-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
1311
pub trait HttpClient {
1412
/// Send an HTTP request and return the response.
15-
async fn send_http(
13+
fn send_http(
1614
&self,
1715
request: Request<Vec<u8>>,
18-
) -> core::result::Result<Response<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>>;
16+
) -> impl Future<Output = core::result::Result<Response<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>>> + Send;
1917
}
2018

2119
type XrpcResult<O, E> = core::result::Result<OutputDataOrBytes<O>, self::Error<E>>;
@@ -24,87 +22,88 @@ type XrpcResult<O, E> = core::result::Result<OutputDataOrBytes<O>, self::Error<E
2422
///
2523
/// [`send_xrpc()`](XrpcClient::send_xrpc) method has a default implementation,
2624
/// which wraps the [`HttpClient::send_http()`]` method to handle input and output as an XRPC Request.
27-
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
28-
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
2925
pub trait XrpcClient: HttpClient {
3026
/// The base URI of the XRPC server.
3127
fn base_uri(&self) -> String;
3228
/// Get the authentication token to use `Authorization` header.
3329
#[allow(unused_variables)]
34-
async fn authentication_token(&self, is_refresh: bool) -> Option<String> {
35-
None
30+
fn authentication_token(&self, is_refresh: bool) -> impl Future<Output = Option<String>> + Send {
31+
async { None }
3632
}
3733
/// Get the `atproto-proxy` header.
38-
async fn atproto_proxy_header(&self) -> Option<String> {
39-
None
34+
fn atproto_proxy_header(&self) -> impl Future<Output = Option<String>> + Send {
35+
async { None }
4036
}
4137
/// Get the `atproto-accept-labelers` header.
42-
async fn atproto_accept_labelers_header(&self) -> Option<Vec<String>> {
43-
None
38+
fn atproto_accept_labelers_header(&self) -> impl Future<Output = Option<Vec<String>>> + Send {
39+
async { None }
4440
}
4541
/// Send an XRPC request and return the response.
46-
async fn send_xrpc<P, I, O, E>(&self, request: &XrpcRequest<P, I>) -> XrpcResult<O, E>
42+
fn send_xrpc<P, I, O, E>(&self, request: &XrpcRequest<P, I>) -> impl Future<Output = XrpcResult<O, E>> + Send
4743
where
4844
P: Serialize + Send + Sync,
4945
I: Serialize + Send + Sync,
5046
O: DeserializeOwned + Send + Sync,
5147
E: DeserializeOwned + Send + Sync + Debug,
48+
Self: Sync,
5249
{
53-
let mut uri = format!("{}/xrpc/{}", self.base_uri(), request.nsid);
54-
// Query parameters
55-
if let Some(p) = &request.parameters {
56-
serde_html_form::to_string(p).map(|qs| {
57-
uri += "?";
58-
uri += &qs;
59-
})?;
60-
};
61-
let mut builder = Request::builder().method(&request.method).uri(&uri);
62-
// Headers
63-
if let Some(encoding) = &request.encoding {
64-
builder = builder.header(Header::ContentType, encoding);
65-
}
66-
if let Some(token) = self
67-
.authentication_token(
68-
request.method == Method::POST && request.nsid == NSID_REFRESH_SESSION,
69-
)
70-
.await
71-
{
72-
builder = builder.header(Header::Authorization, format!("Bearer {}", token));
73-
}
74-
if let Some(proxy) = self.atproto_proxy_header().await {
75-
builder = builder.header(Header::AtprotoProxy, proxy);
76-
}
77-
if let Some(accept_labelers) = self.atproto_accept_labelers_header().await {
78-
builder = builder.header(Header::AtprotoAcceptLabelers, accept_labelers.join(", "));
79-
}
80-
// Body
81-
let body = if let Some(input) = &request.input {
82-
match input {
83-
InputDataOrBytes::Data(data) => serde_json::to_vec(&data)?,
84-
InputDataOrBytes::Bytes(bytes) => bytes.clone(),
50+
async {
51+
let mut uri = format!("{}/xrpc/{}", self.base_uri(), request.nsid);
52+
// Query parameters
53+
if let Some(p) = &request.parameters {
54+
serde_html_form::to_string(p).map(|qs| {
55+
uri += "?";
56+
uri += &qs;
57+
})?;
58+
};
59+
let mut builder = Request::builder().method(&request.method).uri(&uri);
60+
// Headers
61+
if let Some(encoding) = &request.encoding {
62+
builder = builder.header(Header::ContentType, encoding);
8563
}
86-
} else {
87-
Vec::new()
88-
};
89-
// Send
90-
let (parts, body) =
91-
self.send_http(builder.body(body)?).await.map_err(Error::HttpClient)?.into_parts();
92-
if parts.status.is_success() {
93-
if parts
94-
.headers
95-
.get(http::header::CONTENT_TYPE)
96-
.and_then(|value| value.to_str().ok())
97-
.map_or(false, |content_type| content_type.starts_with("application/json"))
64+
if let Some(token) = self
65+
.authentication_token(
66+
request.method == Method::POST && request.nsid == NSID_REFRESH_SESSION,
67+
)
68+
.await
9869
{
99-
Ok(OutputDataOrBytes::Data(serde_json::from_slice(&body)?))
70+
builder = builder.header(Header::Authorization, format!("Bearer {}", token));
71+
}
72+
if let Some(proxy) = self.atproto_proxy_header().await {
73+
builder = builder.header(Header::AtprotoProxy, proxy);
74+
}
75+
if let Some(accept_labelers) = self.atproto_accept_labelers_header().await {
76+
builder = builder.header(Header::AtprotoAcceptLabelers, accept_labelers.join(", "));
77+
}
78+
// Body
79+
let body = if let Some(input) = &request.input {
80+
match input {
81+
InputDataOrBytes::Data(data) => serde_json::to_vec(&data)?,
82+
InputDataOrBytes::Bytes(bytes) => bytes.clone(),
83+
}
84+
} else {
85+
Vec::new()
86+
};
87+
// Send
88+
let (parts, body) =
89+
self.send_http(builder.body(body)?).await.map_err(Error::HttpClient)?.into_parts();
90+
if parts.status.is_success() {
91+
if parts
92+
.headers
93+
.get(http::header::CONTENT_TYPE)
94+
.and_then(|value| value.to_str().ok())
95+
.map_or(false, |content_type| content_type.starts_with("application/json"))
96+
{
97+
Ok(OutputDataOrBytes::Data(serde_json::from_slice(&body)?))
98+
} else {
99+
Ok(OutputDataOrBytes::Bytes(body))
100+
}
100101
} else {
101-
Ok(OutputDataOrBytes::Bytes(body))
102+
Err(Error::XrpcResponse(XrpcError {
103+
status: parts.status,
104+
error: serde_json::from_slice::<XrpcErrorKind<E>>(&body).ok(),
105+
}))
102106
}
103-
} else {
104-
Err(Error::XrpcResponse(XrpcError {
105-
status: parts.status,
106-
error: serde_json::from_slice::<XrpcErrorKind<E>>(&body).ok(),
107-
}))
108107
}
109108
}
110109
}

bsky-sdk/src/agent.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,11 @@ where
260260
#[cfg(test)]
261261
mod tests {
262262
use super::*;
263-
use async_trait::async_trait;
264263
use atrium_api::agent::Session;
265264

266265
#[derive(Clone)]
267266
struct NoopStore;
268267

269-
#[async_trait]
270268
impl SessionStore for NoopStore {
271269
async fn get_session(&self) -> Option<Session> {
272270
unimplemented!()

bsky-sdk/src/agent/builder.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ impl Default for BskyAgentBuilder<ReqwestClient, MemorySessionStore> {
103103
#[cfg(test)]
104104
mod tests {
105105
use super::*;
106-
use async_trait::async_trait;
107106
use atrium_api::agent::Session;
108107
use atrium_api::com::atproto::server::create_session::OutputData;
109108

@@ -125,7 +124,6 @@ mod tests {
125124

126125
struct MockSessionStore;
127126

128-
#[async_trait]
129127
impl SessionStore for MockSessionStore {
130128
async fn get_session(&self) -> Option<Session> {
131129
Some(session())

bsky-sdk/src/agent/config.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Configuration for the [`BskyAgent`](super::BskyAgent).
22
mod file;
33

4+
use std::future::Future;
5+
46
use crate::error::{Error, Result};
5-
use async_trait::async_trait;
67
use atrium_api::agent::Session;
78
pub use file::FileStore;
89
use serde::{Deserialize, Serialize};
@@ -46,20 +47,18 @@ impl Default for Config {
4647
}
4748

4849
/// The trait for loading configuration data.
49-
#[async_trait]
5050
pub trait Loader {
5151
/// Loads the configuration data.
52-
async fn load(
52+
fn load(
5353
&self,
54-
) -> core::result::Result<Config, Box<dyn std::error::Error + Send + Sync + 'static>>;
54+
) -> impl Future<Output = core::result::Result<Config, Box<dyn std::error::Error + Send + Sync + 'static>>> + Send;
5555
}
5656

5757
/// The trait for saving configuration data.
58-
#[async_trait]
5958
pub trait Saver {
6059
/// Saves the configuration data.
61-
async fn save(
60+
fn save(
6261
&self,
6362
config: &Config,
64-
) -> core::result::Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
63+
) -> impl Future<Output = core::result::Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>> + Send;
6564
}

0 commit comments

Comments
 (0)