Skip to content

Commit 4d17bba

Browse files
committed
atrium-api cleanup
1 parent b083c25 commit 4d17bba

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

atrium-api/src/agent/atp_agent.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub type AtpSession = crate::com::atproto::server::create_session::Output;
1717
pub struct CredentialSession<S, T>
1818
where
1919
S: MapStore<(), AtpSession> + Send + Sync,
20+
S::Error: Send + Sync + 'static,
2021
T: XrpcClient + Send + Sync,
2122
{
2223
store: Arc<inner::Store<S>>,
@@ -27,6 +28,7 @@ where
2728
impl<S, T> CredentialSession<S, T>
2829
where
2930
S: MapStore<(), AtpSession> + Send + Sync,
31+
S::Error: Send + Sync + 'static,
3032
T: XrpcClient + Send + Sync,
3133
{
3234
pub fn new(xrpc: T, store: S) -> Self {
@@ -58,7 +60,7 @@ where
5860
.into(),
5961
)
6062
.await?;
61-
self.store.set((), result.clone()).await.expect("todo");
63+
self.store.set((), result.clone()).await.map_err(|e| Error::SessionStore(Box::new(e)))?;
6264
if let Some(did_doc) = result
6365
.did_doc
6466
.as_ref()
@@ -73,17 +75,22 @@ where
7375
&self,
7476
session: AtpSession,
7577
) -> Result<(), Error<crate::com::atproto::server::get_session::Error>> {
76-
self.store.set((), session.clone()).await.expect("todo");
78+
self.store.set((), session.clone()).await.map_err(|e| Error::SessionStore(Box::new(e)))?;
7779
let result = self.api.com.atproto.server.get_session().await;
7880
match result {
7981
Ok(output) => {
8082
assert_eq!(output.data.did, session.data.did);
81-
if let Some(mut session) = self.store.get(&()).await.expect("todo") {
83+
if let Some(mut session) =
84+
self.store.get(&()).await.map_err(|e| Error::SessionStore(Box::new(e)))?
85+
{
8286
session.did_doc = output.data.did_doc.clone();
8387
session.email = output.data.email;
8488
session.email_confirmed = output.data.email_confirmed;
8589
session.handle = output.data.handle;
86-
self.store.set((), session).await.expect("todo");
90+
self.store
91+
.set((), session)
92+
.await
93+
.map_err(|e| Error::SessionStore(Box::new(e)))?;
8794
}
8895
if let Some(did_doc) = output
8996
.data
@@ -96,7 +103,7 @@ where
96103
Ok(())
97104
}
98105
Err(err) => {
99-
self.store.clear().await.expect("todo");
106+
self.store.clear().await.map_err(|e| Error::SessionStore(Box::new(e)))?;
100107
Err(err)
101108
}
102109
}
@@ -125,7 +132,7 @@ where
125132
}
126133
/// Get the current session.
127134
pub async fn get_session(&self) -> Option<AtpSession> {
128-
self.store.get(&()).await.expect("todo")
135+
self.store.get(&()).await.transpose().and_then(Result::ok)
129136
}
130137
/// Get the current endpoint.
131138
pub async fn get_endpoint(&self) -> String {
@@ -146,6 +153,7 @@ where
146153
pub struct AtpAgent<S, T>
147154
where
148155
S: MapStore<(), AtpSession> + Send + Sync,
156+
S::Error: Send + Sync + 'static,
149157
T: XrpcClient + Send + Sync,
150158
{
151159
inner: CredentialSession<S, T>,
@@ -154,6 +162,7 @@ where
154162
impl<S, T> AtpAgent<S, T>
155163
where
156164
S: MapStore<(), AtpSession> + Send + Sync,
165+
S::Error: Send + Sync + 'static,
157166
T: XrpcClient + Send + Sync,
158167
{
159168
/// Create a new agent.
@@ -165,6 +174,7 @@ where
165174
impl<S, T> Deref for AtpAgent<S, T>
166175
where
167176
S: MapStore<(), AtpSession> + Send + Sync,
177+
S::Error: Send + Sync + 'static,
168178
T: XrpcClient + Send + Sync,
169179
{
170180
type Target = CredentialSession<S, T>;
@@ -365,7 +375,11 @@ mod tests {
365375
..Default::default()
366376
};
367377
let agent = AtpAgent::new(client, MemoryMapStore::default());
368-
agent.store.set((), session_data.clone().into()).await.expect("todo");
378+
agent
379+
.store
380+
.set((), session_data.clone().into())
381+
.await
382+
.expect("set session should be succeeded");
369383
let output = agent
370384
.api
371385
.com
@@ -399,7 +413,11 @@ mod tests {
399413
..Default::default()
400414
};
401415
let agent = AtpAgent::new(client, MemoryMapStore::default());
402-
agent.store.set((), session_data.clone().into()).await.expect("todo");
416+
agent
417+
.store
418+
.set((), session_data.clone().into())
419+
.await
420+
.expect("set session should be succeeded");
403421
let output = agent
404422
.api
405423
.com
@@ -410,7 +428,12 @@ mod tests {
410428
.expect("get session should be succeeded");
411429
assert_eq!(output.did.as_str(), "did:web:example.com");
412430
assert_eq!(
413-
agent.store.get(&()).await.expect("todo").map(|session| session.data.access_jwt),
431+
agent
432+
.store
433+
.get(&())
434+
.await
435+
.expect("get session should be succeeded")
436+
.map(|session| session.data.access_jwt),
414437
Some("access".into())
415438
);
416439
}
@@ -438,7 +461,11 @@ mod tests {
438461
};
439462
let counts = Arc::clone(&client.counts);
440463
let agent = Arc::new(AtpAgent::new(client, MemoryMapStore::default()));
441-
agent.store.set((), session_data.clone().into()).await.expect("todo");
464+
agent
465+
.store
466+
.set((), session_data.clone().into())
467+
.await
468+
.expect("set session should be succeeded");
442469
let handles = (0..3).map(|_| {
443470
let agent = Arc::clone(&agent);
444471
tokio::spawn(async move { agent.api.com.atproto.server.get_session().await })
@@ -453,7 +480,12 @@ mod tests {
453480
assert_eq!(output.did.as_str(), "did:web:example.com");
454481
}
455482
assert_eq!(
456-
agent.store.get(&()).await.expect("todo").map(|session| session.data.access_jwt),
483+
agent
484+
.store
485+
.get(&())
486+
.await
487+
.expect("get session should be succeeded")
488+
.map(|session| session.data.access_jwt),
457489
Some("access".into())
458490
);
459491
assert_eq!(

atrium-api/src/agent/atp_agent/inner.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777
self.store.get_endpoint()
7878
}
7979
async fn authorization_token(&self, is_refresh: bool) -> Option<AuthorizationToken> {
80-
self.store.get(&()).await.expect("todo").map(|session| {
80+
self.store.get(&()).await.transpose().and_then(core::result::Result::ok).map(|session| {
8181
AuthorizationToken::Bearer(if is_refresh {
8282
session.data.refresh_jwt
8383
} else {
@@ -157,13 +157,13 @@ where
157157
}
158158
async fn refresh_session_inner(&self) {
159159
if let Ok(output) = self.call_refresh_session().await {
160-
if let Some(mut session) = self.store.get(&()).await.expect("todo") {
160+
if let Ok(Some(mut session)) = self.store.get(&()).await {
161161
session.access_jwt = output.data.access_jwt;
162162
session.did = output.data.did;
163163
session.did_doc = output.data.did_doc.clone();
164164
session.handle = output.data.handle;
165165
session.refresh_jwt = output.data.refresh_jwt;
166-
self.store.set((), session).await.expect("todo");
166+
let _ = self.store.set((), session).await;
167167
}
168168
if let Some(did_doc) = output
169169
.data
@@ -174,7 +174,7 @@ where
174174
self.store.update_endpoint(&did_doc);
175175
}
176176
} else {
177-
self.store.clear().await.expect("todo");
177+
let _ = self.store.clear().await;
178178
}
179179
}
180180
// same as `crate::client::com::atproto::server::Service::refresh_session()`
@@ -247,6 +247,7 @@ where
247247
impl<S, T> XrpcClient for Client<S, T>
248248
where
249249
S: MapStore<(), AtpSession> + Send + Sync,
250+
S::Error: Send + Sync + 'static,
250251
T: XrpcClient + Send + Sync,
251252
{
252253
fn base_uri(&self) -> String {

atrium-xrpc/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ where
1919
SerdeJson(#[from] serde_json::Error),
2020
#[error("serde_html_form error: {0}")]
2121
SerdeHtmlForm(#[from] serde_html_form::ser::Error),
22+
#[error("session store error: {0}")]
23+
SessionStore(Box<dyn std::error::Error + Send + Sync + 'static>),
2224
#[error("unexpected response type")]
2325
UnexpectedResponseType,
2426
}

0 commit comments

Comments
 (0)