Skip to content

Commit

Permalink
NEXT-37316 - add trigger indexing command
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsKemper committed Jul 25, 2024
1 parent 91d61b3 commit ae8d8ea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
36 changes: 35 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod filter;

use crate::api::filter::{Criteria, CriteriaFilter};
use crate::config_file::Credentials;
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{header, Client, Response, StatusCode};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -255,6 +255,35 @@ impl SwClient {
Ok(res)
}

pub async fn index(&self, skip: Vec<String>) -> anyhow::Result<()> {
let access_token = self.access_token.lock().unwrap().clone();

let response = self
.client
.post(format!("{}/api/_action/index", self.credentials.base_url))
.bearer_auth(access_token)
.json(&IndexBody { skip })
.send()
.await?;

if !response.status().is_success() {
let status = response.status();
let body: SwErrorBody = Self::deserialize(response).await?;
return Err(anyhow::Error::msg(SwApiError::Server(status, body)));
}

Ok(())
}

pub async fn read_credentials() -> anyhow::Result<Credentials> {
let serialized_credentials = tokio::fs::read_to_string("../../.credentials.toml")
.await
.context("No .credentials.toml found. Call command auth first.")?;

let credentials: Credentials = toml::from_str(&serialized_credentials)?;
Ok(credentials)
}

async fn deserialize<T>(response: Response) -> Result<T, SwApiError>
where
T: for<'a> Deserialize<'a> + Debug + Send + 'static,
Expand All @@ -281,6 +310,11 @@ impl SwClient {
}
}

#[derive(Debug, Serialize)]
struct IndexBody {
skip: Vec<String>,
}

#[derive(Debug, Serialize)]
struct AuthBody {
grant_type: String,
Expand Down
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub struct Cli {

#[derive(Subcommand)]
pub enum Commands {
/// Trigger indexing of all registered indexer in shopware asynchronously.
Index {
// Array of indexer names to be skipped
#[arg(short, long)]
skip: Vec<String>,
},

/// Authenticate with a given shopware shop via integration admin API.
/// Credentials are stored in .credentials.toml in the current working directory.
Auth {
Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();

match cli.command {
Commands::Index { skip } => {
index(skip).await?;
println!("Successfully triggered indexing.");
}
Commands::Auth { domain, id, secret } => {
auth(domain, id, secret).await?;
println!("Successfully authenticated. You can continue with other commands now.")
Expand Down Expand Up @@ -71,6 +75,16 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

async fn index(skip: Vec<String>) -> anyhow::Result<()> {
let credentials = SwClient::read_credentials().await?;

// TODO: change in flight limit handling? Hardcoded default value?
let sw_client = SwClient::new(credentials.clone(), 8).await?;
sw_client.index(skip).await?;

Ok(())
}

async fn auth(domain: String, id: String, secret: String) -> anyhow::Result<()> {
let credentials = Credentials {
base_url: domain.trim_end_matches('/').to_string(),
Expand Down Expand Up @@ -107,10 +121,7 @@ async fn create_context(
}
}

let serialized_credentials = tokio::fs::read_to_string("./.credentials.toml")
.await
.context("No .credentials.toml found. Call command auth first.")?;
let credentials: Credentials = toml::from_str(&serialized_credentials)?;
let credentials = SwClient::read_credentials().await?;
let sw_client = SwClient::new(credentials, in_flight_limit).await?;

let api_schema = sw_client.entity_schema().await;
Expand Down

0 comments on commit ae8d8ea

Please sign in to comment.