Skip to content

Commit

Permalink
add: better readme md and more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mertakman committed Nov 8, 2024
1 parent 468a7d7 commit 296dd69
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 112 deletions.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# Upstash Rust QStash SDK

# Upstash QStash Rust SDK

**QStash** is a robust, HTTP-based messaging and scheduling solution optimized for serverless and edge runtimes. With a stateless, HTTP-driven design, it supports a broad range of environments and platforms, including:

- Serverless functions (e.g., AWS Lambda) – [Example](https://github.com/mertakman/qstash-rs/tree/main/examples/aws-lambda/main.rs)
- Cloudflare Workers – [Example](https://github.com/mertakman/qstash-rs/tree/main/examples/cloudflare-workers/main.rs)
- Fastly Compute@Edge
- Next.js, including [Edge runtime](https://nextjs.org/docs/api-reference/edge-runtime)
- Deno
- Client-side web and mobile applications
- WebAssembly
- Any other environment where HTTP-based communication is preferred over TCP

## How QStash Works

QStash serves as the intermediary message broker for serverless applications. By sending a simple HTTP request to QStash, you can include a destination, payload, and optional configurations. QStash then stores the message securely and reliably delivers it to the designated API endpoint. In cases where the destination is temporarily unavailable, QStash ensures at-least-once delivery by automatically retrying until the message is successfully received.

## Quick Start

### 1. Obtain Your Authorization Token

To get started, head to the [Upstash Console](https://console.upstash.com/qstash) and copy your **QSTASH_TOKEN**.

### 2. Explore Examples

For API documentation and a quickstart guide, refer to the official [QStash API Documentation](https://upstash.com/docs/qstash/api/). Below, you'll find links to additional examples that demonstrate usage for each endpoint:

- **Dead Letter Queue**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/dead_letter_queue/main.rs)
- **Events**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/events/main.rs)
- **LLM**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/llm/main.rs)
- **Messages**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/messages/main.rs)
- **Queues**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/queues/main.rs)
- **Schedulers**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/schedulers/main.rs)
- **Signing Keys**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/signing_keys/main.rs)
- **URL Groups**[Example](https://github.com/mertakman/qstash-rs/blob/main/examples/url_groups/main.rs)

## Supported Environments

QStash is ideal for use with serverless architectures and edge deployments, supporting scenarios where HTTP-based communication provides flexibility and compatibility with modern applications.
File renamed without changes.
14 changes: 12 additions & 2 deletions examples/dead_letter_queue/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use std::env;

use qstash_rs::client::QstashClient;
use qstash_rs::{client::QstashClient, dead_letter_queue::DlqQueryParams};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");

let client = QstashClient::builder().api_key(&api_key).build().unwrap();
let client = QstashClient::builder().api_key(&api_key).build()?;

let dlq_messages_list = client.dlq_list_messages(DlqQueryParams::default()).await?;
println!("{:#?}", dlq_messages_list);

let message_list = vec![];
let deleted_messages_list = client.dlq_delete_messages(message_list).await?;
println!("{:#?}", deleted_messages_list);

let dlq_message_id = "";
client.dlq_delete_message(dlq_message_id).await?;

Ok(())
}
6 changes: 4 additions & 2 deletions examples/events/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use qstash_rs::{client::QstashClient, events_types::EventsRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");
let client = QstashClient::builder().api_key(&api_key).build().unwrap();
let client = QstashClient::builder().api_key(&api_key).build()?;

println!("Starting the process to retrieve the event list.");
let resp = client.list_events(EventsRequest::default()).await?;
println!("Successfully retrieved the events list.");
println!("Retrieved Events: {:#?}", resp);

println!("{:#?}", resp);
Ok(())
}
22 changes: 19 additions & 3 deletions examples/llm/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
use std::env;

use qstash_rs::client::QstashClient;
use qstash_rs::{
client::QstashClient,
llm_types::{ChatCompletionRequest, Message},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");
let client = QstashClient::builder().api_key(&api_key).build()?;

let client = QstashClient::builder().api_key(&api_key).build().unwrap();

let mut chat_completion_request = ChatCompletionRequest::default();
chat_completion_request.model = "meta-llama/Meta-Llama-3-8B-Instruct".to_string();
chat_completion_request.messages = vec![Message {
role: "user".to_string(),
content: "What is the capital of Türkiye?".to_string(),
name: None,
}];

println!("Starting the process to create a chat completion.");
let resp = client
.create_chat_completion(chat_completion_request)
.await?;
println!("Retrieved response succesfully");
println!("{:#?}", resp);
Ok(())
}
67 changes: 51 additions & 16 deletions examples/messages/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
use qstash_rs::client::QstashClient;
use http::{
header::{AUTHORIZATION, CONTENT_TYPE},
HeaderValue,
};
use qstash_rs::{client::QstashClient, message_types::MessageResponseResult};
use reqwest::header::HeaderMap;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");

