Crates.io | lightspeed_api |
lib.rs | lightspeed_api |
version | 0.1.4 |
source | src |
created_at | 2024-06-25 21:33:38.81419 |
updated_at | 2024-06-26 13:24:58.025023 |
description | A Rust crate for the Lightspeed eCommerce (C-Series) API |
homepage | |
repository | https://github.com/NickTacke/lightspeed-api-rust |
max_upload_size | |
id | 1283894 |
size | 17,968 |
This crate provides a Rust interface for the Lightspeed eCommerce (C-Series) API. It allows you to easily integrate Lightspeed's powerful e-commerce features into your Rust applications.
Add this to your Cargo.toml
:
[dependencies]
lightspeed_api = { git = "https://github.com/NickTacke/lightspeed-api-rust.git" }
Here's a simple example to get you started:
use lightspeed_api::{LightspeedClient, AccountResource};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = LightspeedClient::new("live", "your_api_key", "your_api_secret", "en");
let account = AccountResource::new(&client);
let account_info = account.get().await?;
println!("Account info: {:?}", account_info);
Ok(())
}
First, create an instance of LightspeedClient
with your Lightspeed API credentials:
let client = LightspeedClient::new("live", "your_api_key", "your_api_secret", "en");
Each API resource (e.g., products, orders, customers) has its own struct. Here's an example using the ProductResource
:
use lightspeed_api::{LightspeedClient, ProductResource};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = LightspeedClient::new("live", "your_api_key", "your_api_secret", "en");
let products = ProductResource::new(&client);
// Create a new product
let new_product = json!({
"title": "New Product",
"description": "This is a new product",
"price": 19.99
});
let created_product = products.create(&new_product).await?;
// Get a product
let product = products.get("product_id").await?;
// Update a product
let update_data = json!({"price": 24.99});
let updated_product = products.update("product_id", &update_data).await?;
// Delete a product
let deleted_response = products.delete("product_id").await?;
Ok(())
}
AccountResource
ProductResource
OrderResource
CustomerResource
This crate uses a custom LightspeedApiError
for API-related errors. You can catch and handle these errors in your code:
match result {
Ok(data) => println!("Success: {:?}", data),
Err(e) => {
if let Some(api_error) = e.downcast_ref::<LightspeedApiError>() {
println!("API Error: {} (Status: {})", api_error.message, api_error.status_code);
} else {
println!("Other error: {}", e);
}
}
}
This project is licensed under the MIT License - see the LICENSE file for details.
This crate is not officially associated with or endorsed by Lightspeed. Use at your own risk.