pub struct Client { /* private fields */ }Expand description
An async client for queries, table writes, and catalog operations.
Start with Self::builder to configure your endpoint and API key.
Clones share the underlying HTTP connection pool.
Implementations§
Source§impl Client
impl Client
Sourcepub fn builder(endpoint: impl ToString) -> ClientBuilder
pub fn builder(endpoint: impl ToString) -> ClientBuilder
Starts a client builder for the common API-key authentication path.
Examples found in repository?
34pub fn client() -> Result<Client, Box<dyn std::error::Error>> {
35 let api_key = std::env::var("SCOPEDB_API_KEY")
36 .ok()
37 .filter(|value| !value.is_empty())
38 .or_else(|| {
39 std::env::var("SCOPEDB_TOKEN")
40 .ok()
41 .filter(|value| !value.is_empty())
42 });
43 let mut builder = Client::builder(endpoint());
44 if let Some(api_key) = api_key {
45 builder = builder.api_key(api_key);
46 }
47 Ok(builder.build()?)
48}Sourcepub fn new<E: IntoUrl>(endpoint: E, client: Client) -> Result<Self, Error>
pub fn new<E: IntoUrl>(endpoint: E, client: Client) -> Result<Self, Error>
Creates a ScopeDB client from an endpoint and a compatible HTTP client.
Use the HTTP types re-exported from crate::reqwest to avoid a
dependency-version mismatch in applications using another reqwest major.
Configure the HTTP client as described in ClientBuilder::http_client
to prevent automatic write replay. The write timeout defaults to 30 seconds.
Sourcepub fn statement(&self, statement: impl Into<String>) -> Statement
pub fn statement(&self, statement: impl Into<String>) -> Statement
Examples found in repository?
18async fn main() -> Result<(), Box<dyn std::error::Error>> {
19 let client = common::client()?;
20
21 let result = client.query("SELECT 1 AS ready").await?;
22 let rows = result.into_objects()?;
23 println!("rows: {rows:?}");
24
25 // Keep the handle when an application needs progress, cancellation, or a
26 // durable statement ID before waiting for the result.
27 let mut handle = client.statement("SELECT 2 AS value").submit().await?;
28 let statement_id = handle.statement_id();
29 println!(
30 "statement {statement_id} submitted: {:?}",
31 handle.snapshot()
32 );
33 let status = handle.refresh().await?;
34 println!("statement {statement_id}: {status:?}");
35 let result = handle.wait().await?;
36 println!("handle rows: {:?}", result.into_objects()?);
37 Ok(())
38}pub fn statement_handle(&self, statement_id: Uuid) -> StatementHandle
Sourcepub fn table(&self, table: impl Into<String>) -> Table
pub fn table(&self, table: impl Into<String>) -> Table
Examples found in repository?
50pub fn write_table(client: &Client) -> Result<Table, io::Error> {
51 let table = std::env::var("SCOPEDB_TABLE")
52 .ok()
53 .filter(|value| !value.is_empty())
54 .ok_or_else(|| {
55 io::Error::new(
56 io::ErrorKind::InvalidInput,
57 "set SCOPEDB_TABLE to a disposable table before running this write example",
58 )
59 })?;
60
61 Ok(client
62 .table(table)
63 .with_database(database())
64 .with_schema(schema()))
65}More examples
18async fn main() -> Result<(), Box<dyn std::error::Error>> {
19 let client = common::client()?;
20 let table_name = std::env::var("SCOPEDB_TABLE").unwrap_or_else(|_| "events".to_string());
21 let table = client
22 .table(table_name)
23 .with_database(common::database())
24 .with_schema(common::schema());
25
26 println!("identifier: {}", table.identifier());
27
28 let description = table.catalog().get().await?.definition;
29 for column in description.columns {
30 println!("{}: {:?}", column.name, column.data_type);
31 }
32
33 Ok(())
34}Sourcepub async fn query(
&self,
statement: impl Into<String>,
) -> Result<ResultSet, Error>
pub async fn query( &self, statement: impl Into<String>, ) -> Result<ResultSet, Error>
Executes a ScopeQL statement and waits for all result rows.
Errors after submission retain Error::statement_id for resuming the
existing execution with Self::statement_handle, not resubmitting it.
Examples found in repository?
18async fn main() -> Result<(), Box<dyn std::error::Error>> {
19 let client = common::client()?;
20
21 let result = client.query("SELECT 1 AS ready").await?;
22 let rows = result.into_objects()?;
23 println!("rows: {rows:?}");
24
25 // Keep the handle when an application needs progress, cancellation, or a
26 // durable statement ID before waiting for the result.
27 let mut handle = client.statement("SELECT 2 AS value").submit().await?;
28 let statement_id = handle.statement_id();
29 println!(
30 "statement {statement_id} submitted: {:?}",
31 handle.snapshot()
32 );
33 let status = handle.refresh().await?;
34 println!("statement {statement_id}: {status:?}");
35 let result = handle.wait().await?;
36 println!("handle rows: {:?}", result.into_objects()?);
37 Ok(())
38}Sourcepub fn catalog(&self) -> Catalog
pub fn catalog(&self) -> Catalog
Selects the complete declarative catalog or an individual resource.
Examples found in repository?
18async fn main() -> Result<(), Box<dyn std::error::Error>> {
19 let client = common::client()?;
20 let database = common::database();
21 let schema = common::schema();
22
23 let catalog = client.catalog().get().await?;
24 for item in catalog.definition.databases {
25 println!(
26 "database {}: {}",
27 item.name,
28 item.comment.unwrap_or_default()
29 );
30 }
31 let snapshot = client.catalog().schema(&database, &schema).get().await?;
32 println!("schema ETag: {}", snapshot.etag);
33 for table in snapshot.definition.tables {
34 println!("table {}: {:?}", table.name, table.columns);
35 }
36 Ok(())
37}Sourcepub async fn append_rows(
&self,
database: &str,
schema: &str,
table: &str,
ndjson: impl Into<String>,
) -> Result<AppendRowsResult, Error>
pub async fn append_rows( &self, database: &str, schema: &str, table: &str, ndjson: impl Into<String>, ) -> Result<AppendRowsResult, Error>
Appends at most 8 MiB of uncompressed newline-delimited JSON to a table.
Sends one request without automatic retries. An unknown outcome remains
non-retryable through Error::is_retryable because replay may duplicate rows.