Skip to content

Commit aa805d7

Browse files
64bitifsheldon
authored andcommitted
feat: webhooks (64bit#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 (cherry picked from commit 890bb6a)
1 parent fb8ae4f commit aa805d7

File tree

11 files changed

+1427
-0
lines changed

11 files changed

+1427
-0
lines changed

async-openai/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ native-tls = ["reqwest/native-tls"]
2727
native-tls-vendored = ["reqwest/native-tls-vendored"]
2828
realtime = []
2929
byot = []
30+
webhook = ["dep:hmac", "dep:sha2", "dep:hex"]
3031

3132
[dependencies]
3233
async-openai-macros = { path = "../async-openai-macros", version = "0.1.0" }
@@ -43,6 +44,10 @@ secrecy = { version = "0.10", features = ["serde"] }
4344
pin-project = "1.1"
4445
bytes = "1.9"
4546
eventsource-stream = "0.2"
47+
hmac = { version = "0.12", optional = true, default-features = false}
48+
sha2 = { version = "0.10", optional = true, default-features = false }
49+
hex = { version = "0.4", optional = true, default-features = false }
50+
4651

4752
[target.'cfg(target_arch = "wasm32")'.dependencies]
4853
rand = "0.9"

async-openai/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ Only types for Realtime API are implemented, and can be enabled with feature fla
8989

9090
Again, the types do not bundle with a specific WS implementation. Need to convert a client event into a WS message by yourself, which is just simple `your_ws_impl::Message::Text(some_client_event.into_text())`.
9191

92+
## Webhooks
93+
94+
Support for webhook event types, signature verification, and building webhook events from payloads can be enabled by using the `webhook` feature flag.
95+
9296
## Image Generation Example
9397

9498
```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::{Client, types::CreateEmbeddingRequestArgs};
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)