use amplify::confinement;
use chrono::{DateTime, Utc};
use strict_types::{StrictVal, TypeName};
use crate::{CallRequest, CallState, Endpoint, MethodName, StateName};
impl<T, A> CallRequest<T, A> {
pub fn new(scope: T, auth: A, data: Option<StrictVal>) -> Self {
Self {
scope,
api: None,
call: None,
auth,
data,
lock: None,
expiry: None,
endpoints: Default::default(),
unknown_query: Default::default(),
}
}
pub fn use_api(mut self, api: impl Into<TypeName>) -> Self {
self.api = Some(api.into());
self
}
pub fn use_method(mut self, method: MethodName) -> Self {
if let Some(call) = &mut self.call {
call.method = method;
} else {
self.call = Some(CallState::new(method));
}
self
}
pub fn use_state(mut self, state: StateName) -> Self {
let mut call = self
.call
.expect("use_method must be called before use_state");
call.destructible = Some(state);
self.call = Some(call);
self
}
pub fn use_expiry(mut self, expiry: DateTime<Utc>) -> Self {
self.expiry = Some(expiry);
self
}
pub fn add_endpoint(mut self, endpoint: Endpoint) -> Result<Self, confinement::Error> {
self.endpoints.push(endpoint)?;
Ok(self)
}
}