Skip to content

Commit 890bb6a

Browse files
authored
feat: webhooks (#462)
* types for webhook; traits for events * checkpoint: working webhooks * updates * return string which failed deserialiaztion * update * webhook feature flag * update example * update readme * cleanup
1 parent fa1a399 commit 890bb6a

File tree

11 files changed

+1426
-0
lines changed

11 files changed

+1426
-0
lines changed

async-openai/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ native-tls-vendored = ["reqwest/native-tls-vendored"]
2525
realtime = ["dep:tokio-tungstenite"]
2626
# Bring your own types
2727
byot = []
28+
webhook = ["dep:hmac", "dep:sha2", "dep:hex"]
2829

2930
[dependencies]
3031
async-openai-macros = { path = "../async-openai-macros", version = "0.1.0" }
@@ -50,6 +51,9 @@ secrecy = { version = "0.10.3", features = ["serde"] }
5051
bytes = "1.9.0"
5152
eventsource-stream = "0.2.3"
5253
tokio-tungstenite = { version = "0.26.1", optional = true, default-features = false }
54+
hmac = { version = "0.12", optional = true, default-features = false}
55+
sha2 = { version = "0.10", optional = true, default-features = false }
56+
hex = { version = "0.4", optional = true, default-features = false }
5357

5458
[dev-dependencies]
5559
tokio-test = "0.4.4"

async-openai/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ $Env:OPENAI_API_KEY='sk-...'
6868

6969
Only types for Realtime API are implemented, and can be enabled with feature flag `realtime`.
7070

71+
## Webhooks
72+
73+
Support for webhook event types, signature verification, and building webhook events from payloads can be enabled by using the `webhook` feature flag.
74+
7175
## Image Generation Example
7276

7377
```rust

async-openai/src/embedding.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<'c, C: Config> Embeddings<'c, C> {
6464

6565
#[cfg(test)]
6666
mod tests {
67+
use crate::error::OpenAIError;
6768
use crate::types::{CreateEmbeddingResponse, Embedding, EncodingFormat};
6869
use crate::{types::CreateEmbeddingRequestArgs, Client};
6970

async-openai/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ mod vector_store_file_batches;
179179
mod vector_store_files;
180180
mod vector_stores;
181181
mod video;
182+
#[cfg(feature = "webhook")]
183+
pub mod webhooks;
182184

183185
pub use assistants::Assistants;
184186
pub use audio::Audio;

async-openai/src/traits.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@ pub trait AsyncTryFrom<T>: Sized {
55
/// Performs the conversion.
66
fn try_from(value: T) -> impl std::future::Future<Output = Result<Self, Self::Error>> + Send;
77
}
8+
9+
/// Trait for events to get their event type string.
10+
pub trait EventType {
11+
/// Returns the event type string (e.g., "batch.cancelled")
12+
fn event_type(&self) -> &'static str;
13+
}
14+
15+
/// Trait for events to get their event ID.
16+
pub trait EventId {
17+
/// Returns the event ID
18+
fn event_id(&self) -> &str;
19+
}

async-openai/src/types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ mod upload;
3434
mod users;
3535
mod vector_store;
3636
mod video;
37+
#[cfg_attr(docsrs, doc(cfg(feature = "webhook")))]
38+
#[cfg(feature = "webhook")]
39+
pub mod webhooks;
3740

3841
pub use assistant::*;
3942
pub use assistant_stream::*;

0 commit comments

Comments
 (0)