Skip to content

Commit

Permalink
Revert "remove content_type from endpoint config"
Browse files Browse the repository at this point in the history
This reverts commit e82888a.
  • Loading branch information
shouya committed Feb 4, 2024
1 parent 9baf863 commit c4230a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ mod tests {

#[tokio::test]
async fn test_client_cache() {
let client =
Client::new(1, Duration::from_secs(1), reqwest::Client::new(), None);
let client = Client::new(1, Duration::from_secs(1), reqwest::Client::new());
let url = Url::parse("http://example.com").unwrap();
let body: Box<str> = "foo".into();
let response = Response::new(
Expand All @@ -198,8 +197,7 @@ mod tests {

#[tokio::test]
async fn test_client() {
let client =
Client::new(0, Duration::from_secs(1), reqwest::Client::new(), None);
let client = Client::new(0, Duration::from_secs(1), reqwest::Client::new());
let url = Url::parse(YT_SCISHOW_FEED_URL).unwrap();
let resp = client.get(&url).await.unwrap();
assert_eq!(resp.status(), reqwest::StatusCode::OK);
Expand Down
24 changes: 12 additions & 12 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl EndpointConfig {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct EndpointServiceConfig {
source: Option<String>,
content_type: Option<String>,
filters: Vec<FilterConfig>,
#[serde(default)]
client: Option<ClientConfig>,
Expand All @@ -57,6 +58,7 @@ pub struct EndpointServiceConfig {
#[derive(Clone)]
pub struct EndpointService {
source: Option<Url>,
content_type: Option<String>,
filters: Arc<Vec<BoxedFilter>>,
client: Arc<Client>,
}
Expand Down Expand Up @@ -234,6 +236,7 @@ impl EndpointService {

Ok(Self {
source,
content_type: config.content_type,
filters: Arc::new(filters),
client: Arc::new(client),
})
Expand Down Expand Up @@ -277,28 +280,25 @@ impl EndpointService {
}
}

const ACCEPTED_CONTENT_TYPES: [&'static str; 6] = [
"application/xml",
"text/xml",
"application/rss+xml",
"application/atom+xml",
"text/html",
"*/*",
];

async fn fetch_feed(&self, source: &Url) -> Result<Feed> {
let resp = self
.client
.get_with(source, |builder| {
builder.header("Accept", Self::ACCEPTED_CONTENT_TYPES.join(", "))
builder.header("Accept", "text/html,application/xml,text/xml,application/rss+xml,application/atom+xml")
})
.await?
.error_for_status()?;

let content_type = resp.content_type().map(|x| x.essence_str().to_owned());
let resp_content_type =
resp.content_type().map(|x| x.essence_str().to_owned());
let content_type = self
.content_type
.as_deref()
.or(resp_content_type.as_deref());

let content = resp.text()?;

let feed = match content_type.as_deref() {
let feed = match content_type {
Some("text/html") => Feed::from_html_content(&content, source)?,
Some("application/rss+xml") => Feed::from_rss_content(&content)?,
Some("application/atom+xml") => Feed::from_atom_content(&content)?,
Expand Down

0 comments on commit c4230a3

Please sign in to comment.