use aide::openapi::{MediaType, ReferenceOr, StatusCode};
use aide::transform::{TransformOperation, TransformResponse};
use axum::Json;
use schemars::JsonSchema;
use crate::error::ErrorBody;
pub trait TransformResponseExtended<'t> {
fn addrs_tag(self) -> Self;
fn blocks_tag(self) -> Self;
fn mempool_tag(self) -> Self;
fn metrics_tag(self) -> Self;
fn mining_tag(self) -> Self;
fn series_tag(self) -> Self;
fn server_tag(self) -> Self;
fn transactions_tag(self) -> Self;
fn deprecated(self) -> Self;
fn ok_response<R>(self) -> Self
where
R: JsonSchema;
fn ok_response_with<R, F>(self, f: F) -> Self
where
R: JsonSchema,
F: FnOnce(TransformResponse<'_, R>) -> TransformResponse<'_, R>;
fn csv_response(self) -> Self;
fn bad_request(self) -> Self;
fn not_found(self) -> Self;
fn not_modified(self) -> Self;
fn server_error(self) -> Self;
}
impl<'t> TransformResponseExtended<'t> for TransformOperation<'t> {
fn addrs_tag(self) -> Self {
self.tag("Addresses")
}
fn blocks_tag(self) -> Self {
self.tag("Blocks")
}
fn mempool_tag(self) -> Self {
self.tag("Mempool")
}
fn metrics_tag(self) -> Self {
self.tag("Metrics")
}
fn series_tag(self) -> Self {
self.tag("Series")
}
fn mining_tag(self) -> Self {
self.tag("Mining")
}
fn server_tag(self) -> Self {
self.tag("Server")
}
fn transactions_tag(self) -> Self {
self.tag("Transactions")
}
fn ok_response<R>(self) -> Self
where
R: JsonSchema,
{
self.ok_response_with(|r: TransformResponse<'_, R>| r)
}
fn deprecated(mut self) -> Self {
self.inner_mut().deprecated = true;
self
}
fn ok_response_with<R, F>(self, f: F) -> Self
where
R: JsonSchema,
F: FnOnce(TransformResponse<'_, R>) -> TransformResponse<'_, R>,
{
self.response_with::<200, Json<R>, _>(|res| f(res.description("Successful response")))
}
fn csv_response(mut self) -> Self {
if let Some(responses) = &mut self.inner_mut().responses
&& let Some(ReferenceOr::Item(response)) =
responses.responses.get_mut(&StatusCode::Code(200))
{
response
.content
.insert("text/csv".into(), MediaType::default());
}
self
}
fn bad_request(self) -> Self {
self.response_with::<400, Json<ErrorBody>, _>(|res| {
res.description("Invalid request parameters")
})
}
fn not_found(self) -> Self {
self.response_with::<404, Json<ErrorBody>, _>(|res| res.description("Resource not found"))
}
fn not_modified(self) -> Self {
self.response_with::<304, (), _>(|res| {
res.description("Not modified - content unchanged since last request")
})
}
fn server_error(self) -> Self {
self.response_with::<500, Json<ErrorBody>, _>(|res| {
res.description("Internal server error")
})
}
}