mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-20 07:28:11 -07:00
global: snapshot
This commit is contained in:
@@ -10,12 +10,14 @@ pub trait HeaderMapExtended {
|
||||
fn insert_cache_control(&mut self, value: &str);
|
||||
fn insert_cache_control_must_revalidate(&mut self);
|
||||
|
||||
fn insert_content_disposition_attachment(&mut self);
|
||||
fn insert_content_disposition_attachment(&mut self, filename: &str);
|
||||
|
||||
fn insert_content_type_application_json(&mut self);
|
||||
fn insert_content_type_text_csv(&mut self);
|
||||
fn insert_content_type_text_plain(&mut self);
|
||||
fn insert_content_type_octet_stream(&mut self);
|
||||
|
||||
fn insert_deprecation(&mut self, sunset: &'static str);
|
||||
}
|
||||
|
||||
impl HeaderMapExtended for HeaderMap {
|
||||
@@ -36,8 +38,11 @@ impl HeaderMapExtended for HeaderMap {
|
||||
self.insert_cache_control("public, max-age=1, must-revalidate");
|
||||
}
|
||||
|
||||
fn insert_content_disposition_attachment(&mut self) {
|
||||
self.insert(header::CONTENT_DISPOSITION, "attachment".parse().unwrap());
|
||||
fn insert_content_disposition_attachment(&mut self, filename: &str) {
|
||||
self.insert(
|
||||
header::CONTENT_DISPOSITION,
|
||||
format!("attachment; filename=\"{filename}\"").parse().unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
fn insert_content_type_application_json(&mut self) {
|
||||
@@ -58,4 +63,9 @@ impl HeaderMapExtended for HeaderMap {
|
||||
"application/octet-stream".parse().unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
fn insert_deprecation(&mut self, sunset: &'static str) {
|
||||
self.insert("Deprecation", "true".parse().unwrap());
|
||||
self.insert("Sunset", sunset.parse().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use axum::{http::StatusCode, response::Response};
|
||||
use brk_error::{Error, Result};
|
||||
use axum::response::Response;
|
||||
use brk_error::Result;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::extended::ResponseExtended;
|
||||
use crate::{Error, extended::ResponseExtended};
|
||||
|
||||
pub trait ResultExtended<T> {
|
||||
fn with_status(self) -> Result<T, (StatusCode, String)>;
|
||||
fn to_json_response(self, etag: &str) -> Response
|
||||
where
|
||||
T: Serialize;
|
||||
@@ -18,29 +17,13 @@ pub trait ResultExtended<T> {
|
||||
}
|
||||
|
||||
impl<T> ResultExtended<T> for Result<T> {
|
||||
fn with_status(self) -> Result<T, (StatusCode, String)> {
|
||||
self.map_err(|e| {
|
||||
(
|
||||
match e {
|
||||
Error::InvalidTxid
|
||||
| Error::InvalidNetwork
|
||||
| Error::InvalidAddress
|
||||
| Error::UnsupportedType(_) => StatusCode::BAD_REQUEST,
|
||||
Error::UnknownAddress | Error::UnknownTxid => StatusCode::NOT_FOUND,
|
||||
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
e.to_string(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn to_json_response(self, etag: &str) -> Response
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
match self.with_status() {
|
||||
match self {
|
||||
Ok(value) => Response::new_json(&value, etag),
|
||||
Err((status, message)) => Response::new_json_with(status, &message, etag),
|
||||
Err(e) => Error::from(e).into_response_with_etag(etag),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,9 +31,9 @@ impl<T> ResultExtended<T> for Result<T> {
|
||||
where
|
||||
T: AsRef<str>,
|
||||
{
|
||||
match self.with_status() {
|
||||
match self {
|
||||
Ok(value) => Response::new_text(value.as_ref(), etag),
|
||||
Err((status, message)) => Response::new_text_with(status, &message, etag),
|
||||
Err(e) => Error::from(e).into_response_with_etag(etag),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,9 +41,9 @@ impl<T> ResultExtended<T> for Result<T> {
|
||||
where
|
||||
T: Into<Vec<u8>>,
|
||||
{
|
||||
match self.with_status() {
|
||||
match self {
|
||||
Ok(value) => Response::new_bytes(value.into(), etag),
|
||||
Err((status, message)) => Response::new_bytes_with(status, message.into_bytes(), etag),
|
||||
Err(e) => Error::from(e).into_response_with_etag(etag),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ use aide::transform::{TransformOperation, TransformResponse};
|
||||
use axum::Json;
|
||||
use schemars::JsonSchema;
|
||||
|
||||
use crate::error::ErrorBody;
|
||||
|
||||
pub trait TransformResponseExtended<'t> {
|
||||
fn addresses_tag(self) -> Self;
|
||||
fn blocks_tag(self) -> Self;
|
||||
@@ -99,13 +101,13 @@ impl<'t> TransformResponseExtended<'t> for TransformOperation<'t> {
|
||||
}
|
||||
|
||||
fn bad_request(self) -> Self {
|
||||
self.response_with::<400, Json<String>, _>(|res| {
|
||||
self.response_with::<400, Json<ErrorBody>, _>(|res| {
|
||||
res.description("Invalid request parameters")
|
||||
})
|
||||
}
|
||||
|
||||
fn not_found(self) -> Self {
|
||||
self.response_with::<404, Json<String>, _>(|res| res.description("Resource not found"))
|
||||
self.response_with::<404, Json<ErrorBody>, _>(|res| res.description("Resource not found"))
|
||||
}
|
||||
|
||||
fn not_modified(self) -> Self {
|
||||
@@ -115,6 +117,8 @@ impl<'t> TransformResponseExtended<'t> for TransformOperation<'t> {
|
||||
}
|
||||
|
||||
fn server_error(self) -> Self {
|
||||
self.response_with::<500, Json<String>, _>(|res| res.description("Internal server error"))
|
||||
self.response_with::<500, Json<ErrorBody>, _>(|res| {
|
||||
res.description("Internal server error")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user