Struct Config

Source
pub struct Config {
    pub woo: Woo,
}
Expand description

Configuration struct for storing Woo parameters

Fields§

§woo: Woo

The Woo struct containing ck, cs, and host strings

Implementations§

Source§

impl Config

Source

pub fn new<T: ToString>(file_name: T) -> Result<Self>

Create a new Config instance by reading from a file

§Arguments
  • file_name - A file name (path) to read the configuration from
§Returns

A Result containing the Config instance if successful, or an error

Examples found in repository?
examples/data.rs (line 7)
5async fn main() -> anyhow::Result<()> {
6    tracing_subscriber::fmt::init();
7    let config = Config::new("woo.toml")?;
8    let client = ApiClient::new(&config)?;
9    let data = client.list_all::<Data>().await?;
10    info!("Got {} data", data.len());
11    Ok(())
12}
More examples
Hide additional examples
examples/customer.rs (line 8)
6async fn main() -> anyhow::Result<()> {
7    tracing_subscriber::fmt::init();
8    let config = Config::new("woo.toml")?;
9    let client = ApiClient::new(&config)?;
10    let customers = client.list_all::<Customer>().await?;
11    info!("Got {} customers", customers.len());
12    let retrieved: Customer = client.retrieve(customers.first().unwrap().id).await?;
13    info!("Retrieved customer name is {}", retrieved.first_name);
14    Ok(())
15}
examples/order.rs (line 9)
7async fn main() -> anyhow::Result<()> {
8    tracing_subscriber::fmt::init();
9    let config = Config::new("woo.toml")?;
10    let client = ApiClient::new(&config)?;
11    let orders = client.list_all::<Order>().await?;
12    info!("Got {} orders", orders.len());
13    let random_order_id = orders.first().ok_or(anyhow!("Error"))?.id;
14    let retrieved_order = client.retrieve::<Order>(random_order_id).await?;
15    info!(
16        "Got order with number: {} with total: {}",
17        retrieved_order.number, retrieved_order.total
18    );
19    Ok(())
20}
examples/category.rs (line 8)
6async fn main() -> anyhow::Result<()> {
7    tracing_subscriber::fmt::init();
8    let config = Config::new("woo.toml")?;
9    let client = ApiClient::new(&config)?;
10    let categories = client.list_all::<Category>().await?;
11    info!("Got {} categories", categories.len());
12    let random_id = categories.first().unwrap().id;
13    let retrieved: Category = client.retrieve(random_id).await?;
14    info!("Retrieved category name: {}", retrieved.name);
15    let create = Category::create("Test Category")
16        .parent(retrieved.id)
17        .description("Test description")
18        .display(DisplayOption::Products)
19        .image("https://woocommerce.github.io/woocommerce-rest-api-docs/images/logo-95f5c1ab.png");
20    let batch_create = create.clone();
21    let created: Category = client.create(create).await?;
22    info!("Category with id: {} created", created.id);
23    let update = Category::update().description("Some description");
24    let updated: Category = client.update(created.id, update).await?;
25    info!("New description is {}", updated.description);
26    let deleted: Category = client.delete(updated.id).await?;
27    info!("Category {} deleted", deleted.name);
28    let batch_created: Vec<Category> = client.batch_create(vec![batch_create]).await?;
29    info!("Batch created {} categories", batch_created.len());
30    let batch_update = Category::update()
31        .id(batch_created.first().unwrap().id)
32        .description("Some description");
33    let batch_updated: Vec<Category> = client.batch_update(vec![batch_update]).await?;
34    let id = batch_updated.first().unwrap().id;
35    info!("Batch updated categories contains category with id: {id}");
36    let batch_deleted: Vec<Category> = client.batch_delete(vec![id]).await?;
37    info!("Deleted {} categories", batch_deleted.len());
38    Ok(())
39}
examples/product.rs (line 8)
6async fn main() -> Result<()> {
7    tracing_subscriber::fmt::init();
8    let config = Config::new("woo.toml")?;
9    let client = ApiClient::new(&config)?;
10    let start = std::time::Instant::now();
11    let products = client.list_all::<Product>().await?;
12    info!(
13        "Got {} products in {} seconds",
14        products.len(),
15        start.elapsed().as_secs()
16    );
17    let random_id = products.first().map(|p| p.id).unwrap_or_default();
18    let retrieved = client.retrieve::<Product>(random_id).await?;
19    info!("Retrieved product has sku: {}", retrieved.sku);
20    let attribute = Attribute::builder()
21        .name("Test Attribute")
22        .option("Best")
23        .visible()
24        .build();
25    let new_product = Product::builder()
26        .name("Test Product For Example")
27        .featured()
28        .short_description("The most professional description")
29        .sku("product for test 42")
30        .regular_price("6969")
31        .manage_stock()
32        .stock_quantity(42)
33        .weight("50")
34        .dimensions("4", "3", "2")
35        .shipping_class("large")
36        .images("https://cs14.pikabu.ru/post_img/2021/06/27/7/1624794514137159585.jpg")
37        .attribute(attribute)
38        .build();
39    let batch_create = vec![new_product.clone()];
40    let created: Product = client.create(new_product).await?;
41    info!("Create product {} with id: {}", created.name, created.id);
42    let update = Product::builder().unfeatured().build();
43    let updated: Product = client.update(created.id, update).await?;
44    info!(
45        "Update product {}, new feature is {}",
46        updated.name, updated.featured
47    );
48    let deleted: Product = client.delete(updated.id).await?;
49    info!("Product {} deleted", deleted.name);
50    let batch_created: Vec<Product> = client.batch_create(batch_create).await?;
51    let id = batch_created.first().ok_or(anyhow!("Error"))?.id;
52    let batch_update = Product::builder().id(id).unfeatured().build();
53    let _batch_updated: Vec<Product> = client.batch_update(vec![batch_update]).await?;
54    let _deleted: Product = client.delete(id).await?;
55    Ok(())
56}
examples/variation.rs (line 12)
10async fn main() -> Result<()> {
11    tracing_subscriber::fmt::init();
12    let config = Config::new("woo.toml")?;
13    let client = ApiClient::new(&config)?;
14    let products = client.list_all::<Product>().await?;
15    let random_variable_id = products
16        .iter()
17        .find(|p| !p.variations.is_empty())
18        .map(|p| p.id)
19        .unwrap_or_default();
20    let variations = client
21        .list_all_subentities::<ProductVariation>(random_variable_id)
22        .await?;
23    info!(
24        "Got {} variations for product with id: {random_variable_id}",
25        variations.len()
26    );
27    let retrieved_variation: ProductVariation = client
28        .retrieve_subentity(
29            random_variable_id,
30            variations.first().map(|v| v.id).unwrap_or_default(),
31        )
32        .await?;
33    info!("Retrieved variation has sku: {}", retrieved_variation.sku);
34    let attribute = Attribute::builder()
35        .name("Test Attribute")
36        .option("Best")
37        .option("Test")
38        .variation()
39        .visible()
40        .build();
41    let new_variable_product = Product::builder()
42        .name("Test Product For Example")
43        .product_type(ProductType::Variable)
44        .featured()
45        .short_description("The most professional description")
46        .sku("product for test 42")
47        .regular_price("6969")
48        .manage_stock()
49        .stock_quantity(42)
50        .weight("50")
51        .dimensions("4", "3", "2")
52        .shipping_class("large")
53        .images("https://cs14.pikabu.ru/post_img/2021/06/27/7/1624794514137159585.jpg")
54        .attribute(attribute)
55        .build();
56    let created: Product = client.create(new_variable_product).await?;
57    info!("Create product {} with id: {}", created.name, created.id);
58
59    let variation = ProductVariation::builder()
60        .sku(format!("{} Best", created.sku))
61        .regular_price("6969")
62        .manage_stock()
63        .stock_quantity(96)
64        .weight("52")
65        .dimensions("5", "4", "3")
66        .attribute(None, "Test Attribute", "Best")
67        .build();
68    let batch_create_variation = vec![variation.clone()];
69    let created_variation: ProductVariation =
70        client.create_subentity(created.id, variation).await?;
71    info!(
72        "Variation {} created with price: {}",
73        created_variation.sku, created_variation.price
74    );
75    let update = ProductVariation::builder().regular_price("7000").build();
76    let updated_variation: ProductVariation = client
77        .update_subentity(created.id, created_variation.id, update)
78        .await?;
79    info!(
80        "Variation {} updated with price: {}",
81        updated_variation.sku, updated_variation.price
82    );
83    let deleted_variation: ProductVariation = client
84        .delete_subentity(created.id, updated_variation.id)
85        .await?;
86    info!("Variation {} deleted", deleted_variation.sku);
87    let batch_created_variation: Vec<ProductVariation> = client
88        .batch_create_subentity(created.id, batch_create_variation)
89        .await?;
90    let bcv_id = batch_created_variation
91        .first()
92        .map(|v| v.id)
93        .unwrap_or_default();
94    let batch_update_variation = vec![ProductVariation::builder()
95        .id(bcv_id)
96        .regular_price("777")
97        .build()];
98    let _batch_updated_variation: Vec<ProductVariation> = client
99        .batch_update_subentity(created.id, batch_update_variation)
100        .await?;
101    let _batch_deleted_variation: Vec<ProductVariation> = client
102        .batch_delete_subentity(created.id, vec![bcv_id])
103        .await?;
104    let deleted: Product = client.delete(created.id).await?;
105    info!("Product {} deleted", deleted.name);
106    Ok(())
107}

Trait Implementations§

Source§

impl<'de> Deserialize<'de> for Config

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,