Skip to content

Commit a6c814f

Browse files
committed
chore(client): further formatting and added documentation
1 parent e70b6ee commit a6c814f

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ testcontainer = ["dep:testcontainers"]
1111
[dependencies]
1212
chrono = { version = "0.4.41", features = ["serde"] }
1313
cloudevents-sdk = { version = "0.8.0", features = ["reqwest"], optional = true }
14-
url = "2.5.4"
14+
futures = "0.3.31"
15+
futures-util = "0.3.31"
16+
jsonschema = "0.30.0"
1517
reqwest = { version = "0.12.15", features = ["json", "stream"] }
1618
serde = { version = "1.0.219", features = ["derive"] }
1719
serde_json = "1.0.140"
1820
testcontainers = { version = "0.24.0", features = [
1921
"http_wait",
2022
], optional = true }
2123
thiserror = "2.0.12"
22-
jsonschema = "0.30.0"
23-
futures-util = "0.3.31"
24+
tokio = { version = "1.44.2", features = ["io-util"] }
2425
tokio-util = { version = "0.7.15", features = ["io"] }
2526
tokio-stream = { version = "0.1.17", features = ["io-util"] }
26-
futures = "0.3.31"
27-
tokio = { version = "1.44.2", features = ["io-util"] }
2827
typed-builder = "0.21.0"
28+
url = "2.5.4"
2929

3030
[dev-dependencies]
3131
eventsourcingdb-client-rust = { path = ".", features = ["testcontainer"] }

src/client.rs

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@
2020
mod client_request;
2121
mod precondition;
2222

23+
use crate::{
24+
error::ClientError,
25+
event::{Event, EventCandidate, ManagementEvent},
26+
};
2327
use client_request::{
2428
ClientRequest, ListEventTypesRequest, ListSubjectsRequest, OneShotRequest, PingRequest,
2529
RegisterEventSchemaRequest, StreamingRequest, VerifyApiTokenRequest, WriteEventsRequest,
2630
list_event_types::EventType,
2731
};
28-
2932
use futures::Stream;
3033
pub use precondition::Precondition;
3134
use reqwest;
3235
use url::Url;
3336

34-
use crate::{
35-
error::ClientError,
36-
event::{Event, EventCandidate, ManagementEvent},
37-
};
38-
3937
/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
4038
#[derive(Debug)]
4139
pub struct Client {
@@ -84,8 +82,6 @@ impl Client {
8482
///
8583
/// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
8684
///
87-
/// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
88-
///
8985
/// # Errors
9086
/// This function will return an error if the request fails or if the URL is invalid.
9187
fn build_request<R: ClientRequest>(
@@ -203,6 +199,34 @@ impl Client {
203199

204200
/// Registers an event schema with the DB instance.
205201
///
202+
/// ```
203+
/// use eventsourcingdb_client_rust::event::EventCandidate;
204+
/// use futures::StreamExt;
205+
/// # use serde_json::json;
206+
/// # tokio_test::block_on(async {
207+
/// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
208+
/// let db_url = "http://localhost:3000/";
209+
/// let api_token = "secrettoken";
210+
/// # let db_url = container.get_base_url().await.unwrap();
211+
/// # let api_token = container.get_api_token();
212+
/// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
213+
/// let event_type = "io.eventsourcingdb.test";
214+
/// let schema = json!({
215+
/// "type": "object",
216+
/// "properties": {
217+
/// "id": {
218+
/// "type": "string"
219+
/// },
220+
/// "name": {
221+
/// "type": "string"
222+
/// }
223+
/// },
224+
/// "required": ["id", "name"]
225+
/// });
226+
/// client.register_event_schema(event_type, &schema).await.expect("Failed to list event types");
227+
/// # })
228+
/// ```
229+
///
206230
/// # Errors
207231
/// This function will return an error if the request fails or if the provided schema is invalid.
208232
pub async fn register_event_schema(
@@ -216,6 +240,44 @@ impl Client {
216240

217241
/// List all subjects in the DB instance.
218242
///
243+
/// To get all subjects in the DB, just pass `None` as the `base_subject`.
244+
/// ```
245+
/// use eventsourcingdb_client_rust::event::EventCandidate;
246+
/// use futures::StreamExt;
247+
/// # use serde_json::json;
248+
/// # tokio_test::block_on(async {
249+
/// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
250+
/// let db_url = "http://localhost:3000/";
251+
/// let api_token = "secrettoken";
252+
/// # let db_url = container.get_base_url().await.unwrap();
253+
/// # let api_token = container.get_api_token();
254+
/// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
255+
/// let mut subject_stream = client.list_subjects(None).await.expect("Failed to list event types");
256+
/// while let Some(subject) = subject_stream.next().await {
257+
/// println!("Found Type {}", subject.expect("Error while reading types"));
258+
/// }
259+
/// # })
260+
/// ```
261+
///
262+
/// To get all subjects under /test in the DB, just pass `Some("/test")` as the `base_subject`.
263+
/// ```
264+
/// use eventsourcingdb_client_rust::event::EventCandidate;
265+
/// use futures::StreamExt;
266+
/// # use serde_json::json;
267+
/// # tokio_test::block_on(async {
268+
/// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
269+
/// let db_url = "http://localhost:3000/";
270+
/// let api_token = "secrettoken";
271+
/// # let db_url = container.get_base_url().await.unwrap();
272+
/// # let api_token = container.get_api_token();
273+
/// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
274+
/// let mut subject_stream = client.list_subjects(Some("/test")).await.expect("Failed to list event types");
275+
/// while let Some(subject) = subject_stream.next().await {
276+
/// println!("Found Type {}", subject.expect("Error while reading types"));
277+
/// }
278+
/// # })
279+
/// ```
280+
///
219281
/// # Errors
220282
/// This function will return an error if the request fails or if the URL is invalid.
221283
pub async fn list_subjects(
@@ -232,6 +294,24 @@ impl Client {
232294

233295
/// List all event types in the DB instance.
234296
///
297+
/// ```
298+
/// use eventsourcingdb_client_rust::event::EventCandidate;
299+
/// use futures::StreamExt;
300+
/// # use serde_json::json;
301+
/// # tokio_test::block_on(async {
302+
/// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
303+
/// let db_url = "http://localhost:3000/";
304+
/// let api_token = "secrettoken";
305+
/// # let db_url = container.get_base_url().await.unwrap();
306+
/// # let api_token = container.get_api_token();
307+
/// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
308+
/// let mut type_stream = client.list_event_types().await.expect("Failed to list event types");
309+
/// while let Some(ty) = type_stream.next().await {
310+
/// println!("Found Type {}", ty.expect("Error while reading types").name);
311+
/// }
312+
/// # })
313+
/// ```
314+
///
235315
/// # Errors
236316
/// This function will return an error if the request fails or if the URL is invalid.
237317
pub async fn list_event_types(

0 commit comments

Comments
 (0)