-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathresponse.rs
More file actions
53 lines (43 loc) · 1.76 KB
/
response.rs
File metadata and controls
53 lines (43 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use http::StatusCode;
use serde::de::DeserializeOwned;
use crate::{WorkOsError, WorkOsResult, traits::ClientResponse};
pub trait ResponseExt<'a>
where
Self: Sized,
{
/// Handles an unauthorized error from the WorkOS API by converting it into a
/// [`WorkOsError::Unauthorized`] response.
fn handle_unauthorized_error<E>(self) -> WorkOsResult<Box<dyn ClientResponse + 'a>, E>;
/// Handles a generic error from the WorkOS API by converting it into a
/// [`WorkOsError::RequestError`] response.
fn handle_generic_error<E>(self) -> WorkOsResult<Box<dyn ClientResponse + 'a>, E>;
/// Handles an unauthorized or generic error from the WorkOS API.
fn handle_unauthorized_or_generic_error<E>(
self,
) -> WorkOsResult<Box<dyn ClientResponse + 'a>, E>;
async fn json<T: DeserializeOwned, E>(self) -> WorkOsResult<T, E>;
}
impl<'a> ResponseExt<'a> for Box<dyn ClientResponse + 'a> {
fn handle_unauthorized_error<E>(self) -> WorkOsResult<Box<dyn ClientResponse + 'a>, E> {
if self.status() == StatusCode::UNAUTHORIZED {
Err(WorkOsError::Unauthorized)
} else {
Ok(self)
}
}
fn handle_generic_error<E>(self) -> WorkOsResult<Box<dyn ClientResponse + 'a>, E> {
match self.error_for_status() {
Ok(response) => Ok(response),
Err(err) => Err(WorkOsError::RequestError(err)),
}
}
fn handle_unauthorized_or_generic_error<E>(
self,
) -> WorkOsResult<Box<dyn ClientResponse + 'a>, E> {
self.handle_unauthorized_error()?.handle_generic_error()
}
async fn json<T: DeserializeOwned, E>(self) -> WorkOsResult<T, E> {
let t = self.text().await.map_err(WorkOsError::RequestError)?;
Ok(serde_json::from_str(&t)?)
}
}