if not using apl token, couldn't fetch all channels

This commit is contained in:
Djkáťo 2024-03-14 15:06:25 +01:00
parent 205ead022e
commit b69cbf840e

View file

@ -76,18 +76,21 @@ pub async fn register(
pub async fn regenerate(state: AppState, saleor_api_url: String) -> anyhow::Result<()> { pub async fn regenerate(state: AppState, saleor_api_url: String) -> anyhow::Result<()> {
info!("regeneration: fetching all categories, products, collections, pages"); info!("regeneration: fetching all categories, products, collections, pages");
let xml_cache = state.xml_cache.lock().await; 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<Arc<CategorisedProduct>>)> = let mut categories: Vec<(Category3, Vec<Arc<CategorisedProduct>>)> =
get_all_categories(&saleor_api_url) get_all_categories(&saleor_api_url, token)
.await? .await?
.into_iter() .into_iter()
.map(|c| (c, vec![])) .map(|c| (c, vec![]))
.collect(); .collect();
let mut products = vec![]; let mut products = vec![];
for category in categories.iter_mut() { 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 pages = get_all_pages(&saleor_api_url, token).await?;
let collections = get_all_collections(&saleor_api_url).await?; let collections = get_all_collections(&saleor_api_url, token).await?;
info!( info!(
"regeneration: found {} products, {} categories, {} pages, {} collections", "regeneration: found {} products, {} categories, {} pages, {} collections",
products.len(), products.len(),
@ -259,10 +262,16 @@ pub async fn regenerate(state: AppState, saleor_api_url: String) -> anyhow::Resu
Ok(()) Ok(())
} }
async fn get_all_pages(saleor_api_url: &str) -> anyhow::Result<Vec<get_all_pages::Page>> { async fn get_all_pages(
saleor_api_url: &str,
token: &str,
) -> anyhow::Result<Vec<get_all_pages::Page>> {
let operation = GetPagesInitial::build(()); let operation = GetPagesInitial::build(());
let mut all_pages = vec![]; 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 if let Ok(query) = &res
&& let Some(data) = &query.data && let Some(data) = &query.data
&& let Some(pages) = &data.pages && let Some(pages) = &data.pages
@ -280,9 +289,8 @@ async fn get_all_pages(saleor_api_url: &str) -> anyhow::Result<Vec<get_all_pages
loop { loop {
if let Some(cursor) = &mut next_cursor { if let Some(cursor) = &mut next_cursor {
let res = surf::post(saleor_api_url) let res = surf::post(saleor_api_url)
.run_graphql(GetPagesNext::build(GetPagesNextVariables { .header("authorization-bearer", token)
after: cursor, .run_graphql(GetPagesNext::build(GetPagesNextVariables { after: cursor }))
}))
.await; .await;
if let Ok(query) = &res if let Ok(query) = &res
&& let Some(data) = &query.data && let Some(data) = &query.data
@ -316,11 +324,14 @@ async fn get_all_pages(saleor_api_url: &str) -> anyhow::Result<Vec<get_all_pages
Ok(all_pages) Ok(all_pages)
} }
async fn get_all_categories(saleor_api_url: &str) -> anyhow::Result<Vec<Category3>> { async fn get_all_categories(saleor_api_url: &str, token: &str) -> anyhow::Result<Vec<Category3>> {
debug!("Collecting all categories..."); debug!("Collecting all categories...");
let operation = GetCategoriesInitial::build(()); let operation = GetCategoriesInitial::build(());
let mut all_categories = vec![]; 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 if let Ok(query) = &res
&& let Some(data) = &query.data && let Some(data) = &query.data
&& let Some(categories) = &data.categories && let Some(categories) = &data.categories
@ -341,6 +352,7 @@ async fn get_all_categories(saleor_api_url: &str) -> anyhow::Result<Vec<Category
loop { loop {
if let Some(cursor) = &mut next_cursor { if let Some(cursor) = &mut next_cursor {
let res = surf::post(saleor_api_url) let res = surf::post(saleor_api_url)
.header("authorization-bearer", token)
.run_graphql(GetCategoriesNext::build(GetCategoriesNextVariables { .run_graphql(GetCategoriesNext::build(GetCategoriesNextVariables {
after: Some(cursor), after: Some(cursor),
})) }))
@ -380,11 +392,14 @@ async fn get_all_categories(saleor_api_url: &str) -> anyhow::Result<Vec<Category
Ok(all_categories) Ok(all_categories)
} }
async fn get_all_collections(saleor_api_url: &str) -> anyhow::Result<Vec<Collection>> { async fn get_all_collections(saleor_api_url: &str, token: &str) -> anyhow::Result<Vec<Collection>> {
debug!("Collecting all Collections..."); debug!("Collecting all Collections...");
let operation = GetCollectionsInitial::build(()); let operation = GetCollectionsInitial::build(());
let mut all_collections = vec![]; 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 if let Ok(query) = &res
&& let Some(data) = &query.data && let Some(data) = &query.data
&& let Some(collections) = &data.collections && let Some(collections) = &data.collections
@ -406,6 +421,7 @@ async fn get_all_collections(saleor_api_url: &str) -> anyhow::Result<Vec<Collect
loop { loop {
if let Some(cursor) = &mut next_cursor { if let Some(cursor) = &mut next_cursor {
let res = surf::post(saleor_api_url) let res = surf::post(saleor_api_url)
.header("authorization-bearer", token)
.run_graphql(GetCollectionsNext::build(GetCollectionsNextVariables { .run_graphql(GetCollectionsNext::build(GetCollectionsNextVariables {
after: Some(cursor), after: Some(cursor),
})) }))
@ -449,6 +465,7 @@ async fn get_all_collections(saleor_api_url: &str) -> anyhow::Result<Vec<Collect
*/ */
async fn get_all_products( async fn get_all_products(
saleor_api_url: &str, saleor_api_url: &str,
token: &str,
main_category: &mut (Category3, Vec<Arc<CategorisedProduct>>), main_category: &mut (Category3, Vec<Arc<CategorisedProduct>>),
) -> anyhow::Result<Vec<Arc<CategorisedProduct>>> { ) -> anyhow::Result<Vec<Arc<CategorisedProduct>>> {
debug!("Collecting all products..."); debug!("Collecting all products...");
@ -456,7 +473,10 @@ async fn get_all_products(
id: &main_category.0.id, id: &main_category.0.id,
}); });
let mut all_categorised_products: Vec<Arc<CategorisedProduct>> = vec![]; let mut all_categorised_products: Vec<Arc<CategorisedProduct>> = 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 if let Ok(query) = &res
&& let Some(data) = &query.data && let Some(data) = &query.data
&& let Some(category) = &data.category && let Some(category) = &data.category
@ -480,6 +500,7 @@ async fn get_all_products(
loop { loop {
if let Some(cursor) = &mut next_cursor { if let Some(cursor) = &mut next_cursor {
let res = surf::post(saleor_api_url) let res = surf::post(saleor_api_url)
.header("authorization-bearer", token)
.run_graphql(GetCategoryProductsNext::build( .run_graphql(GetCategoryProductsNext::build(
GetCategoryProductsNextVariables { GetCategoryProductsNextVariables {
id: &main_category.0.id, id: &main_category.0.id,