move src/server/formatter.rs to proxmox-rest-server crate
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
committed by
Thomas Lamprecht
parent
d4d49f7325
commit
1b552c109d
@ -54,6 +54,7 @@ use pbs_tools::blocking::WrappedReaderStream;
|
||||
use pbs_tools::stream::{AsyncReaderStream, AsyncChannelWriter};
|
||||
use pbs_tools::json::{required_integer_param, required_string_param};
|
||||
use pbs_config::CachedUserInfo;
|
||||
use proxmox_rest_server::formatter;
|
||||
|
||||
use crate::api2::node::rrd::create_value_from_rrd;
|
||||
use crate::backup::{
|
||||
@ -1326,7 +1327,7 @@ pub fn upload_backup_log(
|
||||
replace_file(&path, blob.raw_data(), CreateOptions::new())?;
|
||||
|
||||
// fixme: use correct formatter
|
||||
Ok(crate::server::formatter::json_response(Ok(Value::Null)))
|
||||
Ok(formatter::json_response(Ok(Value::Null)))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,10 @@ use pbs_datastore::backup_info::{BackupDir, BackupInfo};
|
||||
use pbs_datastore::dynamic_index::DynamicIndexWriter;
|
||||
use pbs_datastore::fixed_index::FixedIndexWriter;
|
||||
use pbs_api_types::Authid;
|
||||
use proxmox_rest_server::formatter::*;
|
||||
|
||||
use crate::backup::{verify_backup_dir_with_lock, DataStore};
|
||||
use crate::server::WorkerTask;
|
||||
use crate::server::formatter::*;
|
||||
use hyper::{Body, Response};
|
||||
|
||||
#[derive(Copy, Clone, Serialize)]
|
||||
|
@ -7,9 +7,9 @@ use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
|
||||
|
||||
use pbs_datastore::backup_info::BackupDir;
|
||||
use pbs_api_types::Authid;
|
||||
use proxmox_rest_server::formatter::*;
|
||||
|
||||
use crate::backup::DataStore;
|
||||
use crate::server::formatter::*;
|
||||
use crate::server::WorkerTask;
|
||||
|
||||
//use proxmox::tools;
|
||||
|
@ -1,126 +0,0 @@
|
||||
use anyhow::{Error};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use hyper::{Body, Response, StatusCode};
|
||||
use hyper::header;
|
||||
|
||||
use proxmox::api::{HttpError, RpcEnvironment};
|
||||
|
||||
/// Extension to set error message for server side logging
|
||||
pub struct ErrorMessageExtension(pub String);
|
||||
|
||||
pub struct OutputFormatter {
|
||||
|
||||
pub format_data: fn(data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body>,
|
||||
|
||||
pub format_error: fn(err: Error) -> Response<Body>,
|
||||
}
|
||||
|
||||
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
|
||||
|
||||
pub fn json_response(result: Result<Value, Error>) -> Response<Body> {
|
||||
match result {
|
||||
Ok(data) => json_data_response(data),
|
||||
Err(err) => json_error_response(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn json_data_response(data: Value) -> Response<Body> {
|
||||
|
||||
let json_str = data.to_string();
|
||||
|
||||
let raw = json_str.into_bytes();
|
||||
|
||||
let mut response = Response::new(raw.into());
|
||||
response.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment)
|
||||
{
|
||||
let attributes = match rpcenv.result_attrib().as_object() {
|
||||
Some(attr) => attr,
|
||||
None => return,
|
||||
};
|
||||
|
||||
for (key, value) in attributes {
|
||||
result[key] = value.clone();
|
||||
}
|
||||
}
|
||||
|
||||
fn json_format_data(data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
|
||||
|
||||
let mut result = json!({
|
||||
"data": data
|
||||
});
|
||||
|
||||
add_result_attributes(&mut result, rpcenv);
|
||||
|
||||
json_data_response(result)
|
||||
}
|
||||
|
||||
pub fn json_error_response(err: Error) -> Response<Body> {
|
||||
|
||||
let mut response = if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
||||
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
||||
*resp.status_mut() = apierr.code;
|
||||
resp
|
||||
} else {
|
||||
let mut resp = Response::new(Body::from(err.to_string()));
|
||||
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
||||
resp
|
||||
};
|
||||
|
||||
response.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
|
||||
|
||||
response.extensions_mut().insert(ErrorMessageExtension(err.to_string()));
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
|
||||
format_data: json_format_data,
|
||||
format_error: json_error_response,
|
||||
};
|
||||
|
||||
fn extjs_format_data(data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
|
||||
|
||||
let mut result = json!({
|
||||
"data": data,
|
||||
"success": true
|
||||
});
|
||||
|
||||
add_result_attributes(&mut result, rpcenv);
|
||||
|
||||
json_data_response(result)
|
||||
}
|
||||
|
||||
fn extjs_format_error(err: Error) -> Response<Body> {
|
||||
|
||||
let mut errors = vec![];
|
||||
|
||||
let message = err.to_string();
|
||||
errors.push(&message);
|
||||
|
||||
let result = json!({
|
||||
"message": message,
|
||||
"errors": errors,
|
||||
"success": false
|
||||
});
|
||||
|
||||
let mut response = json_data_response(result);
|
||||
|
||||
response.extensions_mut().insert(ErrorMessageExtension(message));
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter {
|
||||
format_data: extjs_format_data,
|
||||
format_error: extjs_format_error,
|
||||
};
|
@ -11,8 +11,9 @@ use hyper::{Body, Request, Response, StatusCode};
|
||||
use proxmox::api::{ApiResponseFuture, HttpError, Router, RpcEnvironment};
|
||||
use proxmox::http_err;
|
||||
|
||||
use proxmox_rest_server::formatter::*;
|
||||
|
||||
use crate::tools;
|
||||
use crate::server::formatter::*;
|
||||
use crate::server::WorkerTask;
|
||||
|
||||
/// Hyper Service implementation to handle stateful H2 connections.
|
||||
|
@ -55,8 +55,6 @@ pub use worker_task::*;
|
||||
mod h2service;
|
||||
pub use h2service::*;
|
||||
|
||||
pub mod formatter;
|
||||
|
||||
#[macro_use]
|
||||
pub mod rest;
|
||||
|
||||
|
@ -35,8 +35,7 @@ use pbs_tools::compression::{DeflateEncoder, Level};
|
||||
use pbs_tools::stream::AsyncReaderStream;
|
||||
use pbs_api_types::{Authid, Userid};
|
||||
use proxmox_rest_server::{ApiConfig, FileLogger, FileLogOptions, AuthError, RestEnvironment};
|
||||
|
||||
use super::formatter::*;
|
||||
use proxmox_rest_server::formatter::*;
|
||||
|
||||
use crate::auth_helpers::*;
|
||||
use pbs_config::CachedUserInfo;
|
||||
|
Reference in New Issue
Block a user