atrium_xrpc/
types.rs

1use http::header::{HeaderName, HeaderValue, InvalidHeaderValue, AUTHORIZATION, CONTENT_TYPE};
2use http::Method;
3use serde::{de::DeserializeOwned, Serialize};
4
5pub(crate) const NSID_REFRESH_SESSION: &str = "com.atproto.server.refreshSession";
6
7pub enum AuthorizationToken {
8    Bearer(String),
9    Dpop(String),
10}
11
12impl TryFrom<AuthorizationToken> for HeaderValue {
13    type Error = InvalidHeaderValue;
14
15    fn try_from(token: AuthorizationToken) -> Result<Self, Self::Error> {
16        HeaderValue::from_str(&match token {
17            AuthorizationToken::Bearer(t) => format!("Bearer {t}"),
18            AuthorizationToken::Dpop(t) => format!("DPoP {t}"),
19        })
20    }
21}
22
23/// HTTP headers which can be used in XPRC requests.
24pub enum Header {
25    ContentType,
26    Authorization,
27    AtprotoProxy,
28    AtprotoAcceptLabelers,
29}
30
31impl From<Header> for HeaderName {
32    fn from(value: Header) -> Self {
33        match value {
34            Header::ContentType => CONTENT_TYPE,
35            Header::Authorization => AUTHORIZATION,
36            Header::AtprotoProxy => HeaderName::from_static("atproto-proxy"),
37            Header::AtprotoAcceptLabelers => HeaderName::from_static("atproto-accept-labelers"),
38        }
39    }
40}
41
42/// A request which can be executed with [`XrpcClient::send_xrpc()`](crate::XrpcClient::send_xrpc).
43pub struct XrpcRequest<P, I>
44where
45    I: Serialize,
46{
47    pub method: Method,
48    pub nsid: String,
49    pub parameters: Option<P>,
50    pub input: Option<InputDataOrBytes<I>>,
51    pub encoding: Option<String>,
52}
53
54/// A type which can be used as a parameter of [`XrpcRequest`].
55///
56/// JSON serializable data or raw bytes.
57pub enum InputDataOrBytes<T>
58where
59    T: Serialize,
60{
61    Data(T),
62    Bytes(Vec<u8>),
63}
64
65/// A type which can be used as a return value of [`XrpcClient::send_xrpc()`](crate::XrpcClient::send_xrpc).
66///
67/// JSON deserializable data or raw bytes.
68pub enum OutputDataOrBytes<T>
69where
70    T: DeserializeOwned,
71{
72    Data(T),
73    Bytes(Vec<u8>),
74}