Skip to content

Commit 3ff308e

Browse files
authored
Allow integration to proxy requests (#127)
1 parent 49cfbe0 commit 3ff308e

File tree

3 files changed

+329
-56
lines changed

3 files changed

+329
-56
lines changed

crates/common/src/integrations/testlight.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ use std::sync::Arc;
22

33
use async_trait::async_trait;
44
use error_stack::{Report, ResultExt};
5-
use fastly::http::{header, Method};
5+
use fastly::http::{header, HeaderValue};
66
use fastly::{Request, Response};
77
use serde::{Deserialize, Serialize};
88
use serde_json::{Map, Value};
99
use validator::Validate;
1010

11-
use crate::backend::ensure_backend_from_url;
1211
use crate::constants::{HEADER_SYNTHETIC_FRESH, HEADER_SYNTHETIC_TRUSTED_SERVER};
1312
use crate::error::TrustedServerError;
1413
use crate::integrations::{
1514
AttributeRewriteAction, IntegrationAttributeContext, IntegrationAttributeRewriter,
1615
IntegrationEndpoint, IntegrationProxy, IntegrationRegistration,
1716
};
17+
use crate::proxy::{proxy_request, ProxyRequestConfig};
1818
use crate::settings::{IntegrationConfig as IntegrationConfigTrait, Settings};
1919
use crate::synthetic::{generate_synthetic_id, get_or_generate_synthetic_id};
2020
use crate::tsjs;
@@ -143,20 +143,20 @@ impl IntegrationProxy for TestlightIntegration {
143143

144144
payload.user.id = Some(synthetic_id.clone());
145145

146-
let mut upstream = Request::new(Method::POST, self.config.endpoint.clone());
147-
upstream.set_header(header::CONTENT_TYPE, "application/json");
148-
upstream
149-
.set_body_json(&payload)
146+
let payload_bytes = serde_json::to_vec(&payload)
150147
.change_context(Self::error("Failed to serialize request body"))?;
151148

152-
if let Some(user_agent) = req.get_header(header::USER_AGENT) {
153-
upstream.set_header(header::USER_AGENT, user_agent);
154-
}
149+
let mut proxy_config = ProxyRequestConfig::new(&self.config.endpoint);
150+
proxy_config.forward_synthetic_id = false;
151+
proxy_config.body = Some(payload_bytes);
152+
proxy_config.stream_passthrough = true;
153+
proxy_config.headers.push((
154+
header::CONTENT_TYPE,
155+
HeaderValue::from_static("application/json"),
156+
));
155157

156-
let backend = ensure_backend_from_url(&self.config.endpoint)
157-
.change_context(Self::error("Failed to determine backend"))?;
158-
let mut response = upstream
159-
.send(backend)
158+
let mut response = proxy_request(settings, req, proxy_config)
159+
.await
160160
.change_context(Self::error("Failed to contact upstream integration"))?;
161161

162162
// Attempt to parse response into structured form for logging/future transforms.

0 commit comments

Comments
 (0)