use std::collections::HashMap;
use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize, Serializer};
use serde_json::Value;
use crate::types_v1::{SecretRequest, SignIn};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IntentStatus {
Queued,
Running,
RequiresPayment,
RequiresInput,
RequiresLogin,
Completed,
Failed,
Cancelled,
#[serde(other)]
Unknown,
}
impl IntentStatus {
pub fn is_final(self) -> bool {
matches!(self, Self::Completed | Self::Failed | Self::Cancelled)
}
pub fn as_str(self) -> &'static str {
match self {
Self::Queued => "queued",
Self::Running => "running",
Self::RequiresPayment => "requires_payment",
Self::RequiresInput => "requires_input",
Self::RequiresLogin => "requires_login",
Self::Completed => "completed",
Self::Failed => "failed",
Self::Cancelled => "cancelled",
Self::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateIntent {
pub intent: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub context: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub require_payment_link: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sign_in_agent: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sign_in_hint: Option<SignInHint>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sign_in_link: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub country: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub end_user_ip: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub input_ttl_seconds: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub secrets: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub shops: Option<Vec<String>>,
}
impl CreateIntent {
pub fn new(intent: impl Into<String>) -> Self {
Self {
intent: intent.into(),
..Default::default()
}
}
pub fn max_amount(mut self, cents: i64) -> Self {
self.max_amount = Some(cents);
self
}
pub fn currency(mut self, currency: impl Into<String>) -> Self {
self.currency = Some(currency.into());
self
}
pub fn context(mut self, context: Value) -> Self {
self.context = Some(context);
self
}
pub fn require_payment_link(mut self, yes: bool) -> Self {
self.require_payment_link = Some(yes);
self
}
pub fn sign_in_agent(mut self, yes: bool) -> Self {
self.sign_in_agent = Some(yes);
self
}
pub fn sign_in_hint(mut self, hint: SignInHint) -> Self {
self.sign_in_hint = Some(hint);
self
}
pub fn sign_in_link(mut self, yes: bool) -> Self {
self.sign_in_link = Some(yes);
self
}
pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata
.get_or_insert_with(HashMap::new)
.insert(key.into(), value.into());
self
}
pub fn customer(mut self, customer: impl Into<String>) -> Self {
self.customer = Some(customer.into());
self
}
pub fn country(mut self, country: impl Into<String>) -> Self {
self.country = Some(country.into());
self
}
pub fn end_user_ip(mut self, ip: impl Into<String>) -> Self {
self.end_user_ip = Some(ip.into());
self
}
pub fn webhook_url(mut self, url: impl Into<String>) -> Self {
self.webhook_url = Some(url.into());
self
}
pub fn input_ttl_seconds(mut self, seconds: u32) -> Self {
self.input_ttl_seconds = Some(seconds);
self
}
pub fn secrets<I, S>(mut self, handles: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.secrets = Some(handles.into_iter().map(Into::into).collect());
self
}
pub fn shops<I, S>(mut self, shops: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.shops = Some(shops.into_iter().map(Into::into).collect());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CountrySource {
Explicit,
Payer,
EndUserIp,
RequestIp,
Default,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignInRequest {
pub url: String,
pub shop: String,
pub expires_at: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct SignInHint {
#[serde(skip_serializing_if = "Option::is_none")]
pub phone: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
}
impl SignInHint {
pub fn phone(mut self, phone: impl Into<String>) -> Self {
self.phone = Some(phone.into());
self
}
pub fn email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InputKind {
Phone,
Email,
Code,
Approval,
Detail,
Sensitive,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputChoice {
pub id: String,
pub label: String,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub price_delta: Option<i64>,
#[serde(default)]
pub currency: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InputFieldType {
Text,
Email,
Phone,
Date,
Number,
Choice,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputField {
pub name: String,
pub label: String,
#[serde(rename = "type")]
pub kind: InputFieldType,
pub required: bool,
#[serde(default)]
pub options: Vec<InputChoice>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InputExpects {
Text,
Email,
Phone,
Choice,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InputChannel {
Sms,
Email,
Call,
App,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InputRequest {
pub id: String,
pub kind: InputKind,
pub shop: String,
pub prompt: String,
#[serde(default)]
pub options: Vec<String>,
#[serde(default)]
pub choices: Vec<InputChoice>,
#[serde(default)]
pub fields: Vec<InputField>,
#[serde(default)]
pub expects: Option<InputExpects>,
#[serde(default)]
pub channel: Option<InputChannel>,
#[serde(default)]
pub length: Option<u32>,
pub url: String,
pub expires_at: String,
pub attempts_left: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnswerInput {
Value { input: String, value: String },
Values {
input: String,
values: HashMap<String, String>,
},
Decline { input: String },
Done { input: String },
}
impl AnswerInput {
pub fn value(input: impl Into<String>, value: impl Into<String>) -> Self {
Self::Value {
input: input.into(),
value: value.into(),
}
}
pub fn values<I, K, V>(input: impl Into<String>, values: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<String>,
{
Self::Values {
input: input.into(),
values: values.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
}
}
pub fn decline(input: impl Into<String>) -> Self {
Self::Decline { input: input.into() }
}
pub fn done(input: impl Into<String>) -> Self {
Self::Done { input: input.into() }
}
}
impl Serialize for AnswerInput {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(Some(2))?;
match self {
Self::Value { input, value } => {
map.serialize_entry("input", input)?;
map.serialize_entry("value", value)?;
}
Self::Values { input, values } => {
map.serialize_entry("input", input)?;
map.serialize_entry("values", values)?;
}
Self::Decline { input } => {
map.serialize_entry("input", input)?;
map.serialize_entry("decline", &true)?;
}
Self::Done { input } => {
map.serialize_entry("input", input)?;
map.serialize_entry("done", &true)?;
}
}
map.end()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentRequest {
pub reason: String,
pub payment_url: String,
#[serde(default)]
pub merchant: Option<String>,
pub total: i64,
pub fee: i64,
pub currency: String,
pub expires_at: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReceiptItem {
pub name: String,
pub quantity: i64,
pub amount: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Receipt {
pub merchant: String,
#[serde(default)]
pub items: Vec<ReceiptItem>,
pub total: i64,
pub fee: i64,
pub currency: String,
#[serde(default)]
pub confirmation: Option<String>,
#[serde(default)]
pub receipt_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntentError {
pub code: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Intent {
pub id: String,
pub status: IntentStatus,
pub intent: String,
#[serde(default)]
pub customer: Option<String>,
#[serde(default)]
pub max_amount: Option<i64>,
#[serde(default)]
pub currency: Option<String>,
#[serde(default)]
pub context: Option<Value>,
#[serde(default)]
pub metadata: Option<HashMap<String, String>>,
#[serde(default)]
pub payment: Option<PaymentRequest>,
#[serde(default)]
pub sign_in: Option<SignInRequest>,
#[serde(default)]
pub input_request: Option<InputRequest>,
#[serde(default)]
pub webhook_url: Option<String>,
#[serde(default)]
pub webhook_secret: Option<String>,
#[serde(default)]
pub secrets: Option<Vec<String>>,
#[serde(default)]
pub shops: Vec<String>,
#[serde(default)]
pub country: Option<String>,
#[serde(default)]
pub country_source: Option<CountrySource>,
#[serde(default)]
pub result: Option<Receipt>,
#[serde(default)]
pub error: Option<IntentError>,
#[serde(default)]
pub test: bool,
pub created_at: String,
#[serde(default)]
pub completed_at: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListIntents {
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<IntentStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntentList {
pub data: Vec<Intent>,
#[serde(default)]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Balance {
pub available: i64,
#[serde(default)]
pub held: Option<i64>,
pub currency: String,
#[serde(default)]
pub customer: Option<String>,
#[serde(default)]
pub monthly_limit: Option<i64>,
#[serde(default)]
pub per_intent_limit: Option<i64>,
#[serde(default)]
pub spent_this_month: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Customer {
pub id: String,
#[serde(default)]
pub external_id: Option<String>,
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub email: Option<String>,
#[serde(default)]
pub metadata: HashMap<String, String>,
#[serde(default)]
pub limits: Option<Limits>,
pub created_at: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Limits {
#[serde(default)]
pub per_intent: Option<i64>,
#[serde(default)]
pub monthly: Option<i64>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct LimitsParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub per_intent: Option<Option<i64>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub monthly: Option<Option<i64>>,
}
impl LimitsParams {
pub fn per_intent(mut self, cents: Option<i64>) -> Self {
self.per_intent = Some(cents);
self
}
pub fn monthly(mut self, cents: Option<i64>) -> Self {
self.monthly = Some(cents);
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CustomerParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub external_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limits: Option<LimitsParams>,
}
impl CustomerParams {
pub fn limits(mut self, limits: LimitsParams) -> Self {
self.limits = Some(limits);
self
}
pub fn external_id(mut self, id: impl Into<String>) -> Self {
self.external_id = Some(id.into());
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata
.get_or_insert_with(HashMap::new)
.insert(key.into(), value.into());
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListCustomers {
#[serde(skip_serializing_if = "Option::is_none")]
pub external_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomerList {
pub data: Vec<Customer>,
#[serde(default)]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TopupStatus {
Open,
Paid,
Expired,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateTopup {
pub amount: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
}
impl CreateTopup {
pub fn new(amount: i64) -> Self {
Self {
amount,
..Default::default()
}
}
pub fn currency(mut self, currency: impl Into<String>) -> Self {
self.currency = Some(currency.into());
self
}
pub fn customer(mut self, customer: impl Into<String>) -> Self {
self.customer = Some(customer.into());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Topup {
pub id: String,
pub amount: i64,
pub currency: String,
#[serde(default)]
pub customer: Option<String>,
pub status: TopupStatus,
pub url: String,
pub expires_at: String,
pub created_at: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
pub id: String,
#[serde(rename = "type")]
pub kind: String,
pub created_at: String,
#[serde(default)]
pub data: Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventList {
pub data: Vec<Event>,
#[serde(default)]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListEvents {
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProfileStatus {
Ready,
InUse,
Forgetting,
Forgotten,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LeaseHolder {
#[serde(rename = "type")]
pub kind: String,
pub id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileLease {
pub holder: LeaseHolder,
pub expires_at: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Profile {
pub id: String,
#[serde(default)]
pub customer: Option<String>,
pub status: ProfileStatus,
#[serde(default)]
pub lease: Option<ProfileLease>,
#[serde(default)]
pub size_bytes: Option<u64>,
pub created_at: String,
#[serde(default)]
pub last_used_at: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateProfile {
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
}
impl CreateProfile {
pub fn customer(mut self, customer: impl Into<String>) -> Self {
self.customer = Some(customer.into());
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListProfiles {
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileList {
pub data: Vec<Profile>,
#[serde(default)]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SessionStatus {
Queued,
Opening,
Open,
Closing,
Closed,
Failed,
#[serde(other)]
Unknown,
}
impl SessionStatus {
pub fn is_final(self) -> bool {
matches!(self, Self::Closed | Self::Failed)
}
pub fn as_str(self) -> &'static str {
match self {
Self::Queued => "queued",
Self::Opening => "opening",
Self::Open => "open",
Self::Closing => "closing",
Self::Closed => "closed",
Self::Failed => "failed",
Self::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateSession {
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub start_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_minutes: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub country: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub end_user_ip: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
}
impl CreateSession {
pub fn country(mut self, country: impl Into<String>) -> Self {
self.country = Some(country.into());
self
}
pub fn end_user_ip(mut self, ip: impl Into<String>) -> Self {
self.end_user_ip = Some(ip.into());
self
}
pub fn webhook_url(mut self, url: impl Into<String>) -> Self {
self.webhook_url = Some(url.into());
self
}
pub fn profile(mut self, profile: impl Into<String>) -> Self {
self.profile = Some(profile.into());
self
}
pub fn customer(mut self, customer: impl Into<String>) -> Self {
self.customer = Some(customer.into());
self
}
pub fn start_url(mut self, url: impl Into<String>) -> Self {
self.start_url = Some(url.into());
self
}
pub fn max_minutes(mut self, minutes: u32) -> Self {
self.max_minutes = Some(minutes);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
pub id: String,
pub profile: String,
#[serde(default)]
pub customer: Option<String>,
pub status: SessionStatus,
#[serde(default)]
pub test: bool,
#[serde(default)]
pub start_url: Option<String>,
#[serde(default)]
pub url: Option<String>,
#[serde(default)]
pub title: Option<String>,
pub created_at: String,
#[serde(default)]
pub opened_at: Option<String>,
pub expires_at: String,
#[serde(default)]
pub closed_at: Option<String>,
#[serde(default)]
pub close_reason: Option<String>,
#[serde(default)]
pub country: Option<String>,
#[serde(default)]
pub country_source: Option<CountrySource>,
#[serde(default)]
pub webhook_url: Option<String>,
#[serde(default)]
pub webhook_secret: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListSessions {
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<SessionStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub customer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionList {
pub data: Vec<Session>,
#[serde(default)]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionCommand {
Navigate { url: String },
Snapshot,
Act { element: String, action: ActAction },
Read { selector: Option<String> },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ActAction {
Click,
Type(String),
Select(String),
Press(String),
}
impl SessionCommand {
pub fn navigate(url: impl Into<String>) -> Self {
Self::Navigate { url: url.into() }
}
pub fn snapshot() -> Self {
Self::Snapshot
}
pub fn read() -> Self {
Self::Read { selector: None }
}
pub fn read_selector(selector: impl Into<String>) -> Self {
Self::Read {
selector: Some(selector.into()),
}
}
pub fn click(element: impl Into<String>) -> Self {
Self::act(element, ActAction::Click)
}
pub fn type_text(element: impl Into<String>, text: impl Into<String>) -> Self {
Self::act(element, ActAction::Type(text.into()))
}
pub fn select(element: impl Into<String>, value: impl Into<String>) -> Self {
Self::act(element, ActAction::Select(value.into()))
}
pub fn press(element: impl Into<String>, key: impl Into<String>) -> Self {
Self::act(element, ActAction::Press(key.into()))
}
fn act(element: impl Into<String>, action: ActAction) -> Self {
Self::Act {
element: element.into(),
action,
}
}
}
impl Serialize for SessionCommand {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
match self {
Self::Navigate { url } => {
map.serialize_entry("type", "navigate")?;
map.serialize_entry("url", url)?;
}
Self::Snapshot => map.serialize_entry("type", "snapshot")?,
Self::Act { element, action } => {
map.serialize_entry("type", "act")?;
map.serialize_entry("ref", element)?;
let (name, field) = match action {
ActAction::Click => ("click", None),
ActAction::Type(text) => ("type", Some(("text", text))),
ActAction::Select(value) => ("select", Some(("value", value))),
ActAction::Press(key) => ("press", Some(("key", key))),
};
map.serialize_entry("action", name)?;
if let Some((key, value)) = field {
map.serialize_entry(key, value)?;
}
}
Self::Read { selector } => {
map.serialize_entry("type", "read")?;
if let Some(selector) = selector {
map.serialize_entry("selector", selector)?;
}
}
}
map.end()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SessionCommandStatus {
Pending,
Done,
Refused,
Failed,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Failure {
pub code: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionCommandResult {
pub id: String,
#[serde(rename = "type")]
pub kind: String,
pub status: SessionCommandStatus,
#[serde(default)]
pub url: Option<String>,
#[serde(default)]
pub title: Option<String>,
#[serde(default)]
pub snapshot: Option<String>,
#[serde(default)]
pub text: Option<String>,
#[serde(default)]
pub error: Option<Failure>,
pub created_at: String,
#[serde(default)]
pub completed_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(try_from = "RawWebhookEvent")]
pub struct WebhookEvent {
pub id: String,
#[serde(rename = "type")]
pub kind: String,
pub created_at: String,
pub data: WebhookData,
}
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum WebhookData {
Intent(Box<Intent>),
Session(Box<Session>),
SignIn(Box<SignIn>),
SecretRequest(Box<SecretRequest>),
Other(Value),
}
impl WebhookEvent {
pub fn intent(&self) -> Option<&Intent> {
match &self.data {
WebhookData::Intent(intent) => Some(intent),
_ => None,
}
}
pub fn session(&self) -> Option<&Session> {
match &self.data {
WebhookData::Session(session) => Some(session),
_ => None,
}
}
pub fn sign_in(&self) -> Option<&SignIn> {
match &self.data {
WebhookData::SignIn(sign_in) => Some(sign_in),
_ => None,
}
}
pub fn secret_request(&self) -> Option<&SecretRequest> {
match &self.data {
WebhookData::SecretRequest(request) => Some(request),
_ => None,
}
}
}
#[derive(Deserialize)]
struct RawWebhookEvent {
id: String,
#[serde(rename = "type")]
kind: String,
created_at: String,
#[serde(default)]
data: Value,
}
impl TryFrom<RawWebhookEvent> for WebhookEvent {
type Error = serde_json::Error;
fn try_from(raw: RawWebhookEvent) -> Result<Self, Self::Error> {
let data = if raw.kind.starts_with("intent.") {
WebhookData::Intent(Box::new(serde_json::from_value(raw.data)?))
} else if raw.kind.starts_with("session.") {
WebhookData::Session(Box::new(serde_json::from_value(raw.data)?))
} else if raw.kind.starts_with("sign_in.") {
WebhookData::SignIn(Box::new(serde_json::from_value(raw.data)?))
} else if raw.kind.starts_with("secret_request.") {
WebhookData::SecretRequest(Box::new(serde_json::from_value(raw.data)?))
} else {
WebhookData::Other(raw.data)
};
Ok(Self {
id: raw.id,
kind: raw.kind,
created_at: raw.created_at,
data,
})
}
}