use std::sync::{Arc, Mutex, PoisonError};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::{AdsDriver, Campaign, CampaignDraft, CampaignPerformance};
use crate::error::{DriverError, Result};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AdsInteraction {
pub request: Value,
pub response: Value,
}
impl AdsInteraction {
pub fn ok(op: &str, args: Value, ok: Value) -> Self {
Self { request: request(op, args), response: json!({ "ok": ok }) }
}
}
fn request(op: &str, args: Value) -> Value {
json!({ "op": op, "args": args })
}
fn recorded_error(error: &Value) -> DriverError {
let message =
error.get("message").and_then(Value::as_str).unwrap_or("recorded error").to_string();
match error.get("code").and_then(Value::as_str).unwrap_or_default() {
"drivers.invalid" => DriverError::Invalid { reason: message },
"drivers.refused" => DriverError::Refused { reason: message },
"drivers.not_found" => DriverError::NotFound { what: message },
"drivers.unauthorized" => {
DriverError::Unauthorized { service: "ads".into(), reason: message }
}
_ => DriverError::unavailable("ads", message),
}
}
#[derive(Debug, Default)]
pub struct RecordedAds {
interactions: Mutex<Vec<(AdsInteraction, bool)>>,
}
impl RecordedAds {
pub fn new(interactions: Vec<AdsInteraction>) -> Self {
Self {
interactions: Mutex::new(
interactions.into_iter().map(|interaction| (interaction, false)).collect(),
),
}
}
pub fn from_json(text: &str) -> Result<Self> {
let interactions: Vec<AdsInteraction> = serde_json::from_str(text).map_err(|error| {
DriverError::invalid(format!("not a list of recorded interactions: {error}"))
})?;
Ok(Self::new(interactions))
}
pub fn unused(&self) -> Vec<AdsInteraction> {
self.interactions
.lock()
.unwrap_or_else(PoisonError::into_inner)
.iter()
.filter(|(_, used)| !used)
.map(|(interaction, _)| interaction.clone())
.collect()
}
fn answer<T: DeserializeOwned>(&self, op: &str, args: Value) -> Result<T> {
let wanted = request(op, args);
let mut interactions = self.interactions.lock().unwrap_or_else(PoisonError::into_inner);
let Some((interaction, used)) = interactions
.iter_mut()
.find(|(interaction, used)| !*used && interaction.request == wanted)
else {
return Err(DriverError::NoRecording { request: wanted.to_string() });
};
*used = true;
if let Some(error) = interaction.response.get("error") {
return Err(recorded_error(error));
}
let ok = interaction.response.get("ok").cloned().unwrap_or(Value::Null);
serde_json::from_value(ok)
.map_err(|error| DriverError::protocol("ads", format!("recorded answer: {error}")))
}
}
fn window(from: DateTime<Utc>, to: DateTime<Utc>) -> Value {
json!({ "from": from.to_rfc3339(), "to": to.to_rfc3339() })
}
#[async_trait]
impl AdsDriver for RecordedAds {
fn name(&self) -> &str {
"recorded"
}
async fn performance(
&self,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> Result<Vec<CampaignPerformance>> {
if to <= from {
return Err(DriverError::invalid("a window ends after it starts"));
}
self.answer("performance", window(from, to))
}
async fn launch_campaign(&self, draft: &CampaignDraft) -> Result<Campaign> {
draft.validate()?;
self.answer("launch_campaign", serde_json::to_value(draft).unwrap_or_default())
}
async fn set_bid(&self, campaign: &str, bid_minor: u64, currency: &str) -> Result<Campaign> {
self.answer(
"set_bid",
json!({ "campaign": campaign, "bidMinor": bid_minor, "currency": currency }),
)
}
async fn pause_campaign(&self, campaign: &str) -> Result<Campaign> {
self.answer("pause_campaign", json!({ "campaign": campaign }))
}
}
pub struct RecordingAds {
inner: Arc<dyn AdsDriver>,
recorded: Mutex<Vec<AdsInteraction>>,
}
impl std::fmt::Debug for RecordingAds {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RecordingAds").field("inner", &self.inner.name()).finish_non_exhaustive()
}
}
impl RecordingAds {
pub fn new(inner: Arc<dyn AdsDriver>) -> Self {
Self { inner, recorded: Mutex::default() }
}
pub fn interactions(&self) -> Vec<AdsInteraction> {
self.recorded.lock().unwrap_or_else(PoisonError::into_inner).clone()
}
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(&self.interactions()).unwrap_or_default()
}
fn record<T: Serialize>(&self, op: &str, args: Value, result: &Result<T>) {
let response = match result {
Ok(value) => json!({ "ok": value }),
Err(error) => {
json!({ "error": { "code": error.code(), "message": error.to_string() } })
}
};
self.recorded
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push(AdsInteraction { request: request(op, args), response });
}
}
#[async_trait]
impl AdsDriver for RecordingAds {
fn name(&self) -> &str {
self.inner.name()
}
async fn performance(
&self,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> Result<Vec<CampaignPerformance>> {
let result = self.inner.performance(from, to).await;
self.record("performance", window(from, to), &result);
result
}
async fn launch_campaign(&self, draft: &CampaignDraft) -> Result<Campaign> {
let result = self.inner.launch_campaign(draft).await;
self.record("launch_campaign", serde_json::to_value(draft).unwrap_or_default(), &result);
result
}
async fn set_bid(&self, campaign: &str, bid_minor: u64, currency: &str) -> Result<Campaign> {
let result = self.inner.set_bid(campaign, bid_minor, currency).await;
self.record(
"set_bid",
json!({ "campaign": campaign, "bidMinor": bid_minor, "currency": currency }),
&result,
);
result
}
async fn pause_campaign(&self, campaign: &str) -> Result<Campaign> {
let result = self.inner.pause_campaign(campaign).await;
self.record("pause_campaign", json!({ "campaign": campaign }), &result);
result
}
}