#api-client #e-commerce #client #lightspeed #api-bindings #api-error #api

lightspeed_api

A Rust crate for the Lightspeed eCommerce (C-Series) API

5 releases

0.1.4 Jun 26, 2024
0.1.3 Jun 26, 2024
0.1.2 Jun 25, 2024
0.1.1 Jun 25, 2024
0.1.0 Jun 25, 2024

#1859 in Web programming

Download history 335/week @ 2024-06-21 31/week @ 2024-06-28 1/week @ 2024-07-05

97 downloads per month

MIT license

17KB
330 lines

Lightspeed API Rust Crate

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.

Features

  • Simple and intuitive API client
  • Asynchronous operations using Tokio
  • Comprehensive error handling
  • Support for all major Lightspeed API endpoints
  • Easily extensible for custom use cases

Installation

Add this to your Cargo.toml:

[dependencies]
lightspeed_api = { git = "https://github.com/NickTacke/lightspeed-api-rust.git" }

Quick Start

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(())
}

Usage

Initializing the Client

First, create an instance of LightspeedClient with your Lightspeed API credentials:

let client = LightspeedClient::new("live", "your_api_key", "your_api_secret", "en");

Working with Resources

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(())
}

Available Resources

  • AccountResource
  • ProductResource
  • OrderResource
  • CustomerResource
  • More coming soon!

Error Handling

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);
        }
    }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This crate is not officially associated with or endorsed by Lightspeed. Use at your own risk.

Dependencies

~6–18MB
~267K SLoC