]> Untitled Git - lemmy.git/blob - crates/apub_lib/src/utils.rs
Federate with Peertube (#2244)
[lemmy.git] / crates / apub_lib / src / utils.rs
1 use crate::APUB_JSON_CONTENT_TYPE;
2 use anyhow::anyhow;
3 use http::StatusCode;
4 use lemmy_utils::{request::retry, settings::structs::Settings, LemmyError, REQWEST_TIMEOUT};
5 use reqwest_middleware::ClientWithMiddleware;
6 use serde::de::DeserializeOwned;
7 use tracing::log::info;
8 use url::Url;
9
10 pub async fn fetch_object_http<Kind: DeserializeOwned>(
11   url: &Url,
12   client: &ClientWithMiddleware,
13   request_counter: &mut i32,
14 ) -> Result<Kind, LemmyError> {
15   // dont fetch local objects this way
16   debug_assert!(url.domain() != Some(&Settings::get().hostname));
17   info!("Fetching remote object {}", url.to_string());
18
19   *request_counter += 1;
20   if *request_counter > Settings::get().http_fetch_retry_limit {
21     return Err(LemmyError::from(anyhow!("Request retry limit reached")));
22   }
23
24   let res = retry(|| {
25     client
26       .get(url.as_str())
27       .header("Accept", APUB_JSON_CONTENT_TYPE)
28       .timeout(REQWEST_TIMEOUT)
29       .send()
30   })
31   .await?;
32
33   if res.status() == StatusCode::GONE {
34     return Err(LemmyError::from_message("410"));
35   }
36
37   Ok(res.json().await?)
38 }