spacetimedb_cli/
api.rs

1use std::iter::Sum;
2use std::ops::Add;
3
4use reqwest::{header, Client, RequestBuilder};
5use serde::Deserialize;
6
7use spacetimedb_lib::db::raw_def::v9::RawModuleDefV9;
8use spacetimedb_lib::de::serde::DeserializeWrapper;
9use spacetimedb_lib::Identity;
10
11use crate::util::{AuthHeader, ResponseExt};
12
13static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
14
15#[derive(Debug, Clone)]
16pub struct Connection {
17    pub(crate) host: String,
18    pub(crate) database_identity: Identity,
19    pub(crate) database: String,
20    pub(crate) auth_header: AuthHeader,
21}
22
23impl Connection {
24    pub fn db_uri(&self, endpoint: &str) -> String {
25        [
26            &self.host,
27            "/v1/database/",
28            &self.database_identity.to_hex(),
29            "/",
30            endpoint,
31        ]
32        .concat()
33    }
34}
35
36pub fn build_client(con: &Connection) -> Client {
37    let mut builder = Client::builder().user_agent(APP_USER_AGENT);
38
39    if let Some(auth_header) = con.auth_header.to_header() {
40        let headers = http::HeaderMap::from_iter([(header::AUTHORIZATION, auth_header)]);
41
42        builder = builder.default_headers(headers);
43    }
44
45    builder.build().unwrap()
46}
47
48pub struct ClientApi {
49    pub con: Connection,
50    client: Client,
51}
52
53impl ClientApi {
54    pub fn new(con: Connection) -> Self {
55        let client = build_client(&con);
56        Self { con, client }
57    }
58
59    pub fn sql(&self) -> RequestBuilder {
60        self.client.post(self.con.db_uri("sql"))
61    }
62
63    /// Reads the `ModuleDef` from the `schema` endpoint.
64    pub async fn module_def(&self) -> anyhow::Result<RawModuleDefV9> {
65        let res = self
66            .client
67            .get(self.con.db_uri("schema"))
68            .query(&[("version", "9")])
69            .send()
70            .await?;
71        let DeserializeWrapper(module_def) = res.json_or_error().await?;
72        Ok(module_def)
73    }
74
75    pub async fn call(&self, reducer_name: &str, arg_json: String) -> anyhow::Result<reqwest::Response> {
76        Ok(self
77            .client
78            .post(self.con.db_uri("call") + "/" + reducer_name)
79            .header(http::header::CONTENT_TYPE, "application/json")
80            .body(arg_json)
81            .send()
82            .await?)
83    }
84}
85
86pub(crate) type SqlStmtResult<'a> =
87    spacetimedb_client_api_messages::http::SqlStmtResult<&'a serde_json::value::RawValue>;
88
89#[derive(Debug, Clone, Deserialize, Default)]
90pub struct StmtStats {
91    pub total_duration_micros: u64,
92    pub rows_inserted: u64,
93    pub rows_updated: u64,
94    pub rows_deleted: u64,
95    pub total_rows: usize,
96}
97
98impl Sum<StmtStats> for StmtStats {
99    fn sum<I: Iterator<Item = StmtStats>>(iter: I) -> Self {
100        iter.fold(StmtStats::default(), Add::add)
101    }
102}
103
104impl Add for StmtStats {
105    type Output = Self;
106
107    fn add(self, rhs: Self) -> Self::Output {
108        Self {
109            total_duration_micros: self.total_duration_micros + rhs.total_duration_micros,
110            rows_inserted: self.rows_inserted + rhs.rows_inserted,
111            rows_deleted: self.rows_deleted + rhs.rows_deleted,
112            rows_updated: self.rows_updated + rhs.rows_updated,
113            total_rows: self.total_rows + rhs.total_rows,
114        }
115    }
116}
117
118impl From<&SqlStmtResult<'_>> for StmtStats {
119    fn from(value: &SqlStmtResult<'_>) -> Self {
120        Self {
121            total_duration_micros: value.total_duration_micros,
122            rows_inserted: value.stats.rows_inserted,
123            rows_deleted: value.stats.rows_deleted,
124            rows_updated: value.stats.rows_updated,
125            total_rows: value.rows.len(),
126        }
127    }
128}
129
130pub fn from_json_seed<'de, T: serde::de::DeserializeSeed<'de>>(
131    s: &'de str,
132    seed: T,
133) -> Result<T::Value, serde_json::Error> {
134    let mut de = serde_json::Deserializer::from_str(s);
135    let out = seed.deserialize(&mut de)?;
136    de.end()?;
137    Ok(out)
138}