Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async client #11

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tinify-rs"
version = "1.2.0"
version = "1.3.0"
edition = "2021"
description = "A Rust client for the Tinify API"
authors = ["The tinify-rs Developers"]
Expand All @@ -14,6 +14,7 @@ categories = ["compression", "multimedia::images"]

[dependencies]
reqwest = { version = "0.11.11", features = ["blocking"] }
tokio = { version = "1", features = ["full"], optional = true}
serde = { version = "1.0.149", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.89", default-features = false, features = ["alloc"] }
serde_derive = "1.0.149"
Expand All @@ -27,3 +28,6 @@ assert_matches = "1.5.0"
name = "tinify"
path = "./src/lib.rs"
doctest = false

[features]
async = ["dep:tokio"]
132 changes: 21 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To look at all the features of Tinify API: [Documentation](https://tinypng.com/d
* [ ] Preserving metadata
* [ ] Saving to Amazon S3
* [ ] Saving to Google Cloud Storage
* [ ] Implement an async non-blocking Client
* [x] Implement an async non-blocking Client


## Getting Started
Expand All @@ -44,8 +44,16 @@ Install the API client with Cargo. Add this to `Cargo.toml`:

```toml
[dependencies]
tinify-rs = "1.2.0"
tinify-rs = "1.3.0"
```

Using async client

```toml
[dependencies]
tinify-rs = { version = "1.3.0", features = ["async"] }
```

## Usage

- About key
Expand All @@ -69,118 +77,20 @@ fn main() -> Result<(), TinifyError> {
}
```

- Compress from an url
- Compress from a file async
```rust
use tinify::Tinify;
use tinify::TinifyError;
use tinify::async_bin::Tinify as AsyncTinify;
use tinify::error::TinifyError;

fn main() -> Result<(), TinifyError> {
#[tokio::main]
async fn main() -> Result<(), TinifyError> {
let key = "tinify api key";
let tinify = Tinify::new().set_key(key);
let client = tinify.get_client()?;
let _ = client
.from_url("https://tinypng.com/images/panda-happy.png")?
.to_file("./optimized.png")?;

Ok(())
}
```

- Compress from a buffer
```rust
use tinify::Tinify;
use tinify::TinifyError;
use std::fs;

fn main() -> Result<(), TinifyError> {
let key = "tinify api key";
let tinify = Tinify::new().set_key(key);
let client = tinify.get_client()?;
let bytes = fs::read("./unoptimized.jpg")?;
let _ = client
.from_buffer(&bytes)?
let tinify = AsyncTinify::new().set_key(key);
let client = tinify.get_async_client()?;
client
.from_file("./unoptimized.jpg")
.await?
.to_file("./optimized.jpg")?;

Ok(())
}
```

- Resize a compressed image.
```rust
use tinify::Tinify;
use tinify::Client;
use tinify::TinifyError;
use tinify::resize::Method;
use tinify::resize::Resize;

fn get_client() -> Result<Client, TinifyError> {
let key = "tinify api key";
let tinify = Tinify::new();

tinify
.set_key(key)
.get_client()
}

fn main() -> Result<(), TinifyError> {
let client = get_client()?;
let _ = client
.from_file("./unoptimized.jpg")?
.resize(Resize::new(
Method::FIT,
Some(400),
Some(200)),
)?
.to_file("./resized.jpg")?;

Ok(())
}
```

- Converting images.
```rust
use tinify::Tinify;
use tinify::convert::Type;
use tinify::TinifyError;

fn main() -> Result<(), TinifyError> {
let _ = Tinify::new()
.set_key("api key")
.get_client()?
.from_file("./tmp_image.jpg")?
.convert((
Some(Type::PNG),
None,
None,
),
None,
)?
.to_file("./converted.png");

Ok(())
}
```

- Converting images with a transparent background.
```rust
use tinify::Tinify;
use tinify::convert::Color;
use tinify::convert::Type;
use tinify::TinifyError;

fn main() -> Result<(), TinifyError> {
let _ = Tinify::new()
.set_key("api key")
.get_client()?
.from_url("https://tinypng.com/images/panda-happy.png")?
.convert((
Some(Type::JPEG),
None,
None,
),
Some(Color("#FF5733")),
)?
.to_file("./converted.jpg");

Ok(())
}
Expand All @@ -191,7 +101,7 @@ fn main() -> Result<(), TinifyError> {
Create a .env file with a TiniPNG KEY

```
cargo test
cargo test --features async
```

## Contribution
Expand Down
Loading
Loading