use crate::config::advanced;
use crate::config::advanced::HttpClient;
use crate::error::Result;
use crate::http::client::HttpClientConfig;
#[cfg(feature = "experimental")]
use crate::storage::c4gh::C4GHKeys;
use http::Uri;
use reqwest_middleware::ClientWithMiddleware;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Display;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum JsonPathOrUrl {
Url(#[serde(with = "http_serde::uri")] Uri),
JsonPath(String),
}
impl Display for JsonPathOrUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
JsonPathOrUrl::Url(url) => &url.to_string(),
JsonPathOrUrl::JsonPath(url) => url.as_str(),
};
f.write_str(s)
}
}
impl Default for JsonPathOrUrl {
fn default() -> Self {
Self::JsonPath("$".to_string())
}
}
#[derive(JsonSchema, Deserialize, Serialize, Debug, Clone)]
#[serde(try_from = "advanced::json_path::JsonPath", deny_unknown_fields)]
pub struct JsonPath {
#[schemars(with = "String")]
#[serde(with = "http_serde::uri")]
resolve_from: Uri,
content_path: String,
size_path: Option<String>,
#[schemars(with = "Option<String>")]
response_path: Option<JsonPathOrUrl>,
allow_headers_backend: Vec<String>,
deny_headers_backend: Vec<String>,
allow_headers_client: Vec<String>,
deny_headers_client: Vec<String>,
#[serde(skip_serializing)]
#[schemars(skip)]
client: HttpClient,
#[cfg(feature = "experimental")]
#[serde(skip_serializing)]
keys: Option<C4GHKeys>,
#[cfg(feature = "experimental")]
forward_public_key: bool,
#[serde(skip)]
pub(crate) is_defaulted: bool,
}
impl Eq for JsonPath {}
impl PartialEq for JsonPath {
fn eq(&self, other: &Self) -> bool {
self.resolve_from == other.resolve_from
&& self.content_path == other.content_path
&& self.size_path == other.size_path
&& self.response_path == other.response_path
&& self.allow_headers_backend == other.allow_headers_backend
&& self.deny_headers_backend == other.deny_headers_backend
&& self.allow_headers_client == other.allow_headers_client
&& self.deny_headers_client == other.deny_headers_client
}
}
impl JsonPath {
pub fn new(
resolve_from: Uri,
content_path: String,
size_path: Option<String>,
response_path: Option<JsonPathOrUrl>,
allow_headers_backend: Vec<String>,
allow_headers_client: Vec<String>,
client: HttpClient,
) -> Self {
Self {
resolve_from,
content_path,
size_path,
response_path,
allow_headers_backend,
deny_headers_backend: vec![],
allow_headers_client,
deny_headers_client: vec![],
client,
#[cfg(feature = "experimental")]
keys: None,
#[cfg(feature = "experimental")]
forward_public_key: true,
is_defaulted: false,
}
}
pub fn set_deny_headers_backend(&mut self, deny_headers_backend: Vec<String>) {
self.deny_headers_backend = deny_headers_backend;
}
pub fn set_deny_headers_client(&mut self, deny_headers_client: Vec<String>) {
self.deny_headers_client = deny_headers_client;
}
pub fn resolve_from(&self) -> &Uri {
&self.resolve_from
}
pub fn content_path(&self) -> &str {
&self.content_path
}
pub fn size_path(&self) -> Option<&str> {
self.size_path.as_deref()
}
pub fn response_path(&self) -> Option<&JsonPathOrUrl> {
self.response_path.as_ref()
}
pub fn allow_headers_backend(&self) -> &[String] {
&self.allow_headers_backend
}
pub fn deny_headers_backend(&self) -> &[String] {
&self.deny_headers_backend
}
pub fn allow_headers_client(&self) -> &[String] {
&self.allow_headers_client
}
pub fn deny_headers_client(&self) -> &[String] {
&self.deny_headers_client
}
pub fn client_cloned(&mut self) -> Result<ClientWithMiddleware> {
self.client.as_inner_built().cloned()
}
pub fn inner_client_mut(&mut self) -> &mut HttpClient {
&mut self.client
}
#[cfg(feature = "experimental")]
pub fn set_keys(&mut self, keys: Option<C4GHKeys>) {
self.keys = keys;
}
#[cfg(feature = "experimental")]
pub fn keys(&self) -> Option<&C4GHKeys> {
self.keys.as_ref()
}
#[cfg(feature = "experimental")]
pub fn keys_mut(&mut self) -> &mut Option<C4GHKeys> {
&mut self.keys
}
#[cfg(feature = "experimental")]
pub fn set_forward_public_key(&mut self, forward_public_key: bool) {
self.forward_public_key = forward_public_key;
}
#[cfg(feature = "experimental")]
pub fn forward_public_key(&self) -> bool {
self.forward_public_key
}
}
impl Default for JsonPath {
fn default() -> Self {
let mut json_path = Self::new(
Default::default(),
Default::default(),
Default::default(),
Default::default(),
vec!["*".to_string()],
vec!["*".to_string()],
HttpClient::from(HttpClientConfig::default()),
);
#[cfg(feature = "experimental")]
{
json_path.set_forward_public_key(true);
}
json_path.is_defaulted = true;
json_path
}
}