use std::sync::Arc;
use crate::core::error::{DelopayError, Result};
use crate::core::models::live::LiveVersionsResponse;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;
#[derive(Debug, Clone)]
pub struct Live {
inner: Arc<Inner>,
}
impl Live {
pub(crate) fn new(inner: Arc<Inner>) -> Self {
Self { inner }
}
pub async fn versions(&self, params: &LiveVersionsParams) -> Result<LiveVersionsResponse> {
if params.entities.is_empty() {
return Err(DelopayError::InvalidRequest(
"live versions need at least one entity".to_string(),
));
}
let mut query = vec![("entities", params.entities.join(","))];
if let Some(profile_id) = ¶ms.profile_id {
query.push(("profile_id", profile_id.clone()));
}
if let Some(record_id) = ¶ms.record_id {
query.push(("record_id", record_id.clone()));
}
self.inner
.request(HttpMethod::Get, "/live/versions", None, &query, None)
.await
}
}
#[derive(Debug, Clone, Default)]
pub struct LiveVersionsParams {
pub entities: Vec<String>,
pub profile_id: Option<String>,
pub record_id: Option<String>,
}
impl LiveVersionsParams {
pub fn new<I, S>(entities: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
entities: entities.into_iter().map(Into::into).collect(),
..Default::default()
}
}
#[must_use]
pub fn profile_id(mut self, profile_id: impl Into<String>) -> Self {
self.profile_id = Some(profile_id.into());
self
}
#[must_use]
pub fn record_id(mut self, record_id: impl Into<String>) -> Self {
self.record_id = Some(record_id.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http::mock::{Step, client_with};
#[tokio::test]
async fn versions_joins_the_entities_and_scopes_the_query() {
let (client, mock) = client_with(vec![Step::ok(
r#"{"versions":[{"entity":"connector","profile_id":"pro_1",
"record_id":"mca_1","version":3,
"by":{"kind":"user","user_id":"usr_1","name":"Ada"},
"if_match":"\"3\"","entry_surprise":1}],"surprise_field":true}"#,
)]);
let params = LiveVersionsParams::new(["connector", "routing"])
.profile_id("pro_1")
.record_id("mca_1");
let out = client.live().versions(¶ms).await.unwrap();
assert_eq!(out.versions[0].entity, "connector");
assert_eq!(out.versions[0].version, 3);
assert_eq!(out.versions[0].if_match, "\"3\"");
assert_eq!(
out.versions[0].by.as_ref().map(|b| b.kind.as_str()),
Some("user")
);
assert_eq!(out.versions[0].extra["entry_surprise"], 1);
assert_eq!(out.extra["surprise_field"], true);
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "GET");
assert_eq!(
seen[0].url,
"https://api.test/live/versions?entities=connector,routing\
&profile_id=pro_1&record_id=mca_1"
);
}
#[tokio::test]
async fn versions_refuses_an_empty_entity_list_before_sending() {
let (client, mock) = client_with(vec![]);
let params = LiveVersionsParams::new(Vec::<String>::new());
match client.live().versions(¶ms).await.unwrap_err() {
DelopayError::InvalidRequest(msg) => assert!(msg.contains("entity")),
other => panic!("expected InvalidRequest, got {other:?}"),
}
assert_eq!(mock.call_count(), 0);
}
}