9 releases

new 0.3.0 May 13, 2024
0.2.2 May 8, 2024
0.1.4 May 7, 2024
0.1.3 Apr 29, 2024

#113 in HTTP client

Download history 148/week @ 2024-04-22 190/week @ 2024-04-29 488/week @ 2024-05-06

826 downloads per month

Apache-2.0

77KB
1K SLoC

Dify Client

The Dify Client is a Rust library for interacting with the Dify service. It provides a convenient way to integrate Dify functionality into your Rust applications.

Installation

To add dify-client to your package, add the following to your Cargo.toml:

[dependencies]
dify-client = "0.3"

Test

To run the tests, you need to set the DIFY_API_KEY and DIFY_BASE_URL environment variables.

export DIFY_API_KEY=your_api_key
export DIFY_BASE_URL=https://api.dify.io

Then you can run the tests with:

cargo test
# cargo test -- --nocapture
# cargo test test_feedback_message -- --nocapture

Docs

To generate the documentation, run:

cargo doc --no-deps --lib --open

lib.rs:

Dify client library.

Examples

Client with single api key

use dify_client::{request, Config, Client};
use std::time::Duration;

#[tokio::main]
async fn main() {
    let config = Config {
        base_url: "https://api.dify.ai".into(),
        api_key: "API_KEY".into(),
        timeout: Duration::from_secs(60),
    };
    let client = Client::new_with_config(config);

    // Use the client
    let data = request::ChatMessagesRequest {
        query: "What are the specs of the iPhone 13 Pro Max?".into(),
        user: "afa".into(),
        ..Default::default()
    };
    let result = client.api().chat_messages(data).await;
    println!("{:?}", result);
}

Client with multiple api keys

use dify_client::{http::header, request, Config, Client};
use std::time::Duration;

#[tokio::main]
async fn main() {
    let config = Config {
        base_url: "https://api.dify.ai".into(),
        api_key: "API_KEY_DEFAULT".into(),
        timeout: Duration::from_secs(100),
    };
    // The client can be safely shared across multiple threads
    let client = Client::new_with_config(config);
    
    // Use the client
    let data = request::ChatMessagesRequest {
        query: "What are the specs of the iPhone 13 Pro Max?".into(),
        user: "afa".into(),
        ..Default::default()
    };
    // Reuse the client with a new api key
    let mut api = client.api();
    let result = api.chat_messages(data.clone()).await;
    println!("{:?}", result);
    // Override the api key
    api.before_send(|mut req| {
        // rewrite the authorization header
        let mut auth = header::HeaderValue::from_static("Bearer API_KEY_OVERRIDE");
        auth.set_sensitive(true);
        req.headers_mut().insert(header::AUTHORIZATION, auth);
        req
    });
    let result = api.chat_messages(data).await;
    println!("{:?}", result);
}

For more API methods, refer to the Api struct.

Dependencies

~6–20MB
~280K SLoC