Skip to content

Commit

Permalink
allow the endpoint to return json_preview
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Mar 4, 2024
1 parent 9117f18 commit f65f172
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl TestConfig {
self.limit_posts,
!self.compact_output,
self.base.clone(),
false,
)
}
}
Expand Down
44 changes: 32 additions & 12 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use std::time::Duration;

use axum::body::Body;
use axum::response::IntoResponse;
use axum::Json;
use axum_macros::FromRequestParts;
use http::header::HOST;
use http::StatusCode;
use mime::Mime;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tower::Service;
use url::Url;

Expand Down Expand Up @@ -87,6 +89,8 @@ pub struct EndpointParam {
pretty_print: bool,
/// The url base of the feed, used for resolving relative urls
base: Option<Url>,
/// Include a json preview of the feed in the response
include_json_preview: bool,
}

impl EndpointParam {
Expand All @@ -96,13 +100,15 @@ impl EndpointParam {
limit_posts: Option<usize>,
pretty_print: bool,
base: Option<Url>,
include_json_preview: bool,
) -> Self {
Self {
source,
limit_filters,
limit_posts,
pretty_print,
base,
include_json_preview,
}
}

Expand All @@ -113,6 +119,7 @@ impl EndpointParam {
limit_posts: Self::parse_limit_posts(req),
pretty_print: Self::parse_pretty_print(req),
base: Self::get_base(req),
include_json_preview: Self::parse_include_json_preview(req),
}
}

Expand All @@ -134,6 +141,12 @@ impl EndpointParam {
.unwrap_or(false)
}

fn parse_include_json_preview(req: &Request) -> bool {
Self::get_query(req, "include_json_preview")
.map(|x| x == "1" || x == "true")
.unwrap_or(false)
}

fn get_base(req: &Request) -> Option<Url> {
let host = req
.headers()
Expand Down Expand Up @@ -163,40 +176,47 @@ impl EndpointParam {

#[derive(Clone)]
pub struct EndpointOutcome {
feed_xml: String,
content: String,
content_type: Mime,
json_preview: Option<String>,
}

impl EndpointOutcome {
pub fn new(feed_xml: String, content_type: &str) -> Self {
let content_type = content_type.parse().expect("invalid content_type");

Self {
feed_xml,
content: feed_xml,
content_type,
json_preview: None,
}
}

pub fn prettify(&mut self) {
if let Ok(xml) = self.feed_xml.parse::<xmlem::Document>() {
self.feed_xml = xml.to_string_pretty();
if let Ok(xml) = self.content.parse::<xmlem::Document>() {
self.content = xml.to_string_pretty();
}
}

pub fn feed_xml(&self) -> &str {
&self.feed_xml
&self.content
}
}

impl IntoResponse for EndpointOutcome {
fn into_response(self) -> axum::response::Response {
let mut resp = Response::new(Body::from(self.feed_xml));
resp.headers_mut().insert(
"content-type",
http::header::HeaderValue::from_str(self.content_type.as_ref())
.expect("invalid content_type"),
);
resp
match self.json_preview {
Some(json_preview) => Json(json!( {
"content": self.content,
"content_type": self.content_type.to_string(),
"json_preview": json_preview,
}))
.into_response(),
None => {
let headers = [("content-type", self.content_type.to_string())];
(StatusCode::OK, headers, self.content).into_response()
}
}
}
}

Expand Down

0 comments on commit f65f172

Please sign in to comment.