use std::collections::HashMap;
use std::sync::Arc;
use chrono::{DateTime, Utc};
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message;
use crate::authentification::{Credentials, PartialProfile, Profile};
use crate::billing::PageQuery;
use crate::error::{Error, Result};
use crate::groups::NamedGroup;
use crate::http::{client, encode, ok, websocket};
use crate::private::authentification::Account;
use crate::tickets::{optional, parse_date};
static ROUTE: crate::route::Cell = crate::route::cell();
pub fn set_route(route: &str) {
crate::route::set(&ROUTE, route);
}
pub fn get_url() -> String {
crate::route::url(&ROUTE)
}
pub async fn authorize_token(token: &str) -> Result<Credentials> {
let response = client()
.post(format!("{}/authentification/authorize", get_url()))
.header("Authorization", format!("Token {token}"))
.send()
.await?;
let mut credentials: Credentials = ok(response).await?.json().await?;
credentials.token = token.to_string();
Ok(credentials)
}
pub async fn redeem_handoff(code: &str) -> Result<String> {
let response = client()
.post(format!("{}/authentification/handoff/redeem", get_url()))
.json(&json!({ "code": code }))
.send()
.await?;
Ok(ok(response).await?.text().await?)
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AccountQuery {
pub total: u64,
pub accounts: Vec<Account>,
}
impl AccountQuery {
pub fn from_json(json: &Value) -> AccountQuery {
AccountQuery {
total: json["total"].as_u64().unwrap_or(0),
accounts: json["accounts"]
.as_array()
.map(|rows| rows.iter().map(Account::from_json).collect())
.unwrap_or_default(),
}
}
}
pub async fn query(credentials: &Credentials, profile: &PartialProfile) -> Result<AccountQuery> {
let response = client()
.post(format!("{}/credentials/query", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "email": profile.email, "displayName": profile.display_name }))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(AccountQuery::from_json(&json))
}
#[derive(Debug, Clone, Deserialize)]
pub struct SessionToken {
pub token: String,
}
pub async fn session(credentials: &Credentials, email: &str) -> Result<SessionToken> {
let response = client()
.post(format!("{}/authentification/session", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "email": email }))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone)]
pub enum Grant {
Tier(String),
Permissions(Vec<String>),
Credit {
credit: f64,
api_key: Option<String>,
},
}
pub async fn grant(credentials: &Credentials, email: &str, update: &Grant) -> Result<Account> {
let body = match update {
Grant::Tier(tier) => json!({ "email": email, "tier": tier }),
Grant::Permissions(permissions) => json!({ "email": email, "permissions": permissions }),
Grant::Credit { credit, api_key } => {
let mut body = json!({ "email": email, "credit": credit });
if let Some(api_key) = api_key {
body["apiKey"] = json!(api_key);
}
body
}
};
let response = client()
.post(format!("{}/authentification/grant", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&body)
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(Account::from_json(&json))
}
pub async fn grant_group(
credentials: &Credentials,
group: &str,
credit: f64,
) -> Result<NamedGroup> {
let response = client()
.post(format!("{}/authentification/grant", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "group": group, "credit": credit }))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(NamedGroup::from_json(&json))
}
pub async fn ban(credentials: &Credentials, profile: &Profile, banned: bool) -> Result<Account> {
let response = client()
.post(format!("{}/authentification/ban", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "email": profile.email, "banned": banned }))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(Account::from_json(&json))
}
pub async fn remove(credentials: &Credentials, email: &str) -> Result<bool> {
let response = client()
.delete(format!(
"{}/authentification?email={}",
get_url(),
encode(email)
))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
ok(response).await?;
Ok(true)
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ParrotSpreads {
pub llm_spread: f64,
pub embedder_spread: f64,
pub image_spread: f64,
pub video_spread: f64,
pub audio_spread: f64,
pub voice_spread: f64,
pub tts_spread: f64,
pub decision_spread: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MarmotSpreads {
pub search_spread: f64,
pub proxy_datacenter_per_gb: f64,
pub proxy_residential_per_gb: f64,
pub x_tweet_spread: f64,
pub x_user_spread: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct TermiteSpreads {
pub machine_spread_percent: f64,
pub storage_spread_percent: f64,
pub egress_spread_per_gb: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MapsSpreads {
pub geocode_spread: f64,
pub places_spread: f64,
pub autocomplete_spread: f64,
pub routes_spread: f64,
pub weather_spread: f64,
pub tiles_per_1000_spread: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct TravelSpreads {
pub flights_spread: f64,
pub hotels_spread: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct PaymentsSpreads {
pub card_percent: f64,
pub card_fixed: f64,
pub spread_percent: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct GlobalSpreads {
pub demo_budget_per_day: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Spreads {
pub parrot: ParrotSpreads,
pub marmot: MarmotSpreads,
pub termite: TermiteSpreads,
pub maps: MapsSpreads,
pub travel: TravelSpreads,
pub payments: PaymentsSpreads,
pub global: GlobalSpreads,
}
pub async fn list_spread(credentials: &Credentials) -> Result<Spreads> {
let response = client()
.get(format!("{}/spread", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SpreadApp {
Parrot,
Marmot,
Termite,
Maps,
Travel,
Payments,
Global,
}
impl SpreadApp {
fn as_str(self) -> &'static str {
match self {
SpreadApp::Parrot => "parrot",
SpreadApp::Marmot => "marmot",
SpreadApp::Termite => "termite",
SpreadApp::Maps => "maps",
SpreadApp::Travel => "travel",
SpreadApp::Payments => "payments",
SpreadApp::Global => "global",
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct SpreadConfig {
pub app: SpreadApp,
pub config: HashMap<String, f64>,
}
pub async fn set_spread(
credentials: &Credentials,
app: SpreadApp,
config: &HashMap<String, f64>,
) -> Result<SpreadConfig> {
let response = client()
.put(format!("{}/spread/{}", get_url(), encode(app.as_str())))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "config": config }))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone, Serialize)]
pub struct SpreadUsage {
pub app: String,
pub service: String,
pub day: String,
pub revenue: f64,
pub cost: f64,
pub credit: f64,
}
pub async fn usage_spread(credentials: &Credentials) -> Result<Vec<SpreadUsage>> {
let response = client()
.get(format!("{}/spread/usage", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
let rows: Vec<Value> = ok(response).await?.json().await?;
let dollars = |row: &Value, key: &str| row[key].as_f64().unwrap_or(0.0) / 1_000_000.0;
Ok(rows
.iter()
.map(|row| SpreadUsage {
app: row["app"].as_str().unwrap_or_default().to_string(),
service: row["service"].as_str().unwrap_or_default().to_string(),
day: row["day"].as_str().unwrap_or_default().to_string(),
revenue: dollars(row, "revenueMicros"),
cost: dollars(row, "costMicros"),
credit: dollars(row, "creditMicros"),
})
.collect())
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddonDefinition {
pub id: String,
pub name: String,
pub stripe_price_id: String,
pub capabilities: Vec<String>,
pub active: bool,
}
pub async fn list_addons(credentials: &Credentials) -> Result<Vec<AddonDefinition>> {
let response = client()
.get(format!("{}/billing/admin/addons", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NewsletterDraft {
pub subject: String,
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub html: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub test: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NewsletterSend {
pub id: Option<String>,
pub recipients: u64,
}
impl NewsletterSend {
pub fn from_json(json: &Value) -> NewsletterSend {
NewsletterSend {
id: match &json["id"] {
Value::Null => None,
Value::String(id) => Some(id.clone()),
id => Some(id.to_string()),
},
recipients: json["recipients"].as_u64().unwrap_or(0),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NewsletterStatus {
#[default]
Sending,
Sent,
Failed,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Newsletter {
pub id: String,
pub subject: String,
pub created_at: DateTime<Utc>,
pub status: NewsletterStatus,
pub recipients: u64,
pub sent: u64,
pub skipped: u64,
pub failed: u64,
pub finished_at: Option<DateTime<Utc>>,
}
impl Newsletter {
pub fn from_json(json: &Value) -> Newsletter {
let count = |key: &str| json[key].as_u64().unwrap_or(0);
Newsletter {
id: match &json["id"] {
Value::String(id) => id.clone(),
id => id.to_string(),
},
subject: json["subject"].as_str().unwrap_or_default().to_string(),
created_at: parse_date(&json["createdAt"]).unwrap_or_default(),
status: serde_json::from_value(json["status"].clone()).unwrap_or_default(),
recipients: count("recipients"),
sent: count("sent"),
skipped: count("skipped"),
failed: count("failed"),
finished_at: parse_date(&json["finishedAt"]),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Newsletters {
pub audience: u64,
pub newsletters: Vec<Newsletter>,
}
impl Newsletters {
pub fn from_json(json: &Value) -> Newsletters {
Newsletters {
audience: json["audience"].as_u64().unwrap_or(0),
newsletters: json["newsletters"]
.as_array()
.map(|rows| rows.iter().map(Newsletter::from_json).collect())
.unwrap_or_default(),
}
}
}
pub async fn send_newsletter(
credentials: &Credentials,
draft: &NewsletterDraft,
) -> Result<NewsletterSend> {
let response = client()
.post(format!("{}/newsletter", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(draft)
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(NewsletterSend::from_json(&json))
}
pub async fn newsletters(credentials: &Credentials) -> Result<Newsletters> {
let response = client()
.get(format!("{}/newsletter", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(Newsletters::from_json(&json))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TestSuite {
#[default]
BlackBox,
Termite,
Providers,
Live,
Big,
Gpu,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TestRunStatus {
#[default]
Queued,
Running,
Passed,
Failed,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TestRun {
pub id: String,
pub suite: TestSuite,
pub limit: f64,
pub by: String,
pub status: TestRunStatus,
pub passed: u64,
pub failed: u64,
pub skipped: u64,
pub spent: f64,
pub reason: Option<String>,
pub created_at: DateTime<Utc>,
pub finished_at: Option<DateTime<Utc>>,
}
impl TestRun {
pub fn from_json(json: &Value) -> TestRun {
let count = |key: &str| json[key].as_u64().unwrap_or(0);
let dollars = |key: &str| json[key].as_f64().unwrap_or(0.0);
TestRun {
id: text_id(&json["id"]),
suite: serde_json::from_value(json["suite"].clone()).unwrap_or_default(),
limit: dollars("limit"),
by: json["by"].as_str().unwrap_or_default().to_string(),
status: serde_json::from_value(json["status"].clone()).unwrap_or_default(),
passed: count("passed"),
failed: count("failed"),
skipped: count("skipped"),
spent: dollars("spent"),
reason: optional(&json["reason"]),
created_at: parse_date(&json["createdAt"]).unwrap_or_default(),
finished_at: parse_date(&json["finishedAt"]),
}
}
}
fn text_id(id: &Value) -> String {
match id {
Value::String(id) => id.clone(),
id => id.to_string(),
}
}
#[derive(Debug, Clone, Serialize)]
pub struct TestStarted {
pub id: String,
pub at: DateTime<Utc>,
pub logs: Option<String>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TestOutcomeStatus {
#[default]
Passed,
Failed,
Skipped,
}
#[derive(Debug, Clone, Serialize)]
pub struct TestOutcome {
pub id: String,
pub at: DateTime<Utc>,
pub name: String,
pub status: TestOutcomeStatus,
pub estimate: f64,
pub cost: f64,
pub reason: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct TestFinished {
pub id: String,
pub at: DateTime<Utc>,
pub passed: u64,
pub failed: u64,
pub skipped: u64,
pub spent: f64,
pub limit: f64,
pub reason: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "event", rename_all = "lowercase")]
pub enum TestEvent {
Started(TestStarted),
Test(TestOutcome),
Finished(TestFinished),
}
impl TestEvent {
pub fn from_json(json: &Value) -> Result<TestEvent> {
let id = text_id(&json["id"]);
let at = parse_date(&json["at"]).unwrap_or_default();
let count = |key: &str| json[key].as_u64().unwrap_or(0);
let dollars = |key: &str| json[key].as_f64().unwrap_or(0.0);
let reason = optional(&json["reason"]);
match json["event"].as_str() {
Some("started") => Ok(TestEvent::Started(TestStarted {
id,
at,
logs: optional(&json["logs"]),
})),
Some("test") => Ok(TestEvent::Test(TestOutcome {
id,
at,
name: json["name"].as_str().unwrap_or_default().to_string(),
status: serde_json::from_value(json["status"].clone()).unwrap_or_default(),
estimate: dollars("estimate"),
cost: dollars("cost"),
reason,
})),
Some("finished") => Ok(TestEvent::Finished(TestFinished {
id,
at,
passed: count("passed"),
failed: count("failed"),
skipped: count("skipped"),
spent: dollars("spent"),
limit: dollars("limit"),
reason,
})),
event => Err(Error::Message(format!(
"{} isn't a test event.",
event.unwrap_or("undefined")
))),
}
}
}
pub async fn run_tests(credentials: &Credentials, suite: TestSuite, limit: f64) -> Result<TestRun> {
let response = client()
.post(format!("{}/production/tests", get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "suite": suite, "limit": limit }))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(TestRun::from_json(&json))
}
pub async fn test_runs(credentials: &Credentials, query: PageQuery) -> Result<Vec<TestRun>> {
let response = client()
.get(format!(
"{}/production/tests?cursor={}&max={}",
get_url(),
query.cursor,
query.max
))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
let rows: Vec<Value> = ok(response).await?.json().await?;
Ok(rows.iter().map(TestRun::from_json).collect())
}
pub trait TestRunHooks: Send + Sync {
fn on_connection(&self, _run: &TestRun) {}
fn on_event(&self, _event: &TestEvent) {}
fn on_error(&self, _message: &str) {}
fn on_close(&self) {}
}
impl TestRunHooks for () {}
pub struct TestRunRoom {
closing: mpsc::UnboundedSender<()>,
}
impl TestRunRoom {
pub async fn connect(
credentials: &Credentials,
id: &str,
hooks: Option<Arc<dyn TestRunHooks>>,
) -> Result<TestRunRoom> {
let hooks = hooks.unwrap_or_else(|| Arc::new(()));
let params = json!({ "Authorization": format!("Token {}", credentials.token) });
let url = format!(
"{}/production/tests/live?run={}&authorization={}",
websocket(&get_url()),
encode(id),
encode(¶ms.to_string())
);
let (stream, _) = tokio_tungstenite::connect_async(url).await?;
let (mut write, mut read) = stream.split();
let (closing, mut closed) = mpsc::unbounded_channel::<()>();
tokio::spawn(async move {
loop {
tokio::select! {
Some(()) = closed.recv() => {
let _ = write.close().await;
break;
}
message = read.next() => match message {
Some(Ok(Message::Text(text))) => deliver(hooks.as_ref(), text.as_str()),
Some(Ok(Message::Close(_))) | None => break,
Some(Ok(_)) => {}
Some(Err(_)) => {
hooks.on_error("WebSocket connection failed");
break;
}
},
}
}
hooks.on_close();
});
Ok(TestRunRoom { closing })
}
pub fn close(&self) {
let _ = self.closing.send(());
}
}
fn deliver(hooks: &dyn TestRunHooks, text: &str) {
let Ok(packet) = serde_json::from_str::<Value>(text) else {
return;
};
match packet["event"].as_str() {
Some("connection") => hooks.on_connection(&TestRun::from_json(&packet["run"])),
Some("started" | "test" | "finished") => {
if let Ok(event) = TestEvent::from_json(&packet) {
hooks.on_event(&event);
}
}
Some("error") => hooks.on_error(packet["message"].as_str().unwrap_or_default()),
_ => {}
}
}