#[cfg(not(feature = "async-client"))]
use reqwest::blocking::RequestBuilder;
#[cfg(feature = "async-client")]
use reqwest::RequestBuilder;
use super::{CaptureDefaults, ClientOptions};
use crate::error::Error;
use crate::event::{BatchRequest, Event, InnerEvent};
pub(crate) fn prepare_event(event: &mut Event, defaults: &CaptureDefaults) {
if defaults.disable_geoip {
event.insert_prop_default("$geoip_disable", serde_json::Value::Bool(true));
}
if defaults.is_server {
event.insert_prop_default("$is_server", serde_json::Value::Bool(true));
}
event.prepare_for_v0();
}
pub(crate) fn build_capture_payload(event: Event, api_key: String) -> Result<String, Error> {
let inner_event = InnerEvent::new(event, api_key);
serde_json::to_string(&inner_event).map_err(|e| Error::Serialization(e.to_string()))
}
pub(crate) fn build_batch_payload(
events: Vec<Event>,
api_key: String,
historical_migration: bool,
defaults: &CaptureDefaults,
) -> Result<String, Error> {
let inner_events: Vec<InnerEvent> = events
.into_iter()
.map(|mut event| {
prepare_event(&mut event, defaults);
InnerEvent::new(event, api_key.clone())
})
.collect();
let batch_request = BatchRequest {
api_key,
historical_migration,
batch: inner_events,
};
serde_json::to_string(&batch_request).map_err(|e| Error::Serialization(e.to_string()))
}
pub(crate) fn encode_body(
options: &ClientOptions,
json: String,
) -> (Vec<u8>, Option<&'static str>) {
match options.capture_compression {
Some(algo) => match crate::compression::compress(algo, json.as_bytes()) {
Some((bytes, encoding)) => (bytes, Some(encoding)),
None => (json.into_bytes(), None),
},
None => (json.into_bytes(), None),
}
}
pub(crate) fn apply_extra_headers(
#[allow(unused_variables)] options: &ClientOptions,
#[allow(unused_mut)] mut request: RequestBuilder,
) -> RequestBuilder {
#[cfg(feature = "test-harness")]
if let Some(ref extra) = options.extra_capture_headers {
for (k, v) in extra {
request = request.header(k.as_str(), v.as_str());
}
}
request
}