let client = QstashClient::builder().api_key(&api_key).build().unwrap();
let destination = "https://www.example.com".to_string();
let body = "{\"message\": \"Hello, world!\"}".to_string();

let message_ids = [
"msg_id_0".to_string(),
"msg_id_1".to_string(),
"msg_id_2".to_string(),
"msg_id_3".to_string(),
];
let mut headers = HeaderMap::new();

let client = QstashClient::builder().api_key(" ").build().unwrap();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
headers.insert("Upstash-Method", HeaderValue::from_static("POST"));
headers.insert("Upstash-Delay", HeaderValue::from_static("0s"));
headers.insert("Upstash-Retries", HeaderValue::from_static("3"));
headers.insert(
"Upstash-Forward-Custom-Header",
HeaderValue::from_static("custom-value"),
);

let client: QstashClient = QstashClient::builder().api_key(&api_key).build()?;
println!(
"Starting to publish message to the destination: {}",
destination
);

let publish_message_resp = client
.publish_message(&destination, headers, body.clone().into())
.await?;
println!("Message published successfully to {}!", destination);
println!(
"Response from publishing message: {:#?}",
publish_message_resp
);

let message_id = match publish_message_resp {
MessageResponseResult::URLResponse(url_response) => url_response.message_id,
MessageResponseResult::URLGroupResponse(_) => {
panic!("Response is not of type URLResponse");
}
};

println!("Retrieving message with id: {}", message_id);
let get_message_resp = client.get_message(&message_id).await?;
println!("Successfully retrieved message with id: {}.", message_id);
println!("Retrieved message details: {:#?}", get_message_resp);

println!("Initiating cancellation of message with id: {}", message_id);
client.cancel_message(&message_id).await?;
println!(
"Message with id: {} has been cancelled successfully.",
message_id
);

// client.publish_message(destination, headers, body).await?;
// client.enqueue_message(destination, queue_name, headers, body)
// client.batch_messages(destination, queue_name, headers, body)
// client.get_message(destination, queue_name, headers, body)
// client.cancel_message(&"msg_id_0".to_string()).await?;
// client.bulk_cancel_messages(message_ids[1..].to_vec()).await?;
//
Ok(())
}
1 change: 0 additions & 1 deletion examples/queues/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");

let client = QstashClient::builder().api_key(&api_key).build().unwrap();

Ok(())
}
2 changes: 1 addition & 1 deletion examples/schedulers/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");

let client = QstashClient::builder().api_key(&api_key).build().unwrap();
let client = QstashClient::builder().api_key(&api_key).build()?;

Ok(())
}
9 changes: 9 additions & 0 deletions examples/signing_keys/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let client = QstashClient::builder().api_key(&api_key).build().unwrap();

let signing_keys = client.get_signing_keys().await?;
println!("Signing keys retrieved successfully");
println!("{:#?}", signing_keys);

println!("Rotating signing keys");
let new_signing_keys = client.rotate_signing_keys().await?;
println!("Signing keys rotated successfully");
println!("{:#?}", new_signing_keys);

Ok(())
}
3 changes: 1 addition & 2 deletions examples/url_groups/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use qstash_rs::client::QstashClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");
let api_key = "eyJVc2VySUQiOiJiMmIxZWIyYS1iNDUzLTQ3NzQtYTllMC0xOTYzMmJhMmE2YmQiLCJQYXNzd29yZCI6ImJhYTNmZmEyODNlYjQzMTdhOGJjY2ViYmIzYTliMGI2In0=".to_string();
let api_key = env::var("QSTASH_API_KEY").expect("QSTASH_API_KEY not set");
let client = QstashClient::builder().api_key(&api_key).build().unwrap();

Ok(())
Expand Down
Loading

0 comments on commit 296dd69

Please sign in to comment.