Skip to content

Commit

Permalink
feat: implement external recipe HTTP client with timeout to be re-used
Browse files Browse the repository at this point in the history
  • Loading branch information
Toromyx committed Mar 12, 2024
1 parent dc2d3c0 commit 044c07c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src-tauri/src/external_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::str::FromStr;
use anyhow::Result;
use async_trait::async_trait;
use regex::Regex;
use reqwest::Client;
use url::Url;

use crate::external_recipe::error::ExternalRecipeError;
Expand All @@ -14,6 +15,17 @@ pub mod knusperstuebchen;
pub mod pinterest;
pub mod sallys_welt;

static CLIENT_ONCE_LOCK: OnceLock<Client> = OnceLock::new();

fn client() -> &'static Client {
CLIENT_ONCE_LOCK.get_or_init(|| {
reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(10))
.build()
.unwrap()
})
}

#[derive(Debug, Clone, Default)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ExternalRecipe {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/external_recipe/knusperstuebchen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct ExternalRecipeGetter;
impl ExternalRecipeGetterTrait for ExternalRecipeGetter {
/// The recipes on Knusperstuebchen can be only a pdf or a pdf and structured html.
async fn get(&self, url: Url) -> anyhow::Result<ExternalRecipe, ExternalRecipeError> {
let response = reqwest::get(url).await?;
let response = super::client().get(url).send().await?;
let text = response.text().await?;
let dom = Dom::create(text).await?;
let name_element = dom.select("h1").await?.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/external_recipe/pinterest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ pub struct ExternalRecipeGetter;
impl ExternalRecipeGetterTrait for ExternalRecipeGetter {
/// For `pin.it` URLs, we first need to find the canonical URL before trying to parse the HTML for the recipe data.
async fn get(&self, url: Url) -> Result<ExternalRecipe, ExternalRecipeError> {
let response = reqwest::get(url.clone()).await?;
let response = super::client().get(url.clone()).send().await?;
let mut text = response.text().await?;
if let Some(prepared_url) = UrlMatch::prepare_url(&url) {
if pin_it_uri_match().is_match(&prepared_url) {
let dom = Dom::create(text).await?;
let canonical_link = dom.canonical_link().await?;
let response = reqwest::get(canonical_link).await?;
let response = super::client().get(canonical_link).send().await?;
text = response.text().await?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/external_recipe/sallys_welt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct ExternalRecipeGetter;
#[async_trait]
impl ExternalRecipeGetterTrait for ExternalRecipeGetter {
async fn get(&self, url: Url) -> Result<ExternalRecipe, ExternalRecipeError> {
let response = reqwest::get(url).await?;
let response = super::client().get(url).send().await?;
let text = response.text().await?;
let dom = Arc::new(Dom::create(text).await?);
let mut steps = vec![];
Expand Down

0 comments on commit 044c07c

Please sign in to comment.