2025-07-02 10:36:30 -07:00
|
|
|
use crate::Endpoint;
|
2025-04-24 14:45:53 -07:00
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
pin::Pin,
|
|
|
|
|
future::Future,
|
|
|
|
|
};
|
2025-07-02 10:36:30 -07:00
|
|
|
use atproto::types::Nsid;
|
2025-04-23 14:44:26 -07:00
|
|
|
use axum::{
|
2025-04-24 14:45:53 -07:00
|
|
|
extract::{
|
|
|
|
|
Json,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
FromRequest,
|
|
|
|
|
FromRequestParts,
|
|
|
|
|
rejection::QueryRejection,
|
|
|
|
|
},
|
|
|
|
|
body::Bytes,
|
2025-04-23 14:44:26 -07:00
|
|
|
routing::{
|
|
|
|
|
get,
|
2025-04-24 14:45:53 -07:00
|
|
|
post,
|
2025-04-23 14:44:26 -07:00
|
|
|
method_routing::MethodRouter,
|
|
|
|
|
},
|
2025-04-24 14:45:53 -07:00
|
|
|
http::{
|
|
|
|
|
StatusCode,
|
|
|
|
|
request::Parts,
|
|
|
|
|
},
|
2025-04-23 14:44:26 -07:00
|
|
|
Router as axumRouter,
|
|
|
|
|
};
|
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
|
|
2025-04-25 13:52:02 -07:00
|
|
|
enum Path {
|
|
|
|
|
Nsid(Nsid),
|
2025-04-23 14:44:26 -07:00
|
|
|
NotImplemented,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct XrpcEndpoint {
|
2025-04-25 13:52:02 -07:00
|
|
|
path: Path,
|
2025-04-23 14:44:26 -07:00
|
|
|
resolver: MethodRouter,
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-24 14:45:53 -07:00
|
|
|
pub type Response = (StatusCode, Json<Value>);
|
|
|
|
|
pub fn error(code: StatusCode, error: &str, message: &str) -> Response {
|
|
|
|
|
(
|
|
|
|
|
code,
|
|
|
|
|
Json(json!({
|
|
|
|
|
"error": error,
|
|
|
|
|
"message": message
|
|
|
|
|
}))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
pub fn response(code: StatusCode, message: &str) -> Response {
|
|
|
|
|
error(code, "", message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct QueryInput {
|
|
|
|
|
parameters: HashMap<String, String>,
|
|
|
|
|
}
|
|
|
|
|
impl<S> FromRequestParts<S> for QueryInput
|
|
|
|
|
where
|
|
|
|
|
S: Send + Sync,
|
|
|
|
|
{
|
|
|
|
|
type Rejection = Response;
|
|
|
|
|
|
|
|
|
|
async fn from_request_parts(parts: &mut Parts, _state: &S)
|
|
|
|
|
-> Result<Self, Self::Rejection> {
|
|
|
|
|
let query_params: Result<Query<HashMap<String, String>>, QueryRejection> = Query::try_from_uri(&parts.uri);
|
|
|
|
|
match query_params {
|
|
|
|
|
Ok(p) => Ok(QueryInput { parameters: p.0 }),
|
|
|
|
|
Err(e) => Err(error(StatusCode::BAD_REQUEST, "Bad Parameters", &e.body_text())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub struct ProcedureInput {
|
|
|
|
|
parameters: HashMap<String, String>,
|
|
|
|
|
input: Json<Value>,
|
|
|
|
|
}
|
|
|
|
|
impl<S> FromRequest<S> for ProcedureInput
|
|
|
|
|
where
|
|
|
|
|
Bytes: FromRequest<S>,
|
|
|
|
|
S: Send + Sync,
|
|
|
|
|
{
|
|
|
|
|
type Rejection = Response;
|
|
|
|
|
|
|
|
|
|
async fn from_request(req: Request, state: &S)
|
|
|
|
|
-> Result<Self, Self::Rejection> {
|
|
|
|
|
let query_params: Result<Query<HashMap<String, String>>, QueryRejection> = Query::try_from_uri(req.uri());
|
|
|
|
|
let parameters = match query_params {
|
|
|
|
|
Ok(p) => p.0,
|
|
|
|
|
Err(e) => return Err(error(StatusCode::BAD_REQUEST, "Bad Parameters", &e.body_text())),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let json_value = Json::<Value>::from_request(req, state).await;
|
|
|
|
|
let input: Json<Value> = match json_value {
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
Err(e) => return Err(error(StatusCode::BAD_REQUEST, "Bad Parameters", &e.body_text())),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(ProcedureInput { parameters, input })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait XrpcHandler<Input>: Send + Sync + 'static {
|
|
|
|
|
fn call(&self, input: Input)
|
|
|
|
|
-> Pin<Box<dyn Future<Output = Response> + Send>>;
|
|
|
|
|
}
|
|
|
|
|
impl<F, Fut> XrpcHandler<QueryInput> for F
|
|
|
|
|
where
|
|
|
|
|
F: Fn(QueryInput) -> Fut + Send + Sync + 'static,
|
|
|
|
|
Fut: Future<Output = Response> + Send + 'static,
|
|
|
|
|
{
|
|
|
|
|
fn call(&self, input: QueryInput)
|
|
|
|
|
-> Pin<Box<dyn Future<Output = Response>+ Send>> {
|
|
|
|
|
Box::pin((self)(input))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl<F, Fut> XrpcHandler<ProcedureInput> for F
|
|
|
|
|
where
|
|
|
|
|
F: Fn(ProcedureInput) -> Fut + Send + Sync + 'static,
|
|
|
|
|
Fut: Future<Output = Response> + Send + 'static,
|
|
|
|
|
{
|
|
|
|
|
fn call(&self, input: ProcedureInput)
|
|
|
|
|
-> Pin<Box<dyn Future<Output = Response>+ Send>> {
|
|
|
|
|
Box::pin((self)(input))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-23 14:44:26 -07:00
|
|
|
impl XrpcEndpoint {
|
2025-04-25 13:52:02 -07:00
|
|
|
pub fn new_query<Q>(nsid: Nsid, query: Q) -> Self
|
2025-04-24 14:45:53 -07:00
|
|
|
where
|
|
|
|
|
Q: XrpcHandler<QueryInput> + Clone
|
|
|
|
|
{
|
|
|
|
|
XrpcEndpoint {
|
2025-04-25 13:52:02 -07:00
|
|
|
path: Path::Nsid(nsid),
|
2025-04-24 14:45:53 -07:00
|
|
|
resolver: get(async move | mut parts: Parts | -> Response {
|
|
|
|
|
match QueryInput::from_request_parts(&mut parts, &()).await {
|
|
|
|
|
Ok(qi) => query.call(qi).await,
|
|
|
|
|
Err(e) => e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 13:52:02 -07:00
|
|
|
pub fn new_procedure<P>(nsid: Nsid, procedure: P) -> Self
|
2025-04-24 14:45:53 -07:00
|
|
|
where
|
|
|
|
|
P: XrpcHandler<ProcedureInput> + Clone
|
|
|
|
|
{
|
|
|
|
|
XrpcEndpoint {
|
2025-04-25 13:52:02 -07:00
|
|
|
path: Path::Nsid(nsid),
|
2025-04-24 14:45:53 -07:00
|
|
|
resolver: post(async move | req: Request | -> Response {
|
|
|
|
|
match ProcedureInput::from_request(req, &()).await {
|
|
|
|
|
Ok(pi) => procedure.call(pi).await,
|
|
|
|
|
Err(e) => e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-23 14:44:26 -07:00
|
|
|
|
2025-04-24 14:45:53 -07:00
|
|
|
pub fn not_implemented() -> Self {
|
|
|
|
|
let resolver = (
|
2025-04-23 14:44:26 -07:00
|
|
|
StatusCode::NOT_IMPLEMENTED,
|
|
|
|
|
Json(json!({
|
|
|
|
|
"error": "MethodNotImplemented",
|
|
|
|
|
"message": "Method Not Implemented"
|
|
|
|
|
}))
|
2025-04-24 14:45:53 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
XrpcEndpoint {
|
2025-04-25 13:52:02 -07:00
|
|
|
path: Path::NotImplemented,
|
2025-04-24 14:45:53 -07:00
|
|
|
resolver: get(resolver.clone()).post(resolver),
|
|
|
|
|
}
|
2025-04-23 14:44:26 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 10:36:30 -07:00
|
|
|
impl Endpoint for XrpcEndpoint {
|
|
|
|
|
fn add_to_router(self, router: axumRouter) -> axumRouter {
|
|
|
|
|
let path = match self.path {
|
|
|
|
|
Path::Nsid(nsid) => &("/xrpc/".to_owned() + nsid.as_str()),
|
|
|
|
|
Path::NotImplemented => "/xrpc/{*nsid}",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
router.route(path, self.resolver)
|
|
|
|
|
}
|
|
|
|
|
}
|