Skip to content

Commit ae6c583

Browse files
committed
webhook feature flag
1 parent eb10004 commit ae6c583

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

async-openai/Cargo.toml

Lines changed: 4 additions & 3 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,9 +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 }
53-
hmac = "0.12.1"
54-
sha2 = "0.10.8"
55-
hex = "0.4.3"
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 }
5657

5758
[dev-dependencies]
5859
tokio-test = "0.4.4"

async-openai/src/error.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ pub enum OpenAIError {
2828
InvalidArgument(String),
2929
}
3030

31-
/// Errors that can occur when processing webhooks
32-
#[derive(Debug, thiserror::Error)]
33-
pub enum WebhookError {
34-
/// Invalid webhook signature or signature verification failed
35-
#[error("invalid webhook signature: {0}")]
36-
InvalidSignature(String),
37-
/// Failed to deserialize webhook payload
38-
#[error("failed to deserialize webhook payload: error:{0} content:{1}")]
39-
Deserialization(serde_json::Error, String),
40-
}
41-
4231
#[derive(Debug, thiserror::Error)]
4332
pub enum StreamError {
4433
/// Underlying error from reqwest_eventsource library when reading the stream

async-openai/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ mod vector_store_file_batches;
179179
mod vector_store_files;
180180
mod vector_stores;
181181
mod video;
182+
#[cfg(feature = "webhook")]
182183
pub mod webhooks;
183184

184185
pub use assistants::Assistants;

async-openai/src/types/mod.rs

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

3941
pub use assistant::*;

async-openai/src/webhooks.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
use crate::error::WebhookError;
21
use crate::types::webhooks::WebhookEvent;
32
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
43
use hmac::{Hmac, Mac};
54
use sha2::Sha256;
65
use std::time::{SystemTime, UNIX_EPOCH};
76

7+
/// Errors that can occur when processing webhooks
8+
#[derive(Debug, thiserror::Error)]
9+
pub enum WebhookError {
10+
/// Invalid webhook signature or signature verification failed
11+
#[error("invalid webhook signature: {0}")]
12+
InvalidSignature(String),
13+
/// Failed to deserialize webhook payload
14+
#[error("failed to deserialize webhook payload: error:{0} content:{1}")]
15+
Deserialization(serde_json::Error, String),
16+
}
17+
818
type HmacSha256 = Hmac<Sha256>;
919

1020
const DEFAULT_TOLERANCE_SECONDS: i64 = 300;
@@ -356,7 +366,7 @@ mod tests {
356366
assert!(result.is_err());
357367
assert!(matches!(
358368
result.unwrap_err(),
359-
WebhookError::Deserialization(_)
369+
WebhookError::Deserialization(..)
360370
));
361371
}
362372
}

0 commit comments

Comments
 (0)