forge_core/webhook/
context.rs1use std::collections::HashMap;
2use std::sync::Arc;
3use std::time::Duration;
4
5use uuid::Uuid;
6
7use crate::env::{EnvAccess, EnvProvider, RealEnvProvider};
8use crate::function::JobDispatch;
9use crate::http::CircuitBreakerClient;
10
11pub struct WebhookContext {
13 pub webhook_name: String,
15 pub request_id: String,
17 pub idempotency_key: Option<String>,
19 headers: HashMap<String, String>,
21 db_pool: sqlx::PgPool,
23 http_client: CircuitBreakerClient,
25 http_timeout: Option<Duration>,
28 job_dispatch: Option<Arc<dyn JobDispatch>>,
30 env_provider: Arc<dyn EnvProvider>,
32}
33
34impl WebhookContext {
35 pub fn new(
37 webhook_name: String,
38 request_id: String,
39 headers: HashMap<String, String>,
40 db_pool: sqlx::PgPool,
41 http_client: CircuitBreakerClient,
42 ) -> Self {
43 Self {
44 webhook_name,
45 request_id,
46 idempotency_key: None,
47 headers,
48 db_pool,
49 http_client,
50 http_timeout: None,
51 job_dispatch: None,
52 env_provider: Arc::new(RealEnvProvider::new()),
53 }
54 }
55
56 pub fn with_idempotency_key(mut self, key: Option<String>) -> Self {
58 self.idempotency_key = key;
59 self
60 }
61
62 pub fn with_job_dispatch(mut self, dispatcher: Arc<dyn JobDispatch>) -> Self {
64 self.job_dispatch = Some(dispatcher);
65 self
66 }
67
68 pub fn with_env_provider(mut self, provider: Arc<dyn EnvProvider>) -> Self {
70 self.env_provider = provider;
71 self
72 }
73
74 pub fn db(&self) -> crate::function::ForgeDb {
76 crate::function::ForgeDb::from_pool(&self.db_pool)
77 }
78
79 pub async fn conn(&self) -> sqlx::Result<crate::function::ForgeConn<'static>> {
81 Ok(crate::function::ForgeConn::Pool(
82 self.db_pool.acquire().await?,
83 ))
84 }
85
86 pub fn http(&self) -> crate::http::HttpClient {
88 self.http_client.with_timeout(self.http_timeout)
89 }
90
91 pub fn raw_http(&self) -> &reqwest::Client {
93 self.http_client.inner()
94 }
95
96 pub fn http_with_circuit_breaker(&self) -> crate::http::HttpClient {
97 self.http()
98 }
99
100 pub fn set_http_timeout(&mut self, timeout: Option<Duration>) {
101 self.http_timeout = timeout;
102 }
103
104 pub fn header(&self, name: &str) -> Option<&str> {
108 self.headers.get(&name.to_lowercase()).map(|s| s.as_str())
109 }
110
111 pub fn headers(&self) -> &HashMap<String, String> {
113 &self.headers
114 }
115
116 pub async fn dispatch_job<T: serde::Serialize>(
130 &self,
131 job_type: &str,
132 args: T,
133 ) -> crate::error::Result<Uuid> {
134 let dispatcher = self.job_dispatch.as_ref().ok_or_else(|| {
135 crate::error::ForgeError::Internal("Job dispatch not available".into())
136 })?;
137 let args_json = serde_json::to_value(args)?;
138 dispatcher.dispatch_by_name(job_type, args_json, None).await
139 }
140
141 pub async fn cancel_job(
143 &self,
144 job_id: Uuid,
145 reason: Option<String>,
146 ) -> crate::error::Result<bool> {
147 let dispatcher = self.job_dispatch.as_ref().ok_or_else(|| {
148 crate::error::ForgeError::Internal("Job dispatch not available".into())
149 })?;
150 dispatcher.cancel(job_id, reason).await
151 }
152}
153
154impl EnvAccess for WebhookContext {
155 fn env_provider(&self) -> &dyn EnvProvider {
156 self.env_provider.as_ref()
157 }
158}
159
160#[cfg(test)]
161#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
162mod tests {
163 use super::*;
164
165 #[tokio::test]
166 async fn test_webhook_context_creation() {
167 let pool = sqlx::postgres::PgPoolOptions::new()
168 .max_connections(1)
169 .connect_lazy("postgres://localhost/nonexistent")
170 .expect("Failed to create mock pool");
171
172 let mut headers = HashMap::new();
173 headers.insert("x-github-event".to_string(), "push".to_string());
174 headers.insert("x-github-delivery".to_string(), "abc-123".to_string());
175
176 let ctx = WebhookContext::new(
177 "github_webhook".to_string(),
178 "req-123".to_string(),
179 headers,
180 pool,
181 CircuitBreakerClient::with_defaults(reqwest::Client::new()),
182 )
183 .with_idempotency_key(Some("abc-123".to_string()));
184
185 assert_eq!(ctx.webhook_name, "github_webhook");
186 assert_eq!(ctx.request_id, "req-123");
187 assert_eq!(ctx.idempotency_key, Some("abc-123".to_string()));
188 assert_eq!(ctx.header("X-GitHub-Event"), Some("push"));
189 assert_eq!(ctx.header("x-github-event"), Some("push")); assert!(ctx.header("nonexistent").is_none());
191 }
192}