Crate dify_client

Source
Expand description

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.

Re-exports§

pub use client::*;

Modules§

api
This module provides a client for interacting with the Dify API.
client
This module contains the implementation of the Dify client.
http
This module re-exports some common items most developers need from the reqwest crate.
request
This module contains the request structures used in the Dify client SDK.
response
This module contains the response structures used in the Dify SDK.