Skip to main content

snipp-gg/snipp-rust

crates.io/crates/snipp

Installation

Add to your Cargo.toml:
[dependencies]
snipp-rust = "0.1"
tokio = { version = "1", features = ["full"] }

Getting started

use snipp_rust::SnippClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = SnippClient::new("YOUR_API_KEY");

    let me = client.get_user("@me", None).await?;
    println!("{}", me.user.username.unwrap_or_default());

    Ok(())
}

Methods

All methods are async and return Result<T, SnippError>.

get_user(id, options)

Fetch a user by ID. Pass "@me" for the authenticated user.
use snipp_rust::GetUserOptions;

// Without options
let user = client.get_user("@me", None).await?;

// With options
let opts = GetUserOptions {
    include_posts: Some(true),
    posts_limit: Some(10),
};
let user = client.get_user("@me", Some(opts)).await?;

get_post(code)

Fetch a post by its share code.
let post = client.get_post("AbCd1234").await?;
println!("{:?}", post.post.url);

upload(path, privacy)

Upload a file from disk.
use snipp_rust::Privacy;

let result = client.upload("image.png", Some(Privacy::Unlisted)).await?;
println!("{}", result.url);

list_uploads()

List the authenticated user’s recent uploads.
let uploads = client.list_uploads().await?;

edit_upload(code, options)

Edit an existing upload’s title, description, or privacy.
use snipp_rust::{EditUploadOptions, Privacy};

let opts = EditUploadOptions {
    title: Some("New title".into()),
    privacy: Some(Privacy::Public),
    ..Default::default()
};
client.edit_upload("AbCd1234", opts).await?;

delete_upload(filename)

Delete an upload by its filename.
client.delete_upload("a3f7b2c91d4e8f0612ab34cd56ef7890.png").await?;

discover()

Browse public uploads from the community.
let feed = client.discover().await?;

list_blocks()

List the users you have blocked.
let blocks = client.list_blocks().await?;

block_user(target_id)

Block a user by their ID.
client.block_user("123456789012345678").await?;

unblock_user(target_id)

Unblock a user by their ID.
client.unblock_user("123456789012345678").await?;

Error handling

All errors are returned as SnippError, which covers API errors, HTTP errors, deserialization failures, and IO errors.
match client.get_user("nonexistent", None).await {
    Ok(user) => println!("{:?}", user),
    Err(e) => eprintln!("Error: {e}"),
}