From b69cbf840e7f1b5b402423b79991f78acbd8c9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Djk=C3=A1=C5=A5o?= Date: Thu, 14 Mar 2024 15:06:25 +0100 Subject: [PATCH] if not using apl token, couldn't fetch all channels --- sitemap-generator/src/routes/register.rs | 49 +++++++++++++++++------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/sitemap-generator/src/routes/register.rs b/sitemap-generator/src/routes/register.rs index 92a791d..f8f3fb7 100644 --- a/sitemap-generator/src/routes/register.rs +++ b/sitemap-generator/src/routes/register.rs @@ -76,18 +76,21 @@ pub async fn register( pub async fn regenerate(state: AppState, saleor_api_url: String) -> anyhow::Result<()> { info!("regeneration: fetching all categories, products, collections, pages"); let xml_cache = state.xml_cache.lock().await; + let apl = state.saleor_app.lock().await; + let token = token.apl.get(&saleor_api_url).await?; + let mut categories: Vec<(Category3, Vec>)> = - get_all_categories(&saleor_api_url) + get_all_categories(&saleor_api_url, token) .await? .into_iter() .map(|c| (c, vec![])) .collect(); let mut products = vec![]; for category in categories.iter_mut() { - products.append(&mut get_all_products(&saleor_api_url, category).await?); + products.append(&mut get_all_products(&saleor_api_url, token, category).await?); } - let pages = get_all_pages(&saleor_api_url).await?; - let collections = get_all_collections(&saleor_api_url).await?; + let pages = get_all_pages(&saleor_api_url, token).await?; + let collections = get_all_collections(&saleor_api_url, token).await?; info!( "regeneration: found {} products, {} categories, {} pages, {} collections", products.len(), @@ -259,10 +262,16 @@ pub async fn regenerate(state: AppState, saleor_api_url: String) -> anyhow::Resu Ok(()) } -async fn get_all_pages(saleor_api_url: &str) -> anyhow::Result> { +async fn get_all_pages( + saleor_api_url: &str, + token: &str, +) -> anyhow::Result> { let operation = GetPagesInitial::build(()); let mut all_pages = vec![]; - let res = surf::post(saleor_api_url).run_graphql(operation).await; + let res = surf::post(saleor_api_url) + .header("authorization-bearer", token) + .run_graphql(operation) + .await; if let Ok(query) = &res && let Some(data) = &query.data && let Some(pages) = &data.pages @@ -280,9 +289,8 @@ async fn get_all_pages(saleor_api_url: &str) -> anyhow::Result anyhow::Result anyhow::Result> { +async fn get_all_categories(saleor_api_url: &str, token: &str) -> anyhow::Result> { debug!("Collecting all categories..."); let operation = GetCategoriesInitial::build(()); let mut all_categories = vec![]; - let res = surf::post(saleor_api_url).run_graphql(operation).await; + let res = surf::post(saleor_api_url) + .header("authorization-bearer", token) + .run_graphql(operation) + .await; if let Ok(query) = &res && let Some(data) = &query.data && let Some(categories) = &data.categories @@ -341,6 +352,7 @@ async fn get_all_categories(saleor_api_url: &str) -> anyhow::Result anyhow::Result anyhow::Result> { +async fn get_all_collections(saleor_api_url: &str, token: &str) -> anyhow::Result> { debug!("Collecting all Collections..."); let operation = GetCollectionsInitial::build(()); let mut all_collections = vec![]; - let res = surf::post(saleor_api_url).run_graphql(operation).await; + let res = surf::post(saleor_api_url) + .header("authorization-bearer", token) + .run_graphql(operation) + .await; if let Ok(query) = &res && let Some(data) = &query.data && let Some(collections) = &data.collections @@ -406,6 +421,7 @@ async fn get_all_collections(saleor_api_url: &str) -> anyhow::Result anyhow::Result>), ) -> anyhow::Result>> { debug!("Collecting all products..."); @@ -456,7 +473,10 @@ async fn get_all_products( id: &main_category.0.id, }); let mut all_categorised_products: Vec> = vec![]; - let res = surf::post(saleor_api_url).run_graphql(operation).await; + let res = surf::post(saleor_api_url) + .header("authorization-bearer", token) + .run_graphql(operation) + .await; if let Ok(query) = &res && let Some(data) = &query.data && let Some(category) = &data.category @@ -480,6 +500,7 @@ async fn get_all_products( loop { if let Some(cursor) = &mut next_cursor { let res = surf::post(saleor_api_url) + .header("authorization-bearer", token) .run_graphql(GetCategoryProductsNext::build( GetCategoryProductsNextVariables { id: &main_category.0.id,