pub struct Client {
pub org_uuid: String,
pub cookies: String,
}
Fields§
§org_uuid: String
Implementations§
Source§impl Client
impl Client
Sourcepub async fn new(cookies: String) -> Self
pub async fn new(cookies: String) -> Self
Creates a new instance of the struct.
This function takes a cookies
string as input, which is used to get the organization ID.
If the organization ID cannot be retrieved (which may happen if the cookies are expired or invalid),
an error message is logged and the process is terminated with exit code 1.
§Arguments
cookies
- A string representing the cookies to be used for getting the organization ID.
§Returns
Self
- An instance of the struct, with thecookies
field set to the inputcookies
string, and theorg_uuid
field set to the retrieved organization ID.
§Errors
This function will exit the process if the organization ID cannot be retrieved.
§Examples
use claude::Client;
use std::env::var;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cookies = format!(
"activitySessionId={}; sessionKey={}",
var("SESSION_ID").unwrap(),
var("SESSION_KEY").unwrap()
);
let client = Client::new(cookies).await;
tracing::info!("Client created, {:?}", client);
}
Sourcepub async fn get_organization_id(cookies: String) -> Result<String>
pub async fn get_organization_id(cookies: String) -> Result<String>
Retrieves the organization ID from the API.
This function sends a GET request to the API and deserializes the response into a vector of Response
structs.
The uuid
field of the first Response
struct in the vector is then returned.
§Arguments
cookies
- A string representing the cookies to be used for the request.
§Returns
Result<String>
- The organization ID, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the request fails or if the response cannot be deserialized.
Sourcepub async fn create_new_chat(&self) -> Result<Conversation>
pub async fn create_new_chat(&self) -> Result<Conversation>
Creates a new chat conversation.
This function sends a POST request to the API to create a new chat conversation. The payload for the request includes a randomly generated UUID and an empty name.
§Returns
Result<Conversation>
- The created chat conversation, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the request fails or if the response cannot be deserialized.
§Examples
use claude::Client;
use std::env::var;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cookies = format!(
"activitySessionId={}; sessionKey={}",
var("SESSION_ID").unwrap(),
var("SESSION_KEY").unwrap()
);
let client = Client::new(cookies).await;
let chat = client.create_new_chat().await.unwrap();
tracing::info!("{:?}", chat);
}
Sourcepub async fn list_all_conversations(&self) -> Result<Vec<Conversation>>
pub async fn list_all_conversations(&self) -> Result<Vec<Conversation>>
Lists all chat conversations.
This function sends a GET request to the API to retrieve all chat conversations for the organization.
§Returns
Result<Vec<Conversation>>
- A vector ofConversation
structs, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the request fails or if the response cannot be deserialized.
§Examples
§Examples
use claude::Client;
use std::env::var;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cookies = format!(
"activitySessionId={}; sessionKey={}",
var("SESSION_ID").unwrap(),
var("SESSION_KEY").unwrap()
);
let client = Client::new(cookies).await;
let chats = client.list_all_conversations().await.unwrap();
tracing::info!("{:?}", chats);
}
Sourcepub async fn chat_conversation_history(
&self,
chat_uuid: &str,
) -> Result<Vec<ChatMessage>>
pub async fn chat_conversation_history( &self, chat_uuid: &str, ) -> Result<Vec<ChatMessage>>
Retrieves the history of a chat conversation.
This function sends a GET request to the API to retrieve the history of a chat conversation.
The history is returned as a vector of ChatMessage
structs.
§Arguments
chat_uuid
- A string representing the UUID of the chat conversation.
§Returns
Result<Vec<ChatMessage>>
- A vector ofChatMessage
structs, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the request fails or if the response cannot be deserialized.
§Examples
use claude::Client;
use std::env::var;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cookies = format!(
"activitySessionId={}; sessionKey={}",
var("SESSION_ID").unwrap(),
var("SESSION_KEY").unwrap()
);
let client = Client::new(cookies).await;
let chat_hist = client.chat_conversation_history().await.unwrap();
tracing::info!("{:?}", chat_hist);
}
Sourcepub async fn delete_conversation(&self, chat_uuid: &str) -> Result<()>
pub async fn delete_conversation(&self, chat_uuid: &str) -> Result<()>
Deletes a chat conversation.
This function sends a DELETE request to the API to delete a chat conversation.
§Arguments
chat_uuid
- A string representing the UUID of the chat conversation to be deleted.
§Returns
Result<()>
- An emptyResult
, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the request fails.
§Examples
use claude::Client;
use std::env::var;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cookies = format!(
"activitySessionId={}; sessionKey={}",
var("SESSION_ID").unwrap(),
var("SESSION_KEY").unwrap()
);
let client = Client::new(cookies).await;
let chat_hist = client.delete_conversation("chat_uuid_string").await.unwrap();
}
Sourcepub async fn reset_all(&self) -> Result<()>
pub async fn reset_all(&self) -> Result<()>
Resets all chat conversations.
This function retrieves all chat conversations and deletes each one.
§Returns
Result<()>
- An emptyResult
, if all chat conversations are successfully deleted. Otherwise, an error.
§Errors
This function will return an error if the retrieval of chat conversations fails or if any chat conversation cannot be deleted.
Sourcepub async fn upload_attachment(&self, file_path: &str) -> Result<Value>
pub async fn upload_attachment(&self, file_path: &str) -> Result<Value>
Uploads an attachment to the API.
This function sends a POST request to the API to upload a document. The document is read from the file at the specified path and included in the request as a multipart form data. The MIME type of the document is determined based on its file extension.
§Arguments
file_path
- A string representing the path to the file to be uploaded.
§Returns
Result<Value>
- The API response, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the file cannot be opened, if the request fails, or if the response cannot be deserialized.
Sourcepub async fn send_message(
&self,
chat_uuid: &str,
prompt: &str,
attachments: Option<Vec<&str>>,
timeout: Option<u64>,
) -> Result<String>
pub async fn send_message( &self, chat_uuid: &str, prompt: &str, attachments: Option<Vec<&str>>, timeout: Option<u64>, ) -> Result<String>
Sends a message to a chat conversation.
This function sends a POST request to the API to append a message to a chat conversation. The message can include attachments, which are uploaded to the API before the message is sent. The function waits for a response from the API for a specified amount of time before timing out.
§Arguments
chat_uuid
- A string representing the UUID of the chat conversation.prompt
- A string representing the message to be sent.attachments
- An optional vector of strings representing the paths to the files to be uploaded as attachments.timeout
- An optional number representing the amount of time (in seconds) to wait for a response before timing out.
§Returns
Result<String>
- The API response, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if an attachment cannot be uploaded, if the request fails, if the response cannot be deserialized, or if the request times out.
Sourcepub async fn rename_chat(&self, chat_uuid: &str, title: &str) -> Result<()>
pub async fn rename_chat(&self, chat_uuid: &str, title: &str) -> Result<()>
Renames a chat conversation.
This function sends a POST request to the API to rename a chat conversation.
§Arguments
chat_uuid
- A string representing the UUID of the chat conversation to be renamed.title
- A string representing the new title for the chat conversation.
§Returns
Result<()>
- An emptyResult
, if the request is successful. Otherwise, an error.
§Errors
This function will return an error if the request fails